1 / 71

C++ Programming Exam Review Questions

Review questions for C++ programming exam covering topics like loops, conditionals, and switch statements. Test your knowledge with multiple-choice questions.

abalog
Download Presentation

C++ Programming Exam Review Questions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1.  CS 101Second Exam Review  

  2. MC-1 • What would be the output from this code fragment? int x = 3, y = 4, z = 6; if (x < 2) { if (y < 5) cout << "one"; else cout << "two"; } else if (z > 5) { cout << "three"; else cout << "four"; } • a. one • b. two • c. three • d. four

  3. MC-1 • What is the value stored in v1 after the following executes? v1 = 2; v2 = 4; v3 = 3; if (v1 < (v2 – v3)) if ((v2>v1) && (v3>(v2–v1))) {if ( v1 > 0 ) v1++;} else v2++; else v3--; • a. 2 • b. 3 • c. 4 • d. 5

  4. MC-1 • What value is assigned to discount ? double discount; char code = 'C' ; switch ( code ) { case 'A': discount = 0.0; break; case 'B': discount = 0.1; break; case 'C': discount = 0.2; break; default: discount = 0.3; } • a. 0.0 • b. 0.1 • c. 0.2 • d. 0.3

  5. MC-2 • What value is assigned to discount ? double discount; char code = 'C' ; switch ( code ){ case 'A': discount = 0.0; case 'B': discount = 0.1; case 'C': discount = 0.2; default: discount = 0.3; } • a. 0.0 • b. 0.1 • c. 0.2 • d. 0.3

  6. MC-3 • What value is assigned to discount ? double discount; char code = 'X' ; switch ( code ){ case 'A': discount = 0.0; break; case 'B': discount = 0.1; break; case 'C': discount = 0.2; break; default: discount = 0.3; } • a. 0.0 • b. 0.1 • c. 0.2 • d. 0.3

  7. MC-4 • What value is assigned to discount ? double discount; String code = "A" ; switch ( code ){ case 'A': discount = 0.0; break; case 'B': discount = 0.1; break; case 'C': discount = 0.2; break; default: discount = 0.3; } • a. 0.0 • b. 0.1 • c. 0.2 • d. The program will not compile because code is the wrong type.

  8. MC-5 • What value is assigned to discount ? double discount; char code = 'b' ; switch ( code ){ case 'a': case 'A': discount = 0.0; break; case 'b': case 'B': discount = 0.1; break; case 'c': case 'C': discount = 0.2; break; default: discount = 0.3; } • a. 0.0 • b. 0.1 • c. 0.2 • d. The code will not compile because there are missing statements.

  9. MC-5 • Given switch(VAR) { ... }; statement. VAR can not be declared as • a. char • b. int • c. float • d. long int

  10. MC-6 • Rewrite the following statements using a switch statement if( letter == 'X' ) sum = 0; else if ( letter == 'Z' ) valid_flag = 1; else if( letter == 'A' ) sum = 1; else cout << letter;

  11. MC-6 • a. switch( letter ) { • case 'X' : sum = 0; break; • case 'Z' : valid_flag = 1; break; • case 'A' : sum = 1; break; • default : cout << letter; break; • } • b. switch( letter ) { • case 'X' : sum = 0; • case 'Z' : valid_flag = 1; • case 'A' : sum = 1; • default : cout << letter; • } • c. switch( letter ) { • case "X" : sum = 0; break; • case "Z" : valid_flag = 1; break; • case "A" : sum = 1; break; • default : cout << letter; break; • }

  12. MC-6 • Rewrite the following statements using a switch statement if (c == 'X') s = 1; else if (c == 'Z' ) s = 2; else if (c == 'A') s = 3; else cout << "Unknown letter";

  13. MC-6 • a. switch ('c'){ • case 'X' : s = 1; break; • case 'Z' : s = 2; break; • case 'A' : s = 3; break; • default : cout << "Unknown letter”; break; • } • b. switch (c){ • case 'X' : s = 1; • case 'Z' : s = 2; • case 'A' : s = 3; • default : cout << "Unknown letter”; • } • c. switch (c){ • case 'X' : s = 1; break; • case 'Z' : s = 2; break; • case 'A' : s = 3; break; • default : cout << "Unknown letter”; break; • }

  14. MC-1 • What is the output of the following code? int j = 1; while (j < 10) { cout << j << " "; j = j + j%3; } • a. 1 4 7 • b. 1 4 7 10 • c. 1 2 5 8 • d. 1 2 4 5 7 8

  15. MC-2 • What is the output of the following code? int count = 7; while ( count >= 4 ) cout << count--; cout << ”0”; • a. 65430 • b. 76540 • c. 60504030 • d. 70605040

  16. MC-3 • What is the output of the following code? int k = 4; while (k >= 1) cout << k--; • a. 432 • b. 4321 • c. 43210 • d. infinity

  17. MC-4 • What is the output of the following code? int i = 1; while ( i = 5 ) { ++i; cout << i << endl; } • a. 12345 • b. 1234 • c. 123 • d. 66666… for infinity

  18. MC-5 • What is the output of the following code? int i = 1, s = 0; while ( i < 5 ) { s = s + i; i = i + 1; } cout << i << s; • a. 610 • b. 611 • c. 511 • d. 510

  19. MC-6 • What is the output of the following code? int j; for (j = 0; j < 5; ) { cout << j; j++ ; } • a. 0 1 2 3 4 • b. Nothing • c. 1 2 3 4 5 • d. 000000… for infinity.

  20. MC-7 • What is the output of the following code? for(int j = 10; j > 5; j--) cout << j << " "; • a. 10 11 12 13 14 15 • b. 9 8 7 6 5 4 3 2 1 0 • c. 10 9 8 7 6 5 • d. 10 9 8 7 6

  21. MC-8 • What will happen if you try to compile and run the following code? int a =1; for( ; ; ) { if (a > 5) break; cout << a++; } • a. Creates a syntax error • b. Nothing will print • c. Printing out 12345 • d. Infinity loop

  22. MC-9 • What is the output of the following code? for (int i = 1; i <= 5; i++) { if( i > 3 ) break; else continue; cout << i; } • a. 123456 • b. 1234 • c. 4 • d. nothing

  23. MC-10 • What is the output of the following code? int x = 1; for(;;) { if(x%4 == 0) break; cout << x; x++; } • a. 1234 • b. 123 • c. 1 • d. infinite loop

  24. MC-11 • What is the output of the following code? for int (i = 0; i < 10; i++); cout << i%2; • a. 01010101010101 • b. 10101010101010 • c. 0 • d. 1

  25. MC-12 • What is the output of the following code? int k = 1, s = 0; for( ; k <= 8; k *= 2, s += k); cout << s; • a. 02614 • b. 14 • c. 30 • d. 0

  26. MC-13 • What is the output of the following code? int i, sum = 0; for (i = 1; i < 5; i += 1) sum = sum + i; cout << sum; • a. 0 • b. 15 • c. 10 • d. 1

  27. MC-14 • Select the for statement that is equivalent to the following for statement: for (j = 0; j < 8; j++) • a. for (k = 8; k > 1; k -= 1) • b. for (k = 8; k >= 0; k -= 1) • c. for (k = 8; k > 0; k -= 1) • d. for (k = 8; k >= 1; k -= 1)

  28. MC-14 • The following for loop: for (int i = 4 ; i < 9 ; i++) cout << ”x”; • a. will print ‘x’ 4 times. • b. will print ‘x’ 5 times. • c. will print ‘x’ 6 times. • d. will print ‘x’ repeatedly due to an endless loop. • e. will not compile because braces are missing.

  29. MC-15 • What is the output of the following code? • int i, j; for (i=0, j=8; i<8; i++, j--) cout<<i<<"+"<<j<<"="<<i+j<<endl; • 0+8=8 • 1+7=8 • 2+6=8 • 3+5=8 • 4+4=8 • 5+3=8 • 6+2=8 • 7+1=8

  30. MC-16 • What is the output of the following code? int i, j; for ( i = 0; i < 2; ++i ) for ( j = 0; j < 5; ++j ) cout << i << " " << j << endl; • 0 0 • 0 1 • 0 2 • 0 3 • 0 4 • 1 0 • 1 1 • 1 2 • 1 3 • 1 4

  31. MC-17 • What is the output of the following code? int i; for(i = 1; i <= 5; i++) cout << i << i%2 << endl; • 11 • 20 • 31 • 40 • 51

  32. MC-18 • Write a C++ program to display the following B BB BBB BBBB BBBBB • #include <iostream.h> • void main(){ • int i, j; • for(i = 1; i <= 5; i++){ • for(j = 1; j <= i; j++) • cout << ‘B’; • cout << endl; • } • }

  33. MC-19 • What three parts of a counting loop must be coordinated in order for the loop to work properly? • a. initializing the counter, testing the counter, changing the counter • b. initializing the condition, changing the condition, terminating the loop • c. the while, the assignment, and the loop body • d. the while statement, the if statement, and sequential execution.

  34. MC-20 • What makes a loop a counting loop? • a. A loop control variable is tested in the while statement, and is changed each time the loop body executes. • b. A counter is counted upwards by one until it hits a particular limit. • c. A counter is counted downwards by one until it hits zero. • d. No loop control variables are used.

  35. MC-21 • What does this code print on the monitor? int count = 0; while ( count <= 6 ) { cout << count + " "; count = count + 2; } • a. 1 2 3 4 5 6 • b. 0 2 4 6 8 • c. 0 2 4 • d. 0 2 4 6

  36. MC-22 • What does this code print on the monitor? int count = 7; while ( count >= 4 ) { cout << count + " "; count = count - 1; } • a. 1 2 3 4 5 6 7 • b. 7 6 5 4 • c. 6 5 4 3 • d. 7 6 5 4 3

  37. MC-23 • What does this code print on the monitor? int count = -2 ; while ( count < 3 ) { cout << count + " "; count = count + 1; } • a. -2 -1 1 2 3 4 • b. -2 -1 1 2 3 • c. -3 -4 -5 -6 -7 • d. -2 -1 0 1 2

  38. MC-24 • What does this code print on the monitor? int count = 1; while ( count < 5 ) { cout << count + " "; } • a. 1 2 3 4 • b. 1 2 3 4 5 • c. 2 3 4 • d. 1 1 1 1 1 1 1 1 1 1 1 . . . .

  39. MC-25 • What condition should be used so that the code writes out: 1 2 3 4 5 6 7 8 int count = 1; while ( ___________ ) { cout << count + " "; count = count + 1; } • a. count < 8 • b. count < 9 • c. count+1 <= 8 • d. count != 8

  40. MC-26 • Which of the following situation most likely does NOT call for a counting loop? • a. Adding up all the integers between 0 and 100. • b. Writing out a table of Fahrenheit and Celcius temperature equivalents. • c. Prompting the user of a program until the user responds with correct information. • d. Making the computer beep 10 times.

  41. MC-27 • What are the three types of loops that can be built using the while statement and other statements? • a. Counting loops, sentinel-controlled loops, flag-controlled loops • b. Increasing loops, decreasing loops, flag-controlled loops • c. if loops, while loops, counting loops • d. top-driven loops, flag-controlled loops, bottom feeder loops

  42. MC-28 • Which of the following is most likely to use a counting loop? • a. Checking that each price in a list of items offered for sale is less than $125. • b. Asking the user at the end of a game if the user wants to play again. • c. Checking if a particular integer is even or odd. • d. Trying various letter substitution combinations until a message in a secret code can be read.

  43. MC-29 • Which of the following is most likely to use a sentinel loop? • a. Checking that each price in a list of items offered for sale is less than $125. • b. Asking the user at the end of a game if the user wants to play again. • c. Checking if a particular integer is even or odd. • d. Trying various letter substitution combinations until a message in a secret code can be read.

  44. MC-30 • Which of the following is most likely to use a flag-controlled loop? • a. Checking that each price in a list of items offered for sale is less than $125. • b. Asking the user at the end of a game if the user wants to play again. • c. Checking if a particular integer is even or odd. • d. Trying various letter substitution combinations until a message in a secret code can be read.

  45. MC-31 • Here is part of a graphics program that simulates a color fading in the sun. The amount of red starts at the maximum of 1.0 and is faded by decreasing it by 1% each time the loop executes, until it is close to zero. float redLevel = 1.0; while ( __________________ ){ redLevel = redLevel*0.99 ; … } • Pick a condition for the while statement. • a. redLevel == 0.0 • b. redLevel > 0.001 • c. Math.abs(redLevel) < 0.0 • d. redLevel*redLevel < 1.0

  46. MC-32 • When the operating system of a computer writes data to the hard disk, the hard disk is often not ready and does not accept the data. The operating system must repeatedly try to write the data until it is accepted. What kind of loop is this? • a. Sentinel-controlled. • b. Counting loop. • c. Disk-controlled. • d. Result-controlled.

  47. MC-33 • A colony of rabbits doubles its population every 28 days. The population starts out at 2 and increases until it reaches 100,000. Say that a section of code simulated this process. Which of the following while statements is most likely to be used? • a. while ( population = 10000 ) • b. while ( population < 100000 ) • c. while ( population != 100000 ) • d. while ( population < 1.0E+6 )

  48. MC-34 • What is the output of the following program fragment? for ( int j = 0; j < 5; j++ ) cout << j + " " ; • a. 0 1 2 3 4 5 • b. 0 1 2 3 4 • c. 0 1 2 3 4 5 • d. j j j j j

  49. MC-35 • What must the test be so that the following fragment prints out the integers 5 through and including 15? for ( int j = 5; ________ ; j++ ) cout << j + " " ; • a. j < 15 • b. j <= 16 • c. j < 16 • d. j == 15

  50. MC-36 • What must the change be so that the following fragment prints out the even integers 0 2 4 6 8 10? for ( int j = 0; j <= 10; _______) cout << j + ” ”; • a. j+2 • b. j = j+2 • c. j++++ • d. ++j++

More Related