C++ Quiz 4
What will the following program display? int a=0, b=2,x=4,y=0; cout<< (a==b)<< " "; cout << (a !=b)<< " "; cout << (b<=x)<< " "; cout << (y>a)<<endl; Select one: a. 0 1 1 0 b. 0 0 1 0 c. 1 1 0 1 d. 1 0 0 1 e. None of these
a. ( 0 1 1 0 )
What is the value of donuts after the following code executes? int donuts=10; if (donuts=1) donuts=0; else donuts+=2; Select one: a. 0 b. 12 c. 1 d. 10
a. (0)
This operator represents the logical AND. Select one: a. ++ b. || c. && d. @ e. None of these
c. (&& )
Assuming x is 5, y is 6, and z is 8, which of the following is false? 1) x==5; 2) 7 <=(x+2); 3) z < = 4; 4) (1+x) != y; 5) z>=8; 6) x>=0; 7) x<=(y*2); Select one: a. 3, 4, 6, 7 are false. b. Only 5 is false. c. 3 and 4 are false. d. All are false. e. None of these.
c. (3 and 4 are false. )
What will the following segment of code output? score=40; if (score>95) cout <<"Congratulations!\n"; cout <<"That's a high score!\n"; cout <<"This is a test question!"<<endl; Select one: a. This is a test question! b. Congratulations! That's a high score! This is a test question! c. That's a high score! This is a test question! d. Congratulations! That's a high score! e. None of these
c. (That's a high score! This is a test question!)
Relational operators allow you to ________ numbers. Select one: a. add b. multiply c. compare d. average e. None of these
c. (compare )
This operator takes an operand and reverses its truth or falsehood. Select one: a. || b. relational c. arithmetic d. ! e. None of these
d. (! )
True/False: The default section is required in a switch statement. Select one: True False
f (False)
Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression. Select one: a. break b. exit c. switch d. scope e. None of these
a. ( break )
Which line in the following program will cause a compiler error? 1) #include <iostream> 2) using namespace std; 3) 4) int main() 5) { 6) int number = 5; 7) 8) if (number >= 0 && <=100) 9) cout << "passed.\n"; 10) else 11) cout << "failed.\n"; 12) return 0; 13) } Select one: a. 10 b. 8 c. 9 d. 6
b. (8)
When a program lets the user know that an invalid choice has been made, this is known as: Select one: a. input validation b. output correction c. compiler criticism d. output validation e. None of these
a.( input validation)
This is a variable, usually a bool or an int, that signals when a condition exists. Select one: a. relational operator b. arithmetic operator c. flag d. float e. None of these
c. (flag )
What will the following segment of code output if the value 11 is entered at the keyboard? int number; cin>> number; if (number > 0 ) cout<<"C++"; else cout<< "Soccer"; cout << "Is "; cout << "fun"<< endl; Select one: a. C++ b. Soccer is fun c. C++fun d. Soccerfun e.C++ is fun
e.( C++ is fun)
Whereas < is called a relational operator, x < y is called aNo ________. Select one: a. Arithmetic operator b. Relative operator c. Relational expression d. Largeness test e. None of these
c. (Relational expression)
After execution of the following code, what will be the value of input_value if the value 0 is entered at the keyboard at run time? cin>> input_value; if (input_value >5) input_value = input_value+5; else if (input_value > 2) input_value= input_value+10; else input_value= input_value + 15; Select one: a. 0 b. 15 c. 25 d. 10 e. 5
b. (15 )
Given that, x = 2, y = 1, and z = 0, what will the following cout statement display? cout<< "answer = " << (x || !y && z)<< endl; Select one: a. answer = 0 b. answer = 1 c. answer = 2 d. None of these
b.( answer = 1)
This operator is used in C++ to represent equality. Select one: a. = b. >< c. !! d. == e. None of these
d. (== )
This operator is known as the logical OR operator. Select one: a. -- b. // c. # d. || e. None of these
d. (|| )
What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a= x >= y; Select one: a. 1 b. The string "x >= y" c. 0 d. 10 e. 7
a. (1 )
What will be the output of the following code segment after the user enters 0 at the keyboard? int x=-1; cout <<"Enter a 0 or a 1 from the keyboard: "; cin >>x; if (x) cout << "true"<<endl; else cout <<"False"<<endl; Select one: a. false b. x c. Nothing will be displayed. d. true
a. (false)
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; } Select one: a. 99 b. 1 c. 98 d. 0
b. (1)
What will be the value of result after the following code has been executed? int a= 60; int b=15; int result=10; if (a = b) result *=2; Select one: a. This code will not compile. b. 20 c. 120 d. 10
b. (20)
These operators connect two or more relational expressions into one, or reverse the logic of an expression. Select one: a. relational b. logical c. irrational d. negation e. None of these
b.( logical)
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? Select one: a. if code is equal to C cout<<"This is a check\n"<<endl; b. if (code="C") cout <<"This is a check"<<endl; c. if (code=='C') cout<< "This is a check\n"; d. if (code ==C) cout << "This is a check"<< endl;
c. (if (code=='C') cout<< "This is a check\n";)
What is the output of the following segment of code if the value 4 is input by the user when asked to enter a number? int num; int total=0; cout << "Enter a number from 1 to 10: "; cin >>num; 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; } Select one: a. 0 b. 3 c. 13 d. 28 e. None of these
c. (13 )
What will the following segment of code output? Assume the user enters a grade of 90 from the keyboard. cout<< "Enter the test score: "; cin >> test_score; if (test_score<60); cout << "You failed the test!" << endl; If (test_score>60) cout <<"You passed the test!"<<endl; else cout <<"You need to study for the next test!"; Select one: a. You failed the test! b. You passed the test! c. You failed the test! You passed the test! d. You failed the test! You did poorly on the test! e. None of these
c. (You failed the test! You passed the test!)
The ________ of a variable is limited to the block in which it is declared. Select one: a. precedence b. associativity c. scope d. branching ability e. None of these
c. (scope )
The default section of a switch statement performs a similar task as the ________ portion of an if/else if statement. Select one: a. conditional b. break c. trailing else d. All of these e. None of these
c. (trailing else)
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. \n"; else cout <<"That number is not acceptable.\n"; Select one: a. 100 b. 10 c. 99 d. 0 e. All of these
c.(99)
Given the following code segment, what is output after "result = "? int x=1, y=1, z=1; y=y+z; x=x+ y; cout<<"result = " << (x<y? y : x) << endl; Select one: a. 0 b. 1 c. 2 d. 3 e. None of these
d. (3)
What is the output of the following code segment? int x= 5; if(x=2) cout<< "This is true!"<<endl; else cout<<"This is false!"<<endl; cout<<"This is all folks!"<< endl; Select one: a. This is true! b. This is false! Incorrect c. This is true! This is false! d. This is true! This is all folks! e. None of these
d. (This is true! This is all folks!)
If you intend to place a block of statements within an if statement, you must place these around the block. Select one: a. Parentheses egg b. Square brackets [ ] c. Angle brackets < > d. Curly braces { } e. None of these
d.( Curly braces { } )
True/False: An expression that has any value other than 0 is considered true by an if statement. Select one: True False
t (True)
True/False: You should be careful when using the equality operator to compare floating point values because of potential round-off errors. Select one: True False
t (True)
This statement uses the value of a variable or expression to determine where the program will branch to. Select one: a. switch b. select c. associative d. scope e. None of these
a. (switch)
If you place a semicolon after the statement: if (x>y) Select one: a. The code will not compile. b. The compiler will interpret the semicolon as a null statement. c. The if statement will always evaluate to false. d. All of these e. None of these
b. (The compiler will interpret the semicolon as a null statement. )
In C++ the = operator indicates: Select one: a. equality b. assignment c. subtraction d. negation e. None of these
b. (assignment )
This operator performs a logical NOT operation. Select one: a. -- b. ! c. <> d. >< e. None of these
b. (b. ! )
Which of the following expressions will determine whether x is less than or equal to y? Select one: a. x > y b. x <= y c. x =< y d. x >= y
b. (x <= y)
What is the value of the following expression? false || true Select one: a. +1 b. true c. false d. -1
b.( true)
When an if statement is placed within the conditionally-executed code of another if statement, this is known as: Select one: a. complexity b. overloading c. nesting d. validation e. None of these
c. (nesting )
What is the value of the following expression? true && true Select one: a. +1 b. -1 c. true d. false
c.(true)
Input values should always be checked for: Select one: a. Appropriate range b. Reasonableness c. Division by zero, if division is taking place d. All of these e. None of these
d. (All of these)
True/False: Both of the following if statements perform the same operation. if (sales > 10000) commissionRate= 0.15; if (sales > 10000) commissionRate=0.15; (same line) Select one: True False
t (true )
True/False: If the sub-expression on the left side of the || operator is true, the expression on the right side will not be checked. Select one: True False
t (true)