Example model Questions for Midterm and Final Exam

PART A

1.  Suppose that x and y are int variables. Which of the following is a valid input statement?

a). cin>x>cin>y; b). cin<x<y;

c). cin>x>y; d). cout<x<y;

2.  Suppose that x is an int variable, y is a double variable, z is an int variable, and the input is:

15

76.3

14

Choose the values after the following statement executes:

cin>x>y>z;

a). x = 15, y = 76, z = 14 b). x = 15, y = 76, z = 0

c). x = 15, y = 76.3, z = 14 d). x = 15.0, y = 76.3, z = 14.0

3.  The cin object is used to enter data into a program while it is ____.

a). compiling b). interpreting

c). executing d). loading

4.  The const qualifier specifies that the declared identifier can only be read after it is ____.

a). declared b). edited

c). initialized d). validated

5.  One of the most common C++ programming errors is forgetting to ____ values for all variables that are used in an expression.

a). assign or initialize b). compute

c). read in d). parameterize

6.  Memory locations having the bool data type typically are initialized using either the C++ keyword ____ or the C++ keyword ____.

a). True, False b). TRUE, FALSE

c). true, false d). T, F

7.  After executing the following lines of code, the result will be ____.

int average=0;

double test1=90.0;

double test2=81.0;

average=(test1 + test2)/2;

a). 85 assigned to average b). 86 assigned to average

c). 85.5 assigned to average d). a syntax error

8.  Given the following definition

int x,y=2;

which of the following are correct assignment statements?

I. x = 10;

II. x = y*2;

III. x*2 = y;

a). I only b). I and II only

c). I and III only d). I, II, and III

9.  What is the output produced by the following segment of code?

int x,y;

y=123;

x=y+y;

y=19;

cout<"x = "<x;

cout<"\ty = "< y;

a). x = 246 y = 19 b). x = 246 y = 123

c). x = 38 y = 19 d). x = 38 y = 246

10. Given the following definition

const int YEAR = 1978;

Which of the following statements are not legal?

I. YEAR = 1978 + 1;

II. cin>YEAR;

III. cout<YEAR;

a. I only

b. I and II only

c. III only

d. I, II, and III

11. What is the output produced by the following segment of code?

cout<setw(10)<left<"dog";

cout<setw(5)<"cat"<endl;

a). 7 spaces, dog, 2 spaces, cat b). dog, 7 spaces, cat, 2 spaces

c). dogcat followed by 9 spaces d. 10 spaces, dog, 5 spaces, cat

12. const identifiers should be used instead of variables when:

a). the value should not be changed by the program.

b). the value must be changed frequently during the run of the program.

c). the value is very large.

d). the name starts with an uppercase letter.

13. What is the seen on the console screen after the following lines execute?

const double PI = 3.14;

cout<setprecision(5)<fixed< PI;

a). 3.14 b). 3.1400

c). 3.14000 d). 003.14

14. Which of the following is true?
a). 1 b). 66

c). 0.1 d). -1
e). All of the above

15. Which of the following is a correct comment?
a). */ Comments */ b). ** Comment **

c). /* Comment */ d). { Comment }

16.  Which of the following is not a correct variable type?
a). float b). real
c). int d). double

17. What is the correct value to return to the operating system upon the successful completion of a C++ program?
a). -1 b). 1
c). 0 d). Programs do not return a value.

18. What punctuation is used to signal the beginning and end of code blocks?
a). { } b). -> and <-
c). BEGIN and END d). ( and )

19. Every function in C++ are followed by

a). Parameters b). Parenthesis

c). Curly braces d). None of these

20. Which of the following is false?

a). cout represents the standard output stream in c++.

b). cout is declared in the iostream standard file

c). cout is declared within the std namespace

d). None of above

21. When writing comments you can

a). Use code and /* comment on the same line

b). Use code and // comments on the same line

c). Use code and //* comments on the same line

d). Use code and <!- comments on the same line

22. A variable is/are

a). String that varies during program execution

b). A portion of memory to store a determined value

c). Those numbers that are frequently required in programs

d). None of these

23. Which of the following can not be used as identifiers?

a). Letters b). Digits

c). Underscores d). Spaces

24. The difference between x and ‘x’ is

a).The first one refers to a variable whose identifier is x and the second one refers to the character constant x

b). The first one is a character constant x and second one is the string literal x

c). Both are same

d). None of above

25. Regarding #define which of the following statement is false?

a). It is not C++ statement but the directive for the preprocessor

b). This does not require a semicolon at the end of line

c). It is a C++ statement that declares a constant in C++

d).None of the above

26. Regarding following statementwhich of the statements is true?

const int pathwidth=100;

a). Declares a variable pathwidth with 100 as its initial value

b). Declares a construction pathwidth with 100 as its initial value

c). Declares a constant pathwidth whose value will be 100

d). Constructs an integer type variable with pathwidth as identifier and 100 as value

27. In an assignment statement

a). The lvalue must always be a variable

b). The rvalue might be a constant, a variable, an expression or any combination of these

c). The assignment always takes place from right to left and never the other way

d). All of above

28. In an assignment statement a=b; Which of the following statement is true?

a). The variable a and the variable b are equal.

b). The value of b is assigned to variable a but the later changes on variable b will not effect the value of variable a

c). The value of b is assigned to variable a and the later changes on variable b will effect the value of variable a

d). The value of variable a is assigned to variable b and the value of variable b is assigned to variable a.

29. Which of the following is known as insertion operator?

a). ^ b). v

c). d).

30. The return(0); statement in main function indicates

a). The program did nothing; completed 0 tasks

b). The program worked as expected without any errors during its execution

c).not to end the program yet.

d).None of above

31. Identify the correct statement regarding scope of variables

a). Global variables are declared in a separate file and accessible from any program.

b).Local variables are declared inside a function and accessible within the function only.

c).Global variables are declared inside a function and accessible from anywhere in program.

d).Local variables are declared in the main body of the program and accessible only from functions.

32. The size of following data type is not 4 bytes in 32 bit systems

a). int b).long int

c).short int d). float

33. What is the seen on the console screen after the following lines execute?

const double S = 0.314;

cout<setprecision(5)<S;

a). 0.314 b). 0.31400

c). 0.314000 d). 3.1400

34. Assume int a='C'; what is the output of the following programming statement?

cout<a;

a).C b).d c).b

d).67 e).an error message displayed

35. What is the output produced by the following segment of code?

cout<setw(10)<"Welcome";

cout<setw(6)<"Exam"<endl;

a). 3 spaces, Welcome, 2 spaces, Exam

b). Welcome, 3 spaces, Exam, 2 spaces

c). WelcomeExam followed by 5 spaces

d. 10 spaces, Welcome, 6 spaces, Exam

e). Welcome, 2 spaces, Exam, 3 spaces

36. Assume char ch=66; what is the output of the following programming statement?

cout<ch;

a).a b).66 c).B

d).b e).an error message displayed

37. What is the output produced by the following segment of code?

cout<setw(10)<left<"Hello";

cout<setw(5)<"me"<endl;

a). Hello, 5 spaces, me, 3 spaces b). 5 spaces, Hello, 3 spaces, me

c). Hellome followed by 8 spaces d). 10 spaces, Hello, 5 spaces, me

e). Hello, 3 spaces, me, 5 spaces

38. Regarding #define which of the following statement is false?

a). It is not C++ statement but the directive for the preprocessor

b). This does not require a semicolon at the end of line

c). It is a C++ statement that declares a constant in C++

d).All the above

e). None of the above

39.

PART B

1.  Write a program to calculate the pay of employee based on the hour basis.

2.  Write a program to calculate the area of 15 rooms. The length is 55.2meters, and breadth is 26.34meters.

3.  A car holds 60 litters of petrol and can travel 350 miles before refuel. Write an Algorithm and program that calculates the number of Miles Per Litter the car gets. Display the result on the screen.

Hint: MPL=Miles Driven/Litters of Petrol Used

4.  You have been given a job as a programmer on a Software company. In order to accomplish some calculations, you need to know how many bytes the following data types use: char, int, float and double. You do not have manuals, so you can’t look this information up. Write a C++ program that will determine the amount of memory used by these types and display the information on the screen.

5.  A car with a 20 litters of petrol tank averages 21.5 Miles Per Litter (MPL) when driven in town and 26.8 Miles Per Litter (MPL) when driven on the high way. Write an Algorithm and Program that calculates and display the distance the car can travel on one tank of petrol when driven in town and when driven on the high way.

Hint: distance=Number of Litters*Average Miles Per Litter

6.  Convert the following pseudocode to C++ code. Be sure to define the appropriate variables.

Store 20 in the speed variable

Store 10 in the time variable

Multiply speed by time and store the result in the distance variable

Display the contents of the distance variable.

7.  Convert the following pseudocode to C++ code. Be sure to define the appropriate variables.

Store 172.5 in the force variable

Store 27.5 in the area variable

Divide area by force and store the result in the pressure variable

Display the contents of the pressure variable.

8.  Write assignment statements that performs the following operations with the variables a,b and c. Identify the data types of these variables.

a). Adds 2 to a and stores the result in b

b). Multiplies b times 4 and stores the result in a

c).Divides a by 3 and stores the result in b

d).Subtracts 8 from b and stores the result in a

e). Stores the value 27 in a

f). Stores the character ‘K’ in c

g). Stores the ASCII code for ‘B’ in c

9.  How may the float variables temp, weight and age be defined in one statement, with temp initialized to 25.5 and weight initialized to 78.98

10. How may the unsigned short int variables months, days, and years be defined in one statement, with months initialized to 2 and years initialized to 3

11. Modify the following program so it prints two blank lines between each lines of text.

#include<iostream>

using namespace std;

int main()

{

cout<"Two mandolins like creatures in the";

cout<"dark";

cout<"Creating the agony of ecstasy.";

cout<" - George Barker";

return(0);

}

12. Write a program to convert the meters to inches

Hint: 1 meter = 39.3700787 inches

13. Write a program to convert the inches to meters

Hint: 1 inch = 0.0254 meters

14. Draw the Flowchart for the following algorithm

Start

Step 1: Set SUM to 0, Set COUNTER to 0.

Step 2: Read N

Step 3: While N > 0 do

Step 4: Read Number

Step 5: Print Number

Step 6: COUNTER=COUNTER + 1

Step 7: SUM = SUM + Number

Step 8: N=N-1

end-while

Step 9: if COUNTER = = 0 then

Step 10: AVG = 0

else

Step 11: AVG = SUM / COUNTER

end-if

Step 12: Print the SUM

Step 13: Print the AVG

Stop.

15. Draw the Flowchart for the following algorithm

Start

Step 1: Read m, n

Step 2: Repeat until n!=0

Step 3: r=m%n

Step 4: m=n

Step 5: n=r

Step 6: End Repeat

Step 7: Display ‘m’ as the GCD

Stop

16. Draw a flowchart for the problem that is accepting two numbers and calculating sum and difference and then displaying the result.

17. Draw a flowchart to add all the numbers from 1 to 20.

18. A program has to be written to display the grades of the students in a class of 30 students. Grade is calculated on the basis of percentage (%) marks obtained by the student.

•  More than or equal to 80% A

•  More than or equal to 60% B

•  More than or equal to 50% C

•  More than or equal to 40% D

•  Less than 40% F (Fail)

Draw a flowchart and write the corresponding algorithm

19. Draw a flowchart that accepts 3 numbers say x, y, and z from the user and finds the largest number.