CIS 199 - Exam 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

How many iterations will the following loop complete? for (int i = 10; i <= 1; --i) WriteLine("i =" + i);

0

How many iterations will the following loop complete? for (int i = 10; i >= 1; --1) WriteLine("i = " + i);

10

Look at the following code sample: int [ , ] values = {{ 1, 2, 3, 4 } , { 5, 6, 7, 8 }}; What is the value stored in values [1,1]?

6

What will the value of the variable test be after the following sequence of statements complete execution? Enter the numeric value below. int x = 87; int test; test = x--;

87

Explain the difference between a method's parameters and its arguments. Which is used in the method's definition? Which is used in the method's call?

A parameter to a method is a variable that holds data passed to the method when it is called. The parameter is the variable listed in the header of the method's definition. An argument is the data listed in the method call. The parameter will receive the argument's value when the method executes.

In C# arrays, the first element is found at index position one.

False

When the keyword void appears in the method header, it means that the method will return a value.

False

When you instantiate an array, you can choose its exact location in memory.

False

You may modify array elements traversed using a foreach loop.

False

EC: Explain how a jagged 2-D array is different than a rectangular array.

In a rectangular 2-D array every row has the same number of columns but in a jagged 2-D array each row can have a different number of columns.

In C#, all array have a(n) ___ that is set to the number of elements in the array.

Length property

We've looked at three primary loop statements in C# - the While loop, the Do-While loop, and the For loop. All of these loops are similar and, in most ways, functionally equivalent. In what ways are they different (focus on when each loop's condition is tested)? For each loop, describe under what circumstances its use is preferred over the others?

The While loop and For loop are pretest loops. They test their condition before the body of loop executes. The Do-While loop is a posttest loop. It tests its condition after the body of the loop executes. Some situations lend themselves better to one type of loop than another. The For loop is commonly used with definite repetition such as counter-controlled repetition because its structure allows you to easily specify the starting and ending points of a counted loop and the specific update needed all in one line of code. If the situation is indefinite and requires that the body needs to execute at least once, Do-While is the best fit because it guarantees that at least one iteration will occur. The While loop is commonly used with other indefinite repetition situations.

A partially-filled array is normally used with accompanying integer variable that holds the number of items that are actually stored in the array.

True

A variable that refers to an object like an array actually stores the address of the object's location in memory.

True

At most, a method can directly return one value to the statement that calls it.

True

Once an array object's size has been allocated, that array object's size may not be changed.

True

When a method is declared with the private access modifier, it can only be called by code inside the same class as the method.

True

When a variable is declared in the initialization expression of a for loop, the scope of the variable is limited to the loop itself.

True

When arrays of primitive numeric types are allocated, C# automatically initializes each element to zero.

True

When writing a method, you have to specify the data type for every parameter variable that is declared in the parameter list.

True

Explain how pass by value works when passing primitive types (like int and double) to a method. If changes are made to the passed value in the method, do they get reflected back to the variable in the call?

When a primitive type is passed by value to a method, a copy of the argument's value is created and the copy is sent to the method to initialize the parameter. Any changes made in the method using the parameter will only affect the local copy in the method. No changes will be reflected back through the argument used in the method call.

Explain the difference between definite repetition and indefinite repetition.

With definite repetition, the number of loop iterations is predetermined and known from the start. A loop that counts from 1 to 10 would be definite because it does exactly 10 iterations and this was known when the loop began. However, when the number of loop iterations is not known in advance, we have indefinite repetition. For example, with sentinel control, you want to continue performing some task until the user indicates a desire to stop. In this case, you do not know when you write the program whether the loop will be executed two times or two hundred times.

Write a C# code fragment to accomplish the following: a) Declare an array of double values named data using an initializer list with the following values: 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7. b) Write a loop that displays the values in array data from first number to last number, each on a separate line using the console. Solve using a foreach loop. c) Write a loop that displays the values in array data from last number to first number, each on a separate line using the console. Solve using a for loop. Do not change the order of the data in the array.

a) double [] data = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; b) foreach (double number in data) WriteLine($"{number}"); c) for (int i = data.Length-1; i >= 0; i--) WriteLine($"{data[i]}");

The do-while repetition statement tests the condition ___ the body of the loop executes.

after

You can easily navigate through arrays using a for or while loop that varies a subscript from 0 and up to (and including) ___.

arrayName.Length - 1

When you pass an argument to a method, the argument's data type must be ___ with the receiving parameter's data type.

assignment compatible

When you know you want to perform some task at least one time, the ____ loop is the best choice.

do-while

With a ___ loop, you can indicate the starting value for the loop control variable, the test condition that controls the loop entry, and the expression that alters the loop control variable, all in one convenient place.

for

Write a loop that displays every third number from 3 through 33 to the console, each number on a separate line. You may use any loop that you like to solve.

for (int i = 3; i <= 33; i += 3) WriteLine($"{i}");

Sentinel-controlled repetition is an example of:

indefinite repetition

When loops are nested, each pair contains a(n) ___ loop and an outer loop.

inner

Of the following statements, which one correctly initializes an int array named quarters with the values 1, 2, 3, 4?

int [] quarters = {1,2,3,4}; const int SIZE = 4; int [] quarters = new int [SIZE] {1,2,3,4}; int [] quarters = new int [] {1,2,3,4}; int [] quarters = {1,2,3,4};

An array subscript can be an expression, as long as the expression evaluates to a(n) ____.

integer

One execution of any loop is called a(n) ___.

iteration

A(n) ___ is similar to a rectangular 2-D array, but each row can have a different number of columns.

jagged array

A variable is ____ to a method when it is declared within that method.

local

Memory for an array is typically allocated by using keyword ___.

new

A while loop is a(n) ___ loop.

pretest

Write a method named ConvertYardsToMeters that accepts a double parameter yards and returns a double value. The method should convert the specified number of yards into meters, returning the calculated number of meters. Each yard is equivalent to 0.9144 meters. Make the method public and static so that it may be easily reused.

public static double ConvertYardsToMeters (double yards) { double meters = yards * 0.9144; return meters; }

A value-returning method must have a(n) ___ statement.

return

The fact that the result from a ReadLine() method call can be assigned to a string means that its return type is ___.

string

When you call a ___, it executes the statements that it contains and then it returns a value back to the statement that called it.

value-returning method

When you call a ___, it simply executes the statements it contains and then terminates.

void method


संबंधित स्टडी सेट्स

CH 10 Quiz - Liens & Encumbrances

View Set

Stats Quiz #11 Z-Scores and Normal Distribution

View Set

Unit 3 Fundamentals (Medication Administration Ch.31)

View Set

Sociology: Chapter 10 Multiple Choice

View Set

Microbiology, Ch 23, Nester's 9th

View Set