CS201 Exam02

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What was returned by LinearSearch when the array named data was searched with a key of 0 (zero).

-1

What is the underlying integer value for HIGHER in the Compare enumerated type?

1 (The default values for enumeration constants is one more than its predecessor. So, MIDDLE would be 0 and HIGHER would be 1.)

When using Bubble Sort to sort 5 2 3 1 9 4, what is the final result of pass 3?

1 2 3 [4 5 9]

The notation [1, 4) would include which range of integers?

1, 2, 3 (A bracket means to include the boundary number. A parenthesis means to exclude the boundary number.)

What is the output of the following code? (Feel free to try in Visual Studio, but make sure you understand why the output occurs.) using System; class Pet { public string Name { get; set; } public static int NumPets { get; set; } public int TestProperty { get; set; } public Pet(string name) { this.Name = name; NumPets++; TestProperty++; } } class Program { static void Main() { Pet pet1 = new Pet("Shadow"); Pet pet2 = new Pet("Sunny"); Console.WriteLine($"{Pet.NumPets:N0}, {pet2.TestProperty:N0}"); } }

2, 1

When using Bubble Sort to sort 8 3 4 9 7 6, what is the final result of Pass 1?

3 4 8 7 6 [9] 8 3 4 9 7 6 3 8 4 9 7 6 3 4 8 9 7 6 3 4 8 9 7 6 3 4 8 7 9 6 3 4 8 7 6 [9]

When searching for 43 using Binary Search, which of the following shows the values assigned to mid?

3,5,6

Using Insertion Sort, show the data for each pass when sorting 5 2 3 1 9 4. Precede each line with Pass i: (where i is 1, 2, ...) Use brackets to show the sorted data for each pass.e.g.Pass 1: numbers and brackets herePass 2: numbers and brackets herePass 3 : numbers and brackets hereetc.

5 2 3 1 9 4 Pass 1: [2 5] 3 1 9 4 Pass 2: [2 3 5] 1 9 4 Pass 3: [1 2 3 5] 9 4 Pass 4: [1 2 3 5 9] 4 Pass 5: [1 2 3 4 5 9]

What is the output for the following line of code: Console.WriteLine(2 + 3 + " is the sum.");

5 is the sum.

When using Selection Sort (selecting the smallest) to sort 8 9 7 4 3 6, what is the result of pass 3?

8 9 7 4 3 6 Pass 1: [3] 9 7 4 8 6 (8 and 3 switch) Pass 2: [3 4] 7 9 8 6 (9 and 4 switch) Pass 3: [3 4 6] 9 8 7 (7 and 6 switch)

For an array of 256 sorted elements, what is the number of elements checked in the worst case when using binary search?

9 nine 9 elements nine elements (Log2256 + 1 = 8 + 1 = 9)

Using Selection Sort (selecting the smallest), show the data for each pass when sorting 9 5 6 2 4. Precede each line with Pass i: (where i is 1, 2, ...) Use brackets to show the sorted data for each pass.e.g.Pass 1: numbers and brackets herePass 2: numbers and brackets herePass 3 : numbers and brackets hereetc.

9 5 6 2 4 Pass 1: [2] 5 6 9 4 Pass 2: [2 4] 6 9 5 Pass 3: [2 4 5] 9 6 Pass 4: [2 4 5 6] 9

One may avoid the special escape sequence meaning attributed to a backslash in a string literal by preceding the literal with a/an

@

In the external header documentation for a method, __________ precedes each parameter name.

@param

What was the output for Topping.CHOCOLATE on line 23?

CHOCOLATE

When calling a static method that exists in another class, the method name is preceded by what?

Class name and a dot

Given the rectangular scores array that is shown in the notes, write one statement that would add the cell containing 3 to the cell containing 6 and store that result back into the cell containing 3. Hard-coding 3 and 6 into your answer is not permitted as your answer should work even if 3 and 6 were not stored there.

scores[0,0] += scores[1,1];

Given the 2 x 3 rectangular scores array that is initialized as shown in the notes, which expression below would refer to the cell containing 5?

scores[0,2]

Suppose scores is an array containing integers. Write one statement that increases the value of the first element by 2.

scores[0] += 2;

Write an expression that represents the number of elements in an array named students.

students.Length

Suppose an array of integers named tally exists. Using either a pre-increment or post-increment operator, write a statement that would increase the value stored at index k by 1.

tally[k]++; or ++tally[k];

When evaluating a binary operation that contains a float and a long,

the long is promoted to a float before the operation is performed.

Write one compound logical expression that would evaluate to true if there were all X's in the top row of the tic-tac-toe board in the notes, else it would be false. This is just an expression -- not a statement such as an if statement. (Recall that compound expressions contain and operators, or operators, or maybe both)

ticTacToeBoard[0,0] == 'X' && ticTacToeBoard[0,1] == 'X' && ticTacToeBoard[0,2] == 'X'

In the program that rolls the dice 5000 times, what is the purpose of the 4 in the following line of code? Console.WriteLine($"{face, 4}{frequency[face], 10}");

Display the value of face right-aligned in a field width of 4.

In the last grades processing example, suppose the correct input file name for grades has been entered and the file is opened successfully. When attempting to loop through and read the grades, what is the resulting output to the Console window if file data could not be converted to an integer? If there is no Console output for failing to read an integer, just state "no output".

Error processing grades.

Array items must all be of the same type. True/False

False

If the number of file records is not known when building a program to read from a file, then this number must always be read from the file header. True/False

False

The ref keyword could be used for getCredentials if the login method's code is changed to first initialize both username and password to the empty string.

False

There were six array elements allocated for the capitals array because there were six lines in the input file. True/False

False (5 elements were allocated)

A method should be created to accomplish a variety of tasks. True/False

False (A method should be designed to perform a single task.)

The following method signature is valid: DisplayRange(int lower = 1, int upper) True/False

False (All optional parameters (with the defaults) must be specified after any required parameters (with no defaults))

To process a file in the Visual Studio source code folder (Visual Studio 2019 and newer), a relative path that backs up exactly one folder level (e.g. "..\\" or @"..\") must precede the file name. True/False

False (Backing up three folder levels ("..\\..\\..\\" or @"..\..\..\") must be used starting with Visual Studio 2019 .NET Core and newer. Prior versions likely need two folder levels ("..\\..\\" or @"..\..\"))

If the parameter passing technique of "pass-by-value" is used with array objects, then the calling method will not be aware of any data changes to the array. True/False

False (Both the called and calling methods will refer to the same array object in memory.)

An object must be created before the static Main method may execute. True/False

False (It is static so an object does not need to be first created.)

When tracing program flow with the debugger, we found that the "this" keyword was available to the static FindMax method. True/False

False (It is static, so there was no 'this' keyword.)

If two methods have the same name and only differ in their parameter names, they are still considered to be different methods (and thus overloaded). True/False

False (Names do not make a difference)

A catch works like a method because once finished executing, program flow automatically returns back to the next statement to execute in the try block. True/False

False (Program flow will transfer to the finally block if one exists, else it will transfer to the next executable statement after the try-catch block.)

Non-static fields are shared among objects instantiated from the same class. True/False

False (Static fields are shared among objects instantiated from the same class.)

The following code is a valid statement for opening a stream to read from a file named Data.txt. (Assume the proper using statements are included)StreamReader inFile = StreamReader(FileStream("Data.txt", FileMode.Open)); True/False

False (StreamReader inFile = new StreamReader(new FileStream("Data.txt", FileMode.Open));)

An argument that is a double cannot be implicitly or explicitly converted to match a formal parameter that is an integer. True/False

False (The argument could be explicitly cast to an integer.)

The number of the array index and the array element are the same. True/False

False (The array index is one less than the array element.)

If a finally clause is included, it must be surrounded with brackets. True/False

False (The brackets were only used in the syntax description to show that the finally clause is optional. They are not included when coding)

The first index in a rectangular array represents columns and the second represents rows. True/False

False (The first index in a rectangular array represents rows and the second represents columns.)

The lifetime of a non-static field is the duration of the running program (application). True/False

False (The lifetime of a non-static field is the duration of the object's existence.)

The output of the program where array is passed by reference to the method named GetNewArray is: 1 2 3 True/False

False (The output is: 10 20 )

Statements in the finally clause of a try-catch are only executed if an exception is thrown and caught. True/False

False (They are always executed.)

The scope of class members are the point at which they are declared/defined to the remainder of the class. True/False

False (They are visible to the entire class.)

The scope of newRate and newTime included both Main and ComputeDistance. True/False

False (They were limited to ComputeDistance.)

An argument of type int could be implicitly promoted when sending to a formal parameter that is a ulong. True/False

False (an int argument could only be implicitly promoted to long, float, double, or decimal.)

Write a conditional expression using the File class that could be used in an if statement's header parentheses to check if data\students.txt exists. Only write the expression (not any part of the if statement), and just hard code in data\students.txt rather than creating variables such filePath and fileName.

File.Exists("data\\students.txt") or File.Exists(@"data\students.txt")

______________ is the extent of an identifier's existence.

Lifetime or Life time

If the ToString() method in Pet.cs did not use the override keyword, then line 12 (Console.WriteLine(myPet);) would display _________________

Pet

____________ is the extent to which an identifier is visible (or accessible) to other parts of the code.

Scope

The _____________class will read characters from a byte stream.

StreamReader

What is the output for the following line of code?:Console.WriteLine("Sum: " + 2 + 3);

Sum: 23 (2 is converted to a string and appended to 'Sum: '. Then, the 3 is converted to a string and appended to 'Sum: 2'.)

Which namespace contains many of the stream processing classes used in this unit?

System.IO

For the array of Person objects, what method or property was implicitly called by the following statement in the for loop?Console.WriteLine(team[i]);

The ToString() method of the Person class.

If the catch block on line 27 in the last grade processing example was just a catch with no Exception "parameter", what would you not have access to in the WriteLine on line 29?

The message stored in the exception

In the example that displayed the sum of the numbers in Numbers.txt, what would happen if a blank line were placed between each number in the file? You may enter and run the code with the modified file shown below. 3 3 1 0 4

The program will crash because an unhandled exception is generated.

In the example where grades are processed from CS134grades.txt and then transformed into equivalent letter grades, what is the purpose of the ,3 in the WriteLine statement in the while loop?

To right align the data from the associated variable in a field width of 3.

A Random object can be instantiated to always produce the same sequence of "random" numbers. True/False

True

A static method or property cannot access something that is non-static unless it is knows which object to target. True/False

True

A stream ultimately becomes a sequence of 1's and 0's which are represented by high and low voltages when in a computer's random access memory (RAM). True/False

True

For Binary Search to work, the data must already be in sorted order. True/False

True

If two methods have the same name and only differ in their parameter data types, they are still considered to be different methods (and thus overloaded). True/False

True

In the nested for loop example, row is also visible to the inner for loop. True/False

True

In the program where array is passed by value to the method named GetNewArray, Main will not be aware of the new array object created by GetNewArray. True/False

True

Method arguments may consist of complex expressions. True/False

True

The program that rolls the dice 5000 times never accesses frequency[0] True/False

True

The variable named bonus would not be accessible outside the if statement in the notes/lectures' example shown. True/False

True

When implementing Bubble Sort, it could be that when the biggest item is bubbling to the bottom that other elements also switch. True/False

True

If the fourth DisplaySequence method with 3 integer parameters beginning on line 37 were removed, the program would still run. True/False

True (It would still run because the integer offset would be implicitly promoted to a double.)

When using Selection Sort (selecting the smallest) to sort the data for 5 2 3 1 9 4, what was the order of the data for pass 3?

[1 2 3] 5 9 4

When using Insertion Sort to sort 9 5 6 2 4, what is the result of Pass 3?

[2 5 6 9] 4

The newRate parameter contained

a copy of rate's value which is 50.0

A method call consists of zero or more _________ parameters. These are also known as arguments.

actual

Write one statement that declares a character array of letter grades named grades that is initialized to the characters 'A', 'B', 'C'

char[] grades = {'A', 'B', 'C'};

It is a good habit to _______________________ a file/stream immediately when finished with it.

close

Suppose a 2 dimensional jagged array named data exists. Which expression below would refer to the number of elements in the 4th row?

data[3].Length

Write one statement to declare and assign to rates a new 10x20 rectangular array of default decimal values. (Do not use the form where braces are used). The literal values of 10 and 20 may be used in your statement.

decimal[,] rates = new decimal[10, 20];

Write one statement that declares an array named salaries that will hold NUM_EMPLOYEES salaries. (Assume that NUM_EMPLOYEES is already declared and contains a value.)

decimal[] salaries = new decimal[NUM_EMPLOYEES];

The scope of a local variable or local constant is from its ____________ until the end of the _______________ that contains its declaration.

declaration; block

The recursive case(s) must ____________________ the size of the problem to be solved.

decrease, diminish, reduce

The lifetime of a local variable or local constant is from the point its declaration is _________________ until the block in which it is declared finishes execution.

executed

A formal parameter's lifetime is as long as the method is ____________

executing or running

Use one statement to declare and initialize a jagged array named data that has 3 default integers in the first row and 5 default integers in the second row.

int[][] data = { new int[3], new int[5] };

A recursive method is a method that calls _________________

itself

For an array of 16 sorted elements, what is the number of elements checked in the worst case for linear and binary search?

linear = 16; binary = 5

In Linear Search, how many elements are compared in the worst case?

n

Suppose a numeric array named numbers is being declared (but not yet assigned a new array object). What is the default value of numbers?

null

In pass-by-reference, the ______________ keyword is used when the calling method does not initially specify a value for a parameter.

out

When the called method's formal parameters contain a copy of the arguments sent, this parameter passing technique is referred to as

pass by value; pass-by-value

The static Main method is an application's entry ___________

point

When calling a method, the parameters in the method call are termed ______________ or actual parameters.

arguments

Suppose an array of doubles named rates exists. Write the header of a foreach loop (not the body) that would access each array element with a variable named rate.

foreach (double rate in rates)

Suppose a Random object named randomNumbers has been instantiated. Also suppose an integer variable named coin has been declared. Write one syntactically correct statement that would randomly assign either a 0 or a 1 to coin.

random randomNumbers = new Random (Coin); or coin = randomNumbers.Next(2); or coin = randomNumbers.Next(0, 2);

Suppose a rectangular array named rates exists. Which expression below would refer to the number of columns in a row?

rates.GetLength(1)


Ensembles d'études connexes

Oncogenes and Tumour Suppressor Genes

View Set

OSHA 30 Construction Test Answer Key ELECTROCAUTION ClickSafety Redvector - Flash Cards

View Set

Chapter 20 Textbook Questions: Conditions Occuring During Pregnancy

View Set

Romeo and Juliet Act 1, Scene i-v

View Set