c++ exam
A block is enclosed inside Quotes Braces Parentheses Brackets
Braces
__________ is the brain of a computer. Hardware Memory Disk CPU
CPU
What is the output of the following code? string s("abcdefg"); cout << s.substr(1, 3); bcd abc a c
bcd
To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159. functions constants variables classes
constants
Which of the following justifies the output to the left? fixed right showpoint left setw(width)
left
The following program displays #include <iostream> using namespace std; int main() { cout << "A"; cout << "B"; return 0; } BA A B AB B A
AB
Suppose char city[7] = "Dallas"; what is the output of the following statement? cout << city; Dallas D nothing printed Dallas0
Dallas
Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; } No Yes
No
A character is stored in __________. two bytes one byte three bytes four bytes
One Byte
The "less than or equal to" comparison operator is __________. != < <= << =<
<=
Which of the following expression will yield 0.5? (float)(1 / 2) 1.0 / 2 (double) (1 / 2) (int) 1 / 2 1 / 2
1.0/2
How many times the following code prints "Welcome to C++"? int count = 0; do { cout << "Welcome to C++"; count++; } while (count < 10); 11 10 9 8 0
10
How many times the following code prints "Welcome to C++"? int count = 0; do { cout << "Welcome to C++"; } while (++count < 10); 9 11 10 8 0
10
How many times the following code prints "Welcome to C++"? int count = 0; while (count < 10) { cout << "Welcome to C++"; count++; } 0 10 11 8 9
10
Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10). 11 9 10
10
What is the value in count after the following loop is executed? int count = 0; do { cout << "Welcome to C++"; } while (count++ < 9); cout << count; 8 0 9 11 10
10
What is the output of the following code: #include <iostream> using namespace std; int main() { int x[] = {120, 200, 16}; for (int i = 0; i < 3; i++) cout << x[i] << " "; } 120 200 16 16 120 200 200 120 16 16 200 120
120 200 16
Suppose void nPrint(char ch, int n) { while (n > 0) { cout << ch; n--; } } What is k after invoking nPrint('a', k)? int k = 2; nPrint('a', k); 0 1 2 3
2
What is the output of the following code? #include <iostream> using namespace std; int main() { int matrix[4][4] = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; int sum = 0; for (int i = 0; i < 4; i++) cout << matrix[i][1] << " "; return 0; } 2 5 9 13 4 5 6 7 1 3 8 12 1 2 3 4 3 6 10 14
2 5 9 13
If you declare an array double list[] = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. undefined 5.5 3.4 3.4 2.0
2.0
What is Math.floor(3.6)? 2 4 5.0 3
3
How many elements are in array double list[5]? 5 0 4 6
5
What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5); 6 5 7 8
6
Note that the ASCII for character A is 65. The expression 'A' + 1 evaluates to ________. Illegal expression B 66 A1
66
The ASCII of 'a' is 97. What is the ASCII for 'c'? 96 97 99 98
99
Analyze the following code. #include <iostream> using namespace std; class B { public: B() { }; int k; }; int main() { B b; cout << b.k << endl; return 0; } The program has a runtime error because b.k does not have a value. The program displays unpredictable number. The program displays 1. The program displays 0. The program has a compile error because b.k cannot be accessed.
The program displays unpredictable number.
There are no default value for data fields in a class.
True
Suppose two header files t1.h and t2.h contain the declarations for class T. What happens if you include both header files in your program? The program will compile fine and the first header file that is included is used. The compile will automatically decides which implementation to use. You will get multiple declaration error if the header files don't have the include guard. The program will not compile at all.
You will get multiple declaration error if the header files don't have the include guard.
What is the printout of the following switch statement? char ch = 'a'; switch (ch) { case 'a': case 'A': cout << ch << endl; break; case 'b': case 'B': cout << ch << endl; break; case 'c': case 'C': cout << ch << endl; break; case 'd': case 'D': cout << ch << endl; } ab abcd a aa
a
Suppose void nPrint(char ch, int n) { while (n > 0) { cout << ch; n--; } } What is the printout of the call nPrint('a', 4)? aaa invalid call aaaa aaaaa
aaaa
What is the output of the following code? string s("abc"); s.append("welcome", 3); cout << s << endl; abcwelcome abcwel abc welcomeabc
abcwel
What is the output of the following code? string s("abc"); s.append("welcome"); cout << s << endl; welcomeabc welcome abc abcwelcome
abcwelcome
A function that is associated with an individual object is called __________. a static function an object function a class function an instance function
an instance function
What is the output of the following code? string s("abcdefg"); s.insert(1, 3, 'w'); cout << s << endl; abcdefg awwwbcdefg aweldefg awelbcdefg
awwwbcdefg
Why do computers use zeros and ones? because binary numbers are the bases upon which all other number systems are built. because digital devices have two stable states and it is natural to use one state for 0 and the other for 1. because combinations of zeros and ones can represent any numbers and characters. because binary numbers are simplest.
because digital devices have two stable states and it is natural to use one state for 0 and the other for 1.
Which of the following statements are correct? char charArray[][] = {'a', 'b'}; char charArray[2][] = {{'a', 'b'}, {'c', 'd'}}; char charArray[2][2] = {{'a', 'b'}, {'c', 'd'}}; char charArray[][] = {{'a', 'b'}, {'c', 'd'}};
char charArray[2][2] = {{'a', 'b'}, {'c', 'd'}};
Which of the following statement prints smith\exam1\test.txt? cout << "smith\\exam1\\test.txt"; cout << "smith\exam1\test.txt"; cout << "smith"\exam1"\test.txt"; cout << "smith\"exam1\"test.txt";
cout << "smith\\exam1\\test.txt";
The signature of the main function in C++ is __________. Main(String[] args) int main() Main(String args[]) main(String[] args) void main(String[] args)
int main()
The following code displays ______________. #include <iostream> using namespace std; void maxValue(int value1, int value2, int max) { if (value1 > value2) max = value1; else max = value2; } int main() { int max = 0; maxValue(1, 2, max); cout << "max is " << max << endl; return 0; } max is 2 max is 0 max is 1 max is
max is 0
Given the following two arrays: char s1[] = {'a', 'b', 'c'}; char s2[] = "abc"; Which of the following statements is correct? s1 has two characters s1 has four characters s2 has four characters s2 has three characters
s2 has four characters
Variables that are shared by every instances of a class are __________. instance variables static variables private variables public variables
static variables
When you pass an array to a function, the function receives __________. a copy of the array a copy of the first element the length of the array the reference of the array
the reference of the array
To return an uppercase letter from char variable ch, use islower(ch) isdigit(ch) isalpha(ch) toupper(ch) tolower(ch)
toupper(ch)
What is the output of the following code? string s("abc"); s.assign("welcome", 3); cout << s << endl; abc wel abcwww welcome abcwelcome
wel
To assign a value 1 to variable x, you write x == 1; x := 1; 1 := x; x = 1; 1 = x;
x = 1;
To add a value 1 to variable x, you write x := 1; x = 1++ x; x ++= 1; 1 + x = x; x = x + 1;
x = x + 1;
What is the output of the following code? #include <iostream> using namespace std; void f(double &p) { p += 2; } int main() { double x = 1; double y = 1; f(x); f(y); cout << "x is " << x; cout << " y is " << y << endl; return 0; } x is 1 y is 2 x is 2 y is 1 x is 3 y is 3 x is 2 y is 2 x is 1 y is 1
x is 3 y is 3
Does the return statement in the following function cause syntax errors? void f() { int max = 0; if (max != 0) cout << max; else return; } Yes No
No
The extension name of a C++ source code file is .class .exe .obj .java .cpp
.cpp
Suppose x is 1. What is x after x -= 1? -1 -2 1 2 0
0
What is the output of the following code? inline void print(int i) { cout << i << endl; } int main() { print(1); return 0; } 0 2 1 nothing
1
What is the output of the following code? void f() { cout << 1 << endl; } int main() { f(); return 0; } 0 1 1 0 1 0 nothing
1
Suppose circle1 and circle2 are two Circle objects. What does the following statement do? circle2 = circle1; It makes circle2 and circle1 the same object. It copies the contents of circle2 to circle1. This statement is illegal. It copies the contents of circle1 to circle2.
It copies the contents of circle1 to circle2.
Are the following two declarations the same char city[8] = "Dallas"; char city[] = "Dallas";
No
Does the function call in the following function cause syntax errors? #include <iostream> #include <math> using namespace std; int main() { pow(2.0, 4); } Yes No
No
Analyze the following code: #include <iostream> using namespace std; void reverse(int list[], const int size, int newList[]) { for (int i = 0; i < size; i++) newList[i] = list[size - 1 - i]; } int main() { int list[] = {1, 2, 3, 4, 5}; int newList[5]; reverse(list, 5, newList); for (int i = 0; i < 5; i++) cout << newList[i] << " "; } The program displays 5 4 3 2 1. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. The program displays 1 2 3 4 6. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
The program displays 5 4 3 2 1.
Analyze the following code: #include <iostream> using namespace std; int xfunction(int n, long t) { cout << "int"; return n; } long xfunction(long n) { cout << "long"; return n; } int main() { cout << xfunction(5); } The program displays int followed by 5. The program does not compile because the compiler cannot distinguish which xfunction to invoke. The program displays long followed by 5. The program runs fine but displays nothing.
The program displays long followed by 5.
What is the output of the following code? bool even = false; cout << (even ? "true" : "false") << endl; true true false false nothing
false
Programming style is important, because ______________. good programming style can make a program run faster good programming style makes a program more readable a program may not compile if it has a bad style good programming style eliminates programming errors
good programming style makes a program more readable
What will be the output of the following code? #include <iostream> using namespace std; int j = 1; int main() { int i = 2; cout << "i is " << i << " j is " << j << endl; } i is 2 j is 1 i is 1 j is 2 i is 2 j is 2 i is 1 j is 1
i is 2 j is 1
Suppose the input for number is 9. What is the output from running the following program? #include <iostream> using namespace std; int main() { cout << "Enter an integer: "; int number; cin >> number; int i; bool isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } cout << "i is " << i << endl; if (isPrime) cout << number << " is prime" << endl; else cout << number << " is not prime" << endl; return 0; } i is 3 followed by 9 is not prime i is 3 followed by 9 is prime i is 4 followed by 9 is prime i is 4 followed by 9 is not prime
i is 4 followed by 9 is not prime
Which of the following code displays the area of a circle if the radius is positive. if (radius != 0) cout << radius * radius * 3.14159; if (radius >= 0) cout << radius * radius * 3.14159; if (radius <= 0) cout << radius * radius * 3.14159; if (radius > 0) cout << radius * radius * 3.14159;
if (radius > 0) cout << radius * radius * 3.14159;
Which of the following function declaration is correct? int f(int[][] a, int rowSize, int columnSize); int f(int a[][3], int rowSize); int f(int a[][], int rowSize, int columnSize); int f(int a[3][], int rowSize);
int f(int a[][3], int rowSize);
Which of the following statements is valid? int i = {3, 4, 3, 2}; double [30]; int[] i = {3, 4, 3, 2}; int i(30); int i[4] = {3, 4, 3, 2};
int i[4] = {3, 4, 3, 2};