CSCI 40
What is the output of the following code? int w = 98; int x = 99; int y = 0; int z = 1; if (x >= 99){ if (x < 99){ cout << y << endl; } else { cout << z << endl; } } else { if (x==99){ cout << x << endl; } else { cout << w << endl; } } 98 99 0 1
1
What is the output of the following statement? cout << 4*(15/(1+3)) << endl; 15 12 63 72 none of these
12
How many times will the following loop display "Hello!"? for (i=1; i<20; ++i){ cout << "Hello!" << endl; } 20 19 21 An infinite number of times
19
What will the value of x be after the following statements execute? int x; x = 18 % 4; 0.45 4 2 unknown
2
How many times will the following loop display "Hello!"? for (i=0; i<20; ++i){ cout << "Hello!" << endl; } 20 19 21 An infinite number of times
20
What will the following code output? int number = 22; int *var = &number; cout << *var << endl; The address of the number variable 22 An asterisk followed by 22 An asterisk followed by the address of the number variable
22
Which one of the following would be an illegal variable name? 3dGraph dayOfWeek _employee_num June1997 itemsorderedforthemonth
3dGraph
Which value can be entered to cause the following code segment to display the message: "That number is acceptable." int number; cin >> number; if (number > 10 && number < 100) { cout << "That number is acceptable" << endl; } else { cout << "That number is not acceptable" << endl; } 100 10 99 0 All of these
99
Algorithm
A set of well-defined steps for performing a task or solving a problem
Which of the following loop is correct for reversing a vector? A. for (i = 0; i < revVctr.size()/2; ++i) { temp = revVctr.at(i); revVctr.at(i) = revVctr.at(revVctr.size() - 1 - i); revVctr.at(revVctr.size() - 1 - i) = temp; } B. for (i = 0; i < revVctr.size(); ++i) { temp = revVctr.at(i); revVctr.at(i) = revVctr.at(revVctr.size() - 1 - i); revVctr.at(revVctr.size() - 1 - i) = temp; } C. for (i = 0; i < revVctr.size(); ++i) { revVctr.at(i) = revVctr.at(revVctr.size() - i); } D.
A. for (i = 0; i < revVctr.size()/2; ++i) { temp = revVctr.at(i); revVctr.at(i) = revVctr.at(revVctr.size() - 1 - i); revVctr.at(revVctr.size() - 1 - i) = temp; }
What does the following code do with a vector "v" that has N double elements? double temp = v.at(0); v.at(0) = v.at(N-1); v.at(N-1) = temp; A. Swap the first and last elements of vector "v" B. Add an additional element into vector "v" C. Compare the first and last elements of vector "v" D. Remove an existing element into vector "v"
A. Swap the first and last elements of vector "v"
for (i = 0; ;++i) { cout<< daysList.at(i)<< endl; } Complete the code to print all items for the given vector: vector<int> daysList(365); A. i < 365 B. i <= 364 C. i <= 365 D. i < 364
A. i < 365
Complete the for loop to achieve the goal. Iterate for i from 0 to 30 by 5s (0, 5, 10, 15, ...). for (i=0; i<=30; ) { // Loop body } A. i=i+5 B. i=i*5 C. i=i-5 D. i=i/5
A. i=i+5
Given the declarations class BrandInfo { public: string company; string model; }; class DiskType { public: BrandInfo brand; float capacity; }; DiskType myDisk; Which of the following assignment statements is valid? A. myDisk.capacity = 1.44; B. myDisk.BrandInfo = "Memorex"; C. myDisk.brand = "Memorex"; D. a and c above E. none of the above
A. myDisk.capacity = 1.44
Which correctly declares a function named "CalcVal" that takes two integer parameters x and y? The function does not return any value. A. void CalcVal(int x, int y); B. void CalcVal(int x, y); C. void CalcVal(int x; int y); D. int CalcVal(int x, int y);
A. void CalcVal(int x, int y);
How many times will the loop body execute? Assume user would enter 'a', then 'b', then 'n'. // Get userChar from user here while (userChar != 'n') { // Do something //Get userChar from user here } A. 2 B. 3 C. 0 D. 1
A. 2
what is the output of the following program fragment? age = 29; cout << "Are you" << age << "years old?" << endl; A. Are you29years old? B. Are you 29years old? C. Are you 29 years old? D. Are you age years old? E. Are you29 years old?
A. Are you29years old?
Compute the area of a triangle -Determine the base -Determine the height -Compute base times height -_________________________________ A. Divide the previous answer by 2 B. Divide the previous answer by 1/3 C. divide the previous answer by 3 D. Divide the previous answer by 1/2
A. Divide the previous answer by 2
Which of the following statement is required for calling function "sqrt()"? A. #include <iostream> B. #include <cmath> C. #include <library> D. #include <string>
B. #include <cmath>
Given numPeople = 10, numCars = 2, userKey = 'q'. Which choice is false? A. !(userKey == 'a') B. (numPeople >= 10) && (numCars > 2) C. (numPeople >= 20) || (numCars > 1) D. userKey != 'a'
B. (numPeople >= 10) && (numCars > 2)
What is the result of this code? for (i = 0; i < itemsLast.size(); ++i) { itemsList.at(i) = itemsList.at(i+1); } A. -55, 0, 9, 0 B. Error (program aborts) C. 0, -55, -1, 0 D. -1, 0, 9, 0
B. Error (program aborts)
Which of the following prints out the data from a pointer/memory address, given the following code? double x = 0.0; double* p = &x; A. cout << &p << endl; B. cout << *p << endl; C. cout << p.at(0) << endl; D. cout << p << endl;
B. cout << *p << endl;
Find the correct statement to complete the code that finds the minimum of all elements in a vector: tempVal = valsVctr.at(0); for (i=0; i < NUM_VALS;++i){ if (valsVctr.at(i) < ) { tempVal = valsVctr.at(i); } } A. valsVctr.at(i+1) B. tempVal C. valsVctr.at(i-1) D.
B. tempVal
Which of the following choice represents a correct logical expression (right part after "=>") of the described condition (left part before "=>")? A. userNum is between 10 and 20, inclusive => (userNum >= 10)&(userNum <= 20)) B. userNum is neither 5 nor 10 => !( (userNum == 5) || (userNum == 10) ) C. userNum is not greater than 100 => (userNum !> 100) D. userNum is less than -5 or greater than 10 =>(userNum < -5) && (userNum > 10)
B. userNum is neither 5 nor 10 => !( (userNum == 5) || (userNum == 10) )
Define an int vector testVctr with 50 elements. A. vector<int> testVctr(49); B. vector<int> testVctr(50); C. vector<int> testVctr; D. vector<int> testVctr[50];
B. vector<int> testVctr(50);
Which of the following is the correct declaration for pass by reference for a function that converts a given time to hours and mins? A. void ConvHrMin (int timeVal, int hrVal, int minVal); B. void ConvHrMin (int timeVal, int& hrVal, int& minVal); C. void ConvHrMin & (int timeVal, int hrVal, int minVal); D.
B. void ConvHrMin (int timeVal, int& hrVal, int& minVal);
What is the value of an integer variable "y" after the following statement? y = 44/33; A. 2 B. 1 C. 1 and 1/3 D. 1.333
B. 1
Assume variable int numStudents = 12 is at memory address 99, and variable int* myPtr is at address 44. What does cout << &numStudents; output? A. 44 B. 99 C. 12 D. error
B. 99
Which choice is correct according to code like this: if (csci40Score <= 54) { // 1st branch range: _______ ... } else if (csci40Score <= 69) { // 2nd branch range: _______ ... } else if (csci40Score <= 84) { // 3rd branch range: _______ ... } else { // 4th branch range: _______ ... } Note that [a, b] means a range that includes both value "a" and "b". A. 3rd branch range is [69, 84]; B. 4th branch range is from 84 to any number bigger than 84(include 84); C. 1st branch range is [0, 54]; D. 2nd branch range is [54, 69];
C. 1st branch range is [0, 54];
Given a function definition: void CalcVal(int a, int b, int c) and given int variables i, j, and k, which are valid syntax for the call CalcVal(...)? A. cout << void CalcVal(i,j,k) << endl; B. CalcVal(k,i+j+99) C. CalcVal(k,i+j,99) D. CalcVal1(i,j,k)
C. CalcVal(k,i+j,99)
Given a function that returns the bigger value of two integers as follows: int max(int x, int y). Which of the following is the incorrect syntax for getting the maximum of three variables a, b, and c? A. max(max(a,b),max(b,c)) B. max(a, max(b,c)) C. max(a,b,c) D. max(max(a,b),c)
C. max(a,b,c)
Which of the following is the correct syntax for accessing a "public" member variable "GetDist()" of an object "runnerInfo" of class "RunnerInfo"? A. RunnerInfo.GetDist() B. runnerInfo.GetDist C. runnerInfo.GetDist() D. There is no way to do this directly
C. runnerInfo.GetDist()
Given a "pointer sp" to an object of class "Student" with a public member function "GetName()", which of the following is the correct statement to access the member function? A. Cannot access at all B. sp.GetName(); C. sp->GetName(); D. sp GetName();
C. sp->GetName();
The size of vector "vctr" can be set or changed while a program is executing using: A. vctr.(size N) B. vctr.(resize N) C. vctr.resize(N) D. vctr.size(N)
C. vctr.resize(N)
Which is invalid Code? A. // Print "Hello" to the screen // B. // numKids = 2; // Typical number C. / Get user input/ D. // Get user input
C. /Get user input/
How many times will the loop body execute? Assume user would enter 'n', then 'n', then 'y'. // Get userChar from user here while (userChar != 'n') { // Do something // Get userChar from user here } A. 1 B. 2 C. 0 D. 3
C. 0
According to code like this, when userChar = 'C', which value of variable "encodedVal" is correct? switch (userChar) { case 'A': encodedVal = 1; break; case 'B': encodedVal = 2; break; case 'C': case 'D': encodedVal = 4; break; case 'E': encodedVal = 5; case 'F': encodedVal = 6; break; default: encodedVal = -1; break; } A. 2 B. 5 C. 4 D. -1
C. 4
If itemPrices has elements 45, 22, 38, then after "pop_back" function, what does "itemPrices.at(2)" return? Type error if appropriate. A. 45 B. 22 C. error D. 38
C. error
what yields 3.4? A. static_cast<int>(17)/static_cast<int>(5) B. static_cast<double>(17/5) C. static_cast<double>(17)/static_cast<double>(5) D. static_cast<int>(17/5)
C. static_cast<double>(17)/static_cast<double>(5)
Convert the decimal number 34 to an 8-bit binary number A. 00011000 B.00100011 C.00100010 D.00100001
C.00100010
_____ are used to translate each source code instruction into the appropriate machine language instruction.
Compilers
This step will uncover any syntax errors in your program
Compiling
What will the following code output? (For an infinite loop, type "IL") int x = 5; int y = 18; while (y >= x) { cout << y << " "; y = y - x; } A. IL B. 18 13 5 C. 18 13 7 D. 18 13 8
D. 18 13 8
Given the declarations class BrandInfo { public: string company; string model; }; class DiskType { public: BrandInfo brand; float capacity; }; DiskType myDisk; What is the type of myDisk.brand? A. char B. DiskType C. string D. BrandInfo E. none of the above
D. BrandInfo
Suppose we have a class named "RunnerInfo" defined. What is the correct syntax for creating an object of RunnerInfo named "runnerInfo"? A. int runnerInfo; B. RunnerInfo runnerInfo(); C. vector<int> runnerInfo; D. RunnerInfo runnerInfo;
D. RunnerInfo runnerInfo;
Which of the following is the correct syntax for a function call used to print out the area of a rectangle with sides being values of variables x and y? The function declaration is as follows: int RectArea(int a, int b); A. cout << RectArea(x*y); B. cout << int RectArea(x, y); C. cout << RectArea(int x, int y); D. cout << RectArea(x, y);
D. cout << RectArea(x, y);
Complete the for loop to achieve the goal. Iterate for i from 10 down to 0. Compare with 0. for (i=10; --i ) { // Loop body } A. i>0; B. i<0; C. i<=0; D. i>=0;
D. i>=0;
If itemPrices has element values 45, 48, then after what function, itemPrices' element values are 45,48,38? A. itemPrices.(push_back 38); B. itemPrices.pop_back(38); C. itemPrices.(pop_back 38); D. itemPrices.push_back(38);
D. itemPrices.push_back(38);
Assign the vector's last item's value into the first position. Assume vector scoresList has 10 items. A. scoresList.at(9) = scoresList.at(0) B. scoresList.at(1) = scoresList.at(9) C. scoresList.at(10) = scoresList.at(1) D. scoresList.at(0) = scoresList.at(9)
D. scoresList.at(0) = scoresList.at(9)
Assume that a vector is defined as follows. What is the correct statement if we want to set the first element to 3? vector<int> v; A. v.at(0) = 3; B. v.resize(3); C. v.at(1) = 3; D. v.push_back(3);
D. v.push_back(3);
Which correctly declares a function named "CalcVal" that takes two integer parameters x and y? The function does not return any value. A. void CalcVal(int x; int y); B. int CalcVal(int x, int y); C. void CalcVal(int x, y); D. void CalcVal(int x, int y);
D. void CalcVal(int x, int y);
What is the size of the myVector after the following statements: vector<int> myVector; myVector.push_back(1); myVector.pop_back(); myVector.push_back(10); myVector.back(); A. 2 B. 10 C. 0 D. 1
D. 1
Is this a valid comment? /* numKids = 2; /* Typical number*/ numCars = 5; */
False
What is the output of this code : string x = "Sea"; string y = "Island"; string temp = x; x = y; y = temp; cout << x << ' ' << y << endl; A. Island Island B. Sea Island C. Island Sea D. Sea Sea
Island Sea
What does the following statement do? vector<int> v(10); It creates a vector object and initializes all of its elements to the value 10. It creates a vector object with a starting size of 10. It creates a vector object and initializes the first element with the value 10. It creates a vector object that can store only values of 10 or less.
It creates a vector object with a starting size of 10.
Logic errors
Mistakes that cause a running program to produce incorrect results
what will the following code display? cout << "Monday"; cout << "Tuesday"; cout << "Wednesday"; Monday Tuesday Wednesday Monday Tuesday Wednesday MondayTuesdayWednesday "Monday" "Tuesday" "Wednesday"
MondayTuesdayWednesday
________ algorithms are used to arrange random data into some order. Standard search Linear Sorting Binary search None of these
Sorting
Source code
The statements written by the programmer
Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr; The value stored in the variable whose address is contained in ptr. The string "*ptr". The address of the variable stored in ptr. The address of the variable whose address is stored in ptr. None of these
The value stored in the variable whose address is contained in ptr.
A pointer variable is designed to store ________. any legal C++ value only floating-point values a memory address an integer None of these
a memory address
Every byte in the computer's memory is assigned a unique ________. pointer address dynamic allocation name None of these
address
When this is placed in front of a variable name, it returns the address of that variable. asterisk ( * ) conditional operator ampersand ( & ) semicolon ( ; ) None of these
ampersand ( & )
The following statement: int* ptr = new int; results in a compiler error assigns an integer less than 32767 to the variable named ptr assigns an address to the variable named ptr creates a new pointer named int None of these
assigns an address to the variable named ptr
This statement causes a loop to terminate early. Selected Answer: stop break null terminate None of these
break
Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression break exit switch scope None of these
break
This is a variable that is regularly incremented or decremented each time a loop iterates. Selected Answer: constant counter control statement null terminator None of these
counter
Which statement prints: Welcome!
cout << "Welcome!";
Which statement starts a new output line?
cout << endl;
Members of a class object are accessed with the ________. dot operator cin object extraction operator stream insertion operator None of these
dot operator
which statement will read an entire line of input into the following string object? string address; cin << address; cin address; getline(cin, address); cin.get(address); none of these
getline(cin, address)
Which statement allows you to properly check the char variable code to determine whether it is equal to a "C" and then output "This is a check" and then advance to a new line? if code is equal to C { cout << "This is a check\n"; } if (code = "C") { cout << "This is a check" << endl; } if (code == 'C') { cout << "This is a check\n"; } if (code == C) { cout << "This is a check" << endl; }
if (code == 'C') { cout << "This is a check\n"; }
Assume that myCar is an instance of the Car class, and that the Car class has a member function named accelerate. Which of the following is a valid call to the accelerate member function? Car->accelerate(); myCar::accelerate(); myCar.accelerate(); myCar:accelerate();
myCar.accelerate();
Assume that a program has the following string object definition: string name; Which of the following statements correctly assigns a string literal to the string object? name = Jane; name = "Jane"; name = 'Jane'; name = (Jane);
name = "Jane";
This type of member function may be called only from a function that is a member of the same class. public private global local None of these
private
This type of member function may be called from a statement outside the class. public private undeclared global None of these
public
This vector function returns the number of elements in a vector. size num_elements elements length
size
Which statement correctly defines a vector object for holding integers? Selected Answer: vector v<int>; int vector v; int<vector> v; vector<int> v;
vector<int> v;
Dynamic memory allocation occurs ________. when a new variable is created by the compiler when a new variable is created at runtime when a pointer fails to dereference the right variable when a pointer is assigned an incorrect address None of these
when a new variable is created at runtime
Which of the following expressions will determine whether x is less than or equal to y? x > y x =< y x <= y x >= y
x <= y