0% found this document useful (0 votes)
104 views18 pages

CH 1 Number System and Numerical Error Analysis

This document contains a C++ final exam with 37 multiple choice questions testing knowledge of fundamental C++ concepts such as data types, operators, control flow, and input/output. The questions cover topics like integral data types, variable assignment, conditional statements, precedence of operators, and character input/output.

Uploaded by

getacew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views18 pages

CH 1 Number System and Numerical Error Analysis

This document contains a C++ final exam with 37 multiple choice questions testing knowledge of fundamental C++ concepts such as data types, operators, control flow, and input/output. The questions cover topics like integral data types, variable assignment, conditional statements, precedence of operators, and character input/output.

Uploaded by

getacew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

C++ Final Exam 2017/2018

1) All of the following are examples of integral data types EXCEPT ______.
o A Double
o B Char
o C Short
o D Int
2) After the execution of the following code, what will be the value of numb if the input value is 5?

1. cin >> numb;


2. if (numb > 0)
3. numb = numb + 10;
4. else
5. if (numb > 5)
6. numb = numb + 15;

o A 0
o B 5
o C 25
o D 15
3) What type of C++ statement(s) stores a value in a variable?
o A both input and assignments
o B assignments
o C output
o D input

4) Which of the following is NOT a reserved word in C++?

o A return B int C const D num

5) What is the output?

1. int n = 0;
2. cout << ((n) ? n++ : ++n);

o A 0
o B 1
o C None of these
o D 2

Page 1 of 18

#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

6) What is the output?


1. int x = 5;
2. cout<< (x++)+x<<endl;

o A 11
o B 10
o C None of these
o D 12
7) What is the output of the following code?

1. if ( 6 > 8)
2. {
3. cout << " ** " << endl ;
4. cout << "****" << endl;
5. }
6. else if (9 == 4)
7. cout << "***" << endl;
8. else
9. cout << "*" << endl;

o A ****
o B ***
o C *
o D **
8) Which of these is a valid reserved word in C++?
o A Char B Include C Double D Const
o E None of these
9) Suppose that x, y, z, and w are int variables. The expression x(y+z)/w in C++ is written as
o A x * (y + z) / w
o B x*y+z/w
o C x*y+x*z/w
o D x (y + z) / w
10) The expression static_cast<int>(6.9) + static_cast<int>(7.9) evaluates to ____.
o A 14
o B 14.8
o C 13
o D 15

Page 2 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

11) Suppose that ch1, ch2, and ch3 are variables of the type char and the input is:

AB
C
**What is the value of ch3 after the following statements execute?

1. cin.get(ch1);
2. cin.get(ch2);
o cin.get(ch3);

o A 'B'
o B 'C'
o C 'A'
o D '\n'
12) Suppose x and y are int variables. Consider the following statements:

1. if (x > 5)
2. y = 1;
3. else if (x < 5)
4. {
5. if (x < 3)
6. y = 2;
7. else
8. y = 3;
9. }
10. else
11. y = 4;

**What is the value of y if x = 3?

o A 3
o B 1
o C 2
o D 4

Page 3 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

13) The value of the expression 26 – 14 % 3 + 1 is ____.


o A 1
o B 25
o C 24
o D 0

14) Suppose that x is an int variable, ch is a char variable, and the input is: 276.

**Choose the values after the following statement executes: cin >> ch >> x;

o A ch = '2', x = 76
o B ch = '2', x = 276
o C ch = ' ', x = 276
o D Input Failure

1. char ch1, ch2;


2. cin >> ch1;
3. cin.get(ch2);
4. cout << ch1 << ch2;
15) If the input is: M N
**what is the output?
o A MN B M C MN D None of these E N

16) What is the output of the following C++ code?

1. x = 6;
2. if (x > 10)
3. cout << "One ";
4. cout << "Two ";

o A One
o B Two
o C One Two
o D Two One

Page 4 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

17) What is the output?

1. int x = 1;
2. while (--x)
3. cout<< x;

o A 0
o B 1
o C No output
o D Infinite loop
18) Which of the following operators has the highest precedence?
o A * B ! C % D =

19) If the input is: 35 62&1.78


**what are the values of x,z,ch?
1. int x;
2. double z;
3. char ch;
4. cin>>x>>ch>>z;

o A x = 35, ch = '6', z= 2.0 (or 2)


o B x = 35, ch = 6, z= 2.0
o C Input Failure
o D x = 35, ch = 62, z= 78
20) What is the output?

1. int n = 0;
2. cout << ((n) ? n++ : ++n);

o A 1
o B 0
o C None of these
o D 2

Page 5 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

21) After the execution of the following code, what is the value of sum?

1. int sum = 0;
2. int num = 10;
3. if (num > 0)
4. sum = sum + 10;
5. else if (num > 5)
6. sum = num + 15;

o A 20
o B 10
o C 25
o D 0
22) Assume you have three int variables: x = 2, y = 6, and z. Choose the value of z in the following expression:

z = (y / x > 0) ? x : y;

o A 2
o B 3
o C 6
o D 4

23) What is the output?

1. cout << static_cast<int>(7.5 + static_cast<double> (19 / 2));

o A 16
o B 16.5
o C None of these
o D 17
o E 17.5

Page 6 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

24) Which of the following is a reserved word in C++?

o A Char
o B include
o C void
o D duble

25) Which of these is an invalid identifier?

o A None of these
o B pop_
o C _D3
o D 1b
o E _sum3

26) Which of the following is a legal identifier?

o A 1program
o B program_1
o C program 1
o D program!

27) What is the output?


1. cout<<"Welcome\rHi";

o A Syntax Error B Hilcome C Hi D HiWelcome


Welcome

Page 7 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

28) What is the output?

1. int i = 4, j = 6, k = 7, n = 3;
2. cout << i + j*k - k%n << endl;
3. cout << i / k << endl;

o A 45
0.66667
o B None of these
o C 0
0
o D 45
0

29) What is the output?

1. int x = 5;
2. if (x-- == 4)
3. cout << "One" << endl;
4. cout << x << endl;
5. cout << "Two";

o A 5
Two
o B One
Two
o C One
5 Two
o D 4
Two
o E One
4 Two

Page 8 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

30) The conditional operator ? : takes ____ arguments.

o A 1
o B 2
o C 3
o D 4

31) What is the output?

1. if (15 % 5)
2. cout << "Two";
3. else cout << "One";

o A One
o B None of these
o C Two
o D Two
One

32) Which of the following is the new line character?

o A '\r'
o B '/n'
o C '\n'
o D '/r'

33) Which of the following expressions correctly determines that x is greater than 10 and less than 20?
o A 10 < x < 20
o B (10 < x < 20)
o C 10 < x && x < 20
o D 10 < x || x < 20

Page 9 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

34) What is the output?

1. if (1 == 4 >= 1)
2. cout << "One";
3. else
4. cout << "Two";

o A OneTwo
o B One
o C Two
o D None of thes

35) Which of these will cause a syntax error?


o A float f = 1000;
o B char ch = ' ';
o C bool b = 111;
o D None of these
36) What is the output of the following code fragment?

1. int x = 10;
2. if (x > 15)
3. x = 0;
4. cout << x << endl;
5. else
6. cout << x + 5;

o A 5
o B 10
o C 0
o D None of these

Page 10 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

37) Suppose x and y are int variables. Consider the following statements:

1. if (x > 5)
2. y = 1;
3. else if (x < 5)
4. {
5. if (x < 3)
6. y = 2;
7. else
8. y = 3;
9. }
10. else
11. y = 4;

**What is the value of y if x = 6?

o A 1
o B 3
o C 2
o D 4

38) What is the output?

1. int n = 1;
2. cout << ((n) ? n++ : ++n);

o A 3
o B 1
o C 2
o D None of these

Page 11 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

39) Which of these is a valid use of c?

1. const int c = 9;

o A c = c+1;
o B cout<< c*3;
o C c = 20;
o D None of these
oE cin>>c;
40) Which of the following is a relational operator?
o A ==
o B &&
o C =
o D !
41) Suppose that x and y are int variables, z is a double variable, and the input is:

28

32.6 12

**Choose the values of x, y, and z after the following statement executes:

cin >> x >> y >> z;

o A x = 28, y = 12, z = 32.6


o B x = 28, y = 32, z = 0.6
o C x = 28, y = 12, z = 0.6
o D x = 28, y = 32, z = 12.0

Page 12 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

42) Suppose that alpha and beta are int variables. The statement alpha = beta-- ; is equivalent to
the statement(s)
o A alpha = beta - 1;
beta--;
o B alpha = 1 - beta;
beta--;
o C None of these
o D alpha = beta;
beta = beta - 1;

43) Consider the following statement:

int y = !(12 < 5 || 3 <= 5 && 3 > x) ? 7 : 9;


**What is the value of y if x = 2?

o A 3
o B 2
o C 7
o D 9

44) Which of the following will cause a semantic error, if you are trying to compare x to 5?
o A if (x = 5)
o B if (x >= 5)
o C if (x == 5)
o D if (x <= 5)

Page 13 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

45) What is the value of x after the following statements execute?

1. int x;
2. x = (5 <= 3 && 'A' < 'F') ? 3 : 4;

o A 2
o B 3
o C 5
o D 4
46) In a ____ control structure, the computer executes particular statements depending on some condition(s).
o A repetition
o B looping
o C selection
o D sequence
47) What is the output of the following C++ code?

1. int x = 55, y = 5;
2. switch (x % 7)
3. { case 0: case 1: y++;
4. case 2: case 3: y = y + 2;
5. case 4: break;
6. case 5: case 6: y = y – 3;}
7. cout << y << endl;

o A 2 B 5
o C 8 D None of these

48) Consider the following statement:

int y = !(12 < 5 || 3 <= 5 && 3 > x) ? 7 : 9;


**What is the value of y if x = 2?

o A 2
o B 3
o C 7
o D 9
Page 14 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

49) A(n) ____ statement causes an immediate exit from the switch structure.
o A break
o B endl
o C default
o D exit
50) After the execution of the following code, what is the value of sum?

1. int sum = 0;
2. int num = 10;
3. if (num > 0)
4. sum = sum + 10;
5. else if (num > 5)
6. sum = num + 15;

o A 20
o B 10
o C 25
o D 0

51) Consider the following code.

1. const int LASTVAL = -99;


2. int entry;
3. cin >> entry;
4. while (entry != LASTVAL)
5. {
6. triple = entry * 3;
7. cout << triple << endl;
8. cin >> entry;
9. }
**This code is an example of a(n) ____ while loop.

o A EOF-controlled
o B counter-controlled
o C sentinel-controlled
o D None of answers.

Page 15 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

52) Assume all variables are properly declared. What is the output of the following C++ code?

1. num = 100;
2. while (num <= 150)
3. num = num + 5;
4. cout << num << endl;

o A 160
o B 150
o C 145
o D 155
53) What is the output?
1. int x = 5;
2. cout<< (x++)+x<<endl;

o A 10 B 12
o C None of these D 11

54) What is the output of the following program segment?

1. int x = 14, y = 60;


2. while (((y - x) % 3) != 0)
3. {
4. cout << y << " ";
5. y = y - 5;
6. }

o A 60 55 B 46 27
o C 60 55 50 D 60 55 50 45

Page 16 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

55) Which of the following is true about a while loop?

o A It cannot contain if statement.


o B The body of the loop may not execute at all.
o C The body of the loop is executed at least once.
o D The logical expression controlling the loop is evaluated before the loop is entered and after the
loop exits.

56) What is the output of the following C++ code?

1. count = 1;
2. num = 25;
3. while (count < 25)
4. { num = num - 1;
5. count++;}
6. cout << count << " " << num << endl;

o A 25 1
o B 24 1
o C 24 0
o D 25 0
57) In ____ structures, the computer repeats particular statements a certain number of times depending on some
condition(s).
o A looping
o B branching
o C sequence
o D selection
58) What is the output of the following C++ code?

1. num = 0;
2. while (num < 5)
3. { cout << num << " ";
4. num = num + 1; }

o A 012345 B 01234
o C 12345 D 12340

Page 17 of 18
#NASHAMA_BAU @NashamaBAU
C++ Final Exam 2017/2018

59) Suppose sum and num are int variables, and the input is 18 25 61 6 -A What is the
output of the following code?

1. sum = 0;
2. cin >> num;
3. while (num != -1)
4. {
5. sum = sum + num;
6. cin >> num;
7. }
8. cout << sum << endl;

o A 110
o B 92
o C 109
o D 111

^_^ GOOD LUCK ^_^

Page 18 of 18
#NASHAMA_BAU @NashamaBAU

You might also like