Ch 1 - 7 Quizzes

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The name of an array is _________ of the first array element A) memory address B) value C) element number D) None of these

A) memory address

Which statement is equivalent to the following? number /= 2; A) number = number / 2; B) number / 2; C) number = 2/number; D) None of these

A) number = number / 2;

Consider this code. Which of the following answer choices will result in the same value of x? double x; x = 10.9 / static_cast<int>(4.5 + 6.4); A) double x; x = 10.9 / 10; B) double x; x = 1.0; C) double x; x = 10.9 / (static_cast<int>(4.5) + 6.4); D) None of these

A) double x; x = 10.9 / 10;

What is the value of res at line 21? #include <iostream> using namespace std; bool isEven (int num) { if (num % 2 == 0) return true; else return false; } int printParity (int var) { if (isEven (var)) return -1; else return 1; } int main() { int res; res = printParity(5); cout << "Exiting"; return 0; } A) 1 B) -1 C) Error D) true

A) 1

How many times will the following loop display "Hello!"? for (int i = 0; i < 500; i++) cout << "Hello!" << endl; A) 500 B) 499 C) 501 D) An infinite number of times

A) 500

What is printed at the last iteration of the outer loop and second iteration of the inner loop? int row, col; for (row = 1; row <= 50; row++) // outer for (col = 1; col <= 3; col++) // inner cout << row + col; A) 52 B) 51 C) 53 D) None of these

A) 52

What will the following segment of code output if the value 100 is entered at the keyboard? int number; cin >> number; if (number > 0) cout << "C++"; else cout << "Soccer"; cout << " is "; cout << "fun" << endl; A) C++ is fun B) Soccer is fun C) C++ D) C++fun

A) C++ is fun - No braces after else, don't rely on indentations

What does this statement do? cout << 15 % 2.0; A) Error B) print 1 c) 1.0 D) 0.3 E) None of these

A) Error - Modulus requires both operands to be integers

These are data items whose value do not change while the program is running. A) Literals B) Variables C) Comments D) Integers E) None of these

A) Literals

What happens with this program? #include <iostream> using namespace std; int main() { int i; for (i = 1; i <= 3; i++) { cout << i << ' '; if ( i == 2) break; } cout << i; return 0; } A) Print 1 2 2 B) Print 1 2 3 C) Print 1 2 3 4 D) Compilation error

A) Print 1 2 2

The purpose of a memory address is: A) To identify the location of a byte in memory B) To prevent multitasking C) To obtain an algorithm D) To improve the effectiveness of high-level languages E) None of these

A) To identify the location of a byte in memory

When typing in your source code into the computer, you must be very careful since C++ is case sensitive. A) True B) False

A) True

The statements in the body of a while loop may never be executed, whereas the statements in the body of a do-while loop will be executed: A) at least once B) at least twice C) as many times as the user wishes D) None of these

A) at least once

Which one defines a valid C-string? A) char fName[] = "UTDallas"; B) char fName[] = {'U', 'T', 'D', 'a', 'l', 'l', 'a', 's'}; C) char fName[8] = {'U', 'T', 'D', 'a', 'l', 'l', 'a', 's'}; D) None of these

A) char fName[] = "UTDallas"; - A valid Cstring is stored as an array of chars ending with a null terminator. Compiler automatically appends a null terminator when it sees a string in double quotes

A function __________ contains the statements that make up the function. A) definition B) prototype C) call D) parameter list

A) definition

What are the values of x, y, and z after the code is executed if the user enters 10, 11, and 12 for x, y, z respectively? int x, y, z; cin >> x >> y >> z; if ( x++ == y) z = ++x; else z = --y; A) x is 11, y is 10, and z is 10 B) x is 11, y is 10, and z is 12 C) x is 11, y is 10, and z is 11 D) Error

A) x is 11, y is 10, and z is 10

If you leave out the size declarator in an array definition: A) you must provide an initialization list B) you are not required to initialize the array elements C) all array elements default to zero values D) your array will contain no elements

A) you must provide an initialization list

What happens with this code? #include <iostream> using namespace std; int main() { int arr1[] = { 5, 10, 15, 20}; for (int i = 0; i < 5; i++) { arr1[i] = i; cout << i << ' '; } return 0; } A)May crash at execution B) Does not compile C) Will always print 0 1 2 3 4 D) None of these

A)May crash at execution - Going out of bounds

x, num1 and num2 are variables. What happens with this expression? x = ++num1 + num2--; A) Compilation error B) num1 is incremented num1 and num2 are added, and the result is assigned to x num2 is decremented C) num1 is incremented num2 is decremented num1 and num2 are added, and the result is assigned to x D) num1 and num2 are added, and the result is assigned to x num1 is incremented num2 is decremented

B) num1 is incremented num1 and num2 are added, and the result is assigned to x num2 is decremented

In the following C++ statement, what operator will be executed first according to the order of precedence? result = 6 - 3 * 2 + 7 - 10 / 2; A) 6 - 3 B) 3 * 2 C) 2 + 7 D) 10 / 2

B) 3 * 2

What does this code print? cout << 15/2; A) 7.5 B) 7 C) It depends; D) None of these

B) 7

Assume arr is an array of integers, and each integer takes 4 bytes. If the address of arr[2] is 1000, what is the address of arr[0]? A) 996 B) 992 C) 998 D) Cannot determine

B) 992 - Address of arr[2] = starting address of array + (2 * 4), so starting address of array is 1000 - 8 = 992. Starting address of array is address of arr[0]

A set of well-defined steps for performing a task or solving a problem is known as a(n): A) Hierarchy B) Algorithm C) Central Processing Unit D) Encoded instruction E) None of the above

B) Algorithm

This code validates that the number entered is 1, 2, 3, or 4. int number; cin >> number; while (number <= 4 || number >= 1) { cout << "Error, please reenter"; cin >> number; } A) True B) False

B) False

Program 1 and program 2 are equivalent Program 1 #include <iostream> using namespace std; int main() { int num = 2, i = 1, j = 2, k = 3; switch (num) { case i: cout << "Hello"; case j: cout << "Hi"; case k: cout << "Bye"; } return 0; } Program 2 #include <iostream> using namespace std; int main() { int num = 2; switch (num) { case 1: cout << "Hello"; case 2: cout << "Hi"; case 3: cout << "Bye"; } return 0; } A) True B) False

B) False - Reason: program 1 does not compile - Expression next to case must be constant int or int literal, cannot be a variable

Words that have a special meaning and may be used only for their intended purpose are known as: A) Programmer Defined Words B) Key Words C) Syntax D) None of the above

B) Key Words

Consider this code. On which line is the header for the showDub function? #include <iostream> using namespace std; void showDub (int); int main() { int x = 2; showDub (x); return 0; } void showDub (int num) { num *= 2; } A) Line 3 B) Line 10 C) There is no such thing as a function header D) Line 7

B) Line 10

What does the program do? #include <iostream> using namespace std; void myFunct (int & num) { num = num/2; cout << num << ' '; } void myFunct2(int num) { num = num * 2; } int main () { int x = 100; myFunct(x); myFunct2(x); cout << x << endl; return 0; } A) Print 100 100 B) Print 50 50 C) Print 100 50 D) Print 50 100

B) Print 50 50 - x is passed by reference to myFunct, x is passed by value to myFunct2

In a C++ program, two slash marks (//) indicate: A) The end of a statement B) The beginning of a comment C) The end of the program D) The beginning of a block of code E) None of these

B) The beginning of a comment

What will the following code display? int number = 7; cout << "The number is " << "number"; A) The number is 7 B) The number is number C) The number is7 D) The number is 0

B) The number is number

Programmer-defined names of memory locations that may hold data are: A) Operators B) Used for variables C) Syntax D) Operands E) None of these

B) Used for variables

Of the following, which is not a valid C++ identifier? A) June1997 B) _employee_number$ C) ___department D) _XXXXXX e) All of these are valid identifiers

B) _employee_number$

When writing functions that accept multi-dimensional arrays as arguments, ___________ must be explicitly stated in the parameter list. A) all dimensions B) all but the first dimension C) the size declarator of the first dimension D) None of these

B) all but the first dimension

Which of the following best describes an operator? A) An operator is a rule that must be followed when constructing a program. B) An operator allows you to perform operations on one or more pieces of data C) An operator marks the beginning or ending of a statement, or is used to separate items in a list D) An operator is a word that has a special meaning E) An operator is a symbolic name that refers to a variable

B) an operator allows you to perform operations on one or more pieces of data

The return statement can return ______ values. A) any number of B) at most one C) no D) a maximum of ten

B) at most one

Here is the header for a function named computeValue: void computeValue(int value) Which of the following is a valid call to the function? A) computeValue(10) B) computeValue(10); C) void computeValue(10); void computeValue(int x);

B) computeValue(10);

Fill in the blanks so the program prints the squares of the array elements #include <iostream> using namespace std; void printArray(int arr[], int s) { for (int i = 0; i < s; i++) cout << arr[i] * arr[i]; } int main() { int myArr[10]; printArray(---, ---); return 0; } A) myArr, 9 B) myArr, 10 C) myArr[], 9 D) myArr[], 10

B) myArr, 10

Assume that a program has the following C++ string class definition: string name; Which of the following statements correctly assigns a string literal to the string? A) name = Jane; B) name = "Jane"; C) name = 'Jane'; D) name = (Jane);

B) name = "Jane";

Consider num on line 3 and x on line 11 #include <iostream> using namespace std; void showDub (int num) { num = 2 * num; cout << num << ' '; } int main() { int x = 2; showDub(x); cout << x << endl; return 0; } A) num and x are parameters B) num is parameter, x is argument C) num and x are arguments D) num is argument , x is parameter

B) num is parameter, x is argument

A function ____________ eliminates the need to place a function definition before all calls to the function. A) header B) prototype C) argument D) parameter

B) prototype

The value of this type of local variable persists value of this type of local variable persists between function calls. A) global B) static C) internal D) dynamic

B) static

To access an array element, use the array name and the element's ________. A) data type B) subscript C) name D) value

B) subscript

When a relational expression is false, it has the value ___________. A) one B) zero C) Not a number D) None of these

B) zero

What is the value of total after this code segment has been executed? int num = 4; int total = 0; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 4: total = total + 3; case 8: total = total +6; default: total = total + 4; } A) 0 B) 3 C) 13 D) 28

C) 13 - break is left out, so program falls through the remaining statements of the switch

Assuming you are using a system with 1-byte characters, how many bytes of memory will the following C-string occupy? "William" A) 7 B) 14 C) 8 D) 1

C) 8 - A C-string always ends with a null terminator

Data types that can hold numbers with a fractional part are A) Integer data types B) Real data types C) Floating point data types D) Long data types E) None of the above

C) Floating point data types

How many times will the loop iterate? int n = 5, a = 0; while (n > 0) a = a + n; n--; A) 5 B) 4 C) None of these D) Compilation error

C) None of these - Will loop forever, missing braces

What happens with this program? #include <iostream> #include <cstdlib> using namespace std; double myDiv(double, double); int main() { double var =1; var = myDiv(5.0, 0.0); cout << var; return 0; } double myDiv(double x, double y) { if (y == 0.0) exit(2); else return x+y; } A) Print 5 B) Print 1 C) Print nothing D) Does not compile

C) Print nothing - exit(2) results in program termination

What happens in this code? #include <iostream> using namespace std; void myFun (int[]); int main() { int arr1[] = {10, 20, 30}; cout << arr1[0] << '0'; myFun (arr1); cout << arr1[2]; return 0; } void myFun(int arr[]) { arr[2] = 0; } A) Error in line 7 B) Error in line 3 C) Prints 10 0 D) Prints 10 30

C) Prints 10 0

Assuming the file test.txt contains 1 2 3 4, and the file test.txt is in the same folder as the program, what happens with this code? #include <iostream> #include <fstream> using namespace std; int main() { ifstream infile; int num; infile.open("test.txt"); while (infile >> num) { infile >> num; cout << num << ' '; } infile.close(); return0; } A) Compilation error B) Prints 1 2 3 4 then closes file and exits C) Prints 2 4 then closes file and exits D) Prints 1 2 3 4 and loops forever

C) Prints 2 4 then closes file and exits

A(n) _________ is a set of unstructions that the computer follows to solve a probelm. A) Compiler B) Linker C) Program D) Operator E) None of the above

C) Program

The statements written by the programmer are called: A) Syntax B) Object code C) Source code D) Runtime libraries E) None of the above

C) Source code

Assuming dataFile is a file stream object, the statement: dataFile.close(); A) is illegal in C++ B) needs a filename argument to execute correctly C) closes a file D) None of these

C) closes a file

Relational operators allow you to _______ numbers. A) add B) multiply C) compare D) None of these

C) compare

To allow file access in a program, you must #include this header file. A) file B) fileaccess C) fstream D) cfile

C) fstream

Which statement will read an entire line of input into the following string object? string address; A) cin >> address; B) getline(address); C) getline(cin, address); D) cin.get(address);

C) getline(cin, address);

In a for statement, this expression is always executed only once. A) test B) update C) initialization D) None of these

C) initialization

Which of the following is a valid C++ array definition? A) int arr[][10]; B) void numbers[5]; C) int arr[10]; D) None of these

C) int arr[10];

This stream manipulator causes the field to be left-justified with padding spaces printed to the right A) left_justify B) left_pad C) left D) None of these

C) left

"myArray" is a two-dimensional array. Which of the following refers to the element in the first row and second column of myArray? A) myArray[1, 2] B) myArray[1][2] C) myArray[0][1] D) myArray[0, 1]

C) myArray[0][1]

What does this code do? int fName[] = {1,2,3}; cout << fName << endl; A) output 123 B) output 1 C) output the address of the fName array D) does not compile

C) output the address of the fName array

A variable's __________ is the part of the program where you can use the variable. A) data types B) value C) scope D) reach E) None of these

C) scope

Given the following declaration , where is the value 77 stored in the scores array? int scores[] = {83, 62, 77, 97}; A) scores[3] B) scores[1] C) scores[2] d) scores[4]

C) scores[2]

Which code prints 66 101? Code 1 int numbers[] = { 99, 87, 66, 55, 101, 90, 120 }; for (int i = 1; i < 5; i++) { cout << numbers[i] << " "; i++; } Code 2 int numbers[] = { 99, 87, 66, 55, 101, 90, 120 }; for (int i = 1; i < 5; i++) { cout << numbers[i] << " "; } Code 3 int numbers[] = { 99, 87, 55, 101, 90, 120 }; for (int i = 1; i < 5; i++) { i++; cout << numbers[i] << " "; } Code 4 int numbers[] = { 99, 87, 55, 101, 90, 120 }; int i = 1; while ( i < 5) { i++; cout << numbers[i] << " "; }

Code 3

What is contained in the variable a after the statement below is executed? Assume x = 10, y = 7, and a is a bool variable. a = x >= y; A) 10 B) 7 C) The string "x >= y" D) 1

D) 1 - the relational operator has higher precedence than the assignment and true is stored as 1

What is the value of val at line 15 if the user enters 3.14? #include <iostream> using namespace std; int main() { double x; int val = 99; cout << "Enter a value from the keyboard: "; cin >> x; if (val = x) val = 10; else val = 100; val = 11; cout << "Exiting"; return 0; } A) 10 B) 3 C) 0 D) 11

D) 11 - There are no braces after else - Do not rely on the indentations

This operator is used in C++ to represent equality. A) = B) >< C) !! D) ==

D) ==

An Integrated Development Environment (IDE) typically allows one to: A) Write a program B) Compile a program C) Debug a program D) All of these E) None of these

D) All of these

What happens with this code? #include <iostream> using namespace std; int main() { for (int i = 1; i <= 3; i++) { cout << i << ' '; if ( i == 2) break; } cout << i; return 0; } A) Prints 1 2 2 B) Prints 1 2 3 C) Prints 1 2 3 3 D) Compilation errorf

D) Compilation error - Last cout is outside the scope of variable i

What is the value of x at line 10? int x; int myInt1 = 10; int myInt2 = 5; x = myInt1 + myInt2 * 'a'; cout << "Exiting"; A) Error B) 10aaaaa C) 10+aaaaa D) None of these

D) None of these - According to the types coercion rules, the char 'a' is automatically converted to an int, whose value is the ASCII code of 'a'

The statement: int grades[ ] = { 100, 90, 99, 80}; shows an example of: A) default arguments B) an illegal array declaration C) an illegal array initialization D) implicit array sizing

D) implicit array sizing

Given this declaration, where is the value 100 stored in the num array? int num[4][3] = { {1,2, 3}, { }, { }, {10, 5, 100} }; A) Nowhere in the array B) num[4][3] C) num[3][3] D) num[3][2]

D) num[3][2]

This is a special value that marks the end of data A) constant B) variable C) loop D) sentinel

D) sentinel

What is the value of res at line 16 if the user enters 3.1416? #inlcude <iostream> #include <string> using namespace std; int main() { double x; int val = 0; string res; cout << "Enter a value from the keyboard: "; cin >> x; if (x) res = "true"; else res = "false"; cout << "Exiting"; return 0; } A) No output, compilation error B) false C) 3.1416 D) true

D) true - x is evaluated as true because 3.1416 is non zero

This operator in C++ allows you to identify how many bytes of storage on your computer system an integer data value requires. A) len B) bytes C) f(x) D) int E) sizeof

E) sizeof

Which of the following choices will be equivalent to this code? Equivalent means right before return 0, the value of myInt is the same in both codes, and the value of myChar is the same in both codes, and the value of myChar is the same in both codes. #include <iostream> using namespace std; int main () { char myChar = '$'; int myInt = 97; myInt = myChar; return 0; } a) #include <iostream> using namespace std; int main() { char myChar = '$'; int myInt = 97; myInt = static_cast<int>(myChar); return 0; } b) #include <iostream> using namespace std; int main() { char myChar = '$'; int myInt = 97; myInt = static_cast(myChar); return 0; } c) There is no equivalent, the code has an error d) #include <iostream> using namespace std; int main() { char myChar = '4'; int myInt = 97; myInt = static_cast(myInt); return 0; }

a) #include <iostream> using namespace std; int main() { char myChar = '$'; int myInt = 97; myInt = static_cast<int>(myChar); return 0; }

Which of the following answer choices will result in the same value of num as this code? double num; num = static_cast<double>(5/2); a) double num; num = 5 / 2; b) double num; num = 2.5; c) double num; num = 5.0 / 2; d) double num; num = 5.0 / 2.0;

a) double num; num = 5 / 2;

Choose the code (code1, code2, code3, or code4) to insert so that after printing the room's width, the program prompts the user to hit Enter, then pauses. The program resumes when the user hits Enter, then prints "Exiting program". #include <iostream> using namespace std; int main () { int w; char myChar; cout << "Enter room's width: "; cin >> w; cout << "Room's width is " << w << endl; // Insert code here cout << "Exiting program"; return 0; } Code 1 cin,get(); cout << "Hit enter to continue: "; cin.get(); Code 2 cout << "Hit enter to continue: "; cin.get(); Code 3 cin.get(); cout << "Hit enter to continue: "; cin >> myChar; Code 4 cout << "Hit enter to continue: "; cin >> myChar; a) Code 1 b) Code 2 c) Code 3 d) Code 4

a) Code 1 - 1st cin.get flushes the buffer, 2nd cin.get reads "Enter"

Consider this code. This scope of num1 includes #include <iostream> using namespace std; void myFunc1 (int); int main() { cout << "Hello world!" << endl; int x = 2; myFunc1(x); return 0; } int num1; void myFunc1 (int num) { int x = 5; num = num * x; } a) Line 12 to line 15 b) line 1 to line 15 c) Line 4 to line 15 d) None of these

a) Line 12 to line 15

The scope of x is defined in line 7 includes #include <iostream> using namespace std; void myFunc1 (int); int main() { cout << "Hello world!" << endl; int x = 2; myFunc1(x); return 0; } int num1; void myFunc1 (int num) { int x = 5; num = num * x; } a) Line 8 to line 0 b) Line 6 to line 15 c) Line 4 to line 9 d) None of these

a) Line 8 to line 0

What happens with this program? #include <iostream> #include <cstdlib> using namespace std; double myDiv(double, double); int main() { double var =1; var = myDiv(5.0, 0.0); cout << var; return 0; } double myDiv(double x, double y) { if (y == 0.0) return 5; else return x+y; } a) Prints 5 b) Prints 1 c) Prints nothing d) Does not compile

a) Prints 5

What happens with this program? #include <iostream> #include <cmath> using namespace std; void Funct1(); void Funct2(); int main() { Funct1(); Funct2(); return 0; } void Funct1() { double num = 10; num = num + 50; } void Funct2() { double num; cout << cos(num); } a) Prints unpredictable value b) Always prints cos(60) c) Error d) None of these

a) Prints unpredictable value - num is an uninitialized local variable in Funct2

Assume input value is valid if and only if it is between 0 and 100 inclusive. Which of the following code correctly determines the input is invalid and print an error message? a) if (input < 0 && input > 100) cout << "Invalid input"; b) if (!(input >= 0 && input <= 100)) cout << "Invalid input"; c) if (input >= 0 || input <= 100) cout << "Invalid input"; d) None of these

b) if (!(input >= 0 && input <= 100)) cout << "Invalid input";

What is the value of x at line 12? #include <iostream> using namespace std; int main() { int d = 2, x = 3; if (d < x) { int d = 10; x = x * d; } x = x + d; cout << "Exiting"; return 0; } a) 8 b) 32 c) Error d) None of these

b) 32 - d at line 9 is 10, d at line 11 is 2

Which of the following codes will generate a pseudo-random number between 6 and 12, inclusive, and assign to myInt? a) myInt = srand() % 6 + 12; b) myInt = rand() % 6 + 12; c) srand(time(0)); myInt = rand() % 6 + 6; d) None of these

d) None of these - Correct answer is rand() % 7 + 6. The [6, 12] range has 7 values and starts at 6

Consider these 3 codes. Which answer is correct, a), b), c), or d)? Code 1 int myInt1 = 7; double myDouble1 = 3.5; myInt1 = mydouble1 = 31.0/2; Code 2 int myInt1 = 7; double myDouble1 = 3.5; myInt1 = myDouble1 = 31/2; myDouble = 31.0/2 Code 3 int myInt1 = 7; double myDouble1 = 3.5; myInt1 = 31/2; myDouble1 31.0/2; a) Only code 1 and code 2 are equivalent b) All the 3 codes are equivalent c) Only code 1 and code 3 are equivalent d) Only code 2 and code 3 are equivalent

b) All the 3 codes are equivalent

What happens with this program? #include <iostream> #include <cmath> using namespace std; void Funct1(); void Funct2(); int main() { Funct1(); Funct2(); return 0; } void Funct1() { double num = 10; num = num + 50; } void Funct2() { cout << co(num); } a) None of these b) Error c) Always prints cos(60) d) Prints nothing

b) Error - Variable num not defined in Funct2

Which is the correct statement to initialize the 2-dimensional array of ints called arr to the values shown in the table? Column 0 | Column 1 Row 0 | 100 | 0 Row 1 | 0 | 2 Row 2 | 0 | 3 a) int arr[][2] = {{100}, {, 2}, {, 3}}; b) int arr[][2] = {{100, 0, 0}, {0, 2, 3}}; c) int arr[][2] = {{100}, {0, 2}, {0, 3}}; d) none of these

c) int arr[][2] = {{100}, {0, 2}, {0, 3}};

Consider the code below. The scope of variable score includes #include <iostream> using namespace std; int main() { int num; cout << "Enter num: "; cin >> num; if (num < 10) { cout << "Enter your score: "; double score; cin >> score; cout << "Your score is " << score; } return 0; } a) lines 10 to 13 b) lines 12 to 15 c) lines 12 to 13 d) lines 5 to 15

c) lines 12 to 13 - Scope begins at variable definition (line 11) and ends at end of block of code (line 14)

Fill in the blank so the program displays 1 3 5. #include <iostream> using namespace std; void myFunct(); int main() { myFunct(); myFunct(); myFunct(); return 0; } void myFunct() { ________ num = 1; cout << num << " "; num = num +2; } a) int b) global int c) static int d) Not possible

c) static int

What happens with this program? #include <iostream> using namespace std; void myFunct1(int); void myFunct2(int); int num = 6; int main() { myFunct1(num); myFunct2(num); cout << num; return 0; } void myFunct1(int val) { int num = 10; num = num * 2; val = val * 2; cout << num; } void myFunct2(int val) { int num = 10; num = num / 2; val = val / 2; cout << num; } a) Error b) Prints 1236 c) Prints 1266 d) None of these

d) None of these - Prints 2056


Kaugnay na mga set ng pag-aaral

Learning Environment - Face to Face & Virtual Learning

View Set