Example 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;

  1. 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 = 14b). x = 15, y = 76, z = 0

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

  1. The cinobject is used to enter data into a program while it is ____.

a). compiling b). interpreting

c). executing d). loading

  1. The constqualifier specifies that the declared identifier can only be read after it is ____.

a). declared b). edited

c). initialized d). validated

  1. 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

  1. 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

  1. 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

  1. 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 onlyb). I and II only

c). I and III onlyd). I, II, and III

  1. 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 = 19b). x = 246 y = 123

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

  1. 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

  1. 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, catb). dog, 7 spaces, cat, 2 spaces

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

  1. 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.

  1. 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.14b). 3.1400

c). 3.14000d). 003.14

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

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

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

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

  1. Which of the following is not a correct variable type?
    a). floatb). real
    c). intd). double
  1. What is the correct value to return to the operating system upon the successful completion of a C++ program?
    a). -1b). 1
    c). 0d). Programs do not return a value.
  1. What punctuation is used to signal the beginning and end of code blocks?
    a). { }b). -> and <-
    c). BEGIN and ENDd). ( and )
  1. Every function in C++ are followed by

a). Parametersb). Parenthesis

c). Curly bracesd). None of these

  1. 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

  1. 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

  1. 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

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

a). Lettersb). Digits

c). Underscoresd). Spaces

  1. 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

  1. 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

  1. 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

  1. 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

  1. In an assignment statementa=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.

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

a). ^b). v

c).d).

  1. 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

  1. 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.

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

a). intb).long int

c).short intd). float

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

const double S = 0.314;

cout<setprecision(5)<S;

a). 0.314b). 0.31400

c). 0.314000d). 3.1400

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

cout<a;

a).Cb).dc).b

d).67e).an error message displayed

  1. 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

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

cout<ch;

a).ab).66c).B

d).be).an error message displayed

  1. 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 spacesb). 5 spaces, Hello, 3 spaces, me

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

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

  1. 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

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

  1. 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.
  2. 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

  1. 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.

  1. 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.

  1. 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

  1. 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
  2. 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
  3. 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);

}

  1. Write a program to convert the meters to inches

Hint:1 meter = 39.3700787 inches

  1. Write a program to convert the inches to meters

Hint: 1 inch = 0.0254 meters

  1. 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.

  1. 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

  1. Draw a flowchart for the problem that is accepting two numbers and calculating sum and difference and then displaying the result.
  1. Draw a flowchart to add all the numbers from 1 to 20.
  1. 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

  1. Draw a flowchart that accepts 3 numbers say x, y, and z from the user and finds the largest number.
  2. Draw the flowchart and write the algorithm to print the Fibonacci Series 0,1,1,2,3,5,8,13,21
  3. Draw the flowchart and write the algorithm to swap the two numbers
  4. Draw the flowchart and write the algorithm to find the given number is odd or even
  5. Draw the flowchart and write the algorithm to find the square of the given number
  6. Draw the flowchart and write the algorithm to calculate the area of the circle
  7. An electricity board charges the following rates to domestic users to discourage large consumption of energy:

For the first 100 units – 10 Paisa per unit
For next 200 units – 20 Paisa per unit
Beyond 300 units – 30 Paisa per unit

If the total cost is more than 10 OMR then an additional surcharge of 15% is added.
Draw the flowchart and write the algorithm to calculate the amount for number of units consumed.

  1. Draw the flowchart and write the algorithm to read 100 different numbers and then display the sum of numbers.

Lab Example Programs for Midterm Exam

  1. Write a program to convert the pounds to kilograms.

Hint: 1 pound=0.45 kilograms

  1. Write a program to convert the feets to inches

Hint: 1 feet =12 inches

  1. Write a program to convert the feets to meters

Hint: 1 feet=0.3048 meters

  1. Write a program to convert the inches into centimetres

Hint: 1 inch=2.540 centimetres

  1. Write a program to convert the Miles Per Hour(MPH) to knot

Hint: 1 MPH=0.8689762 knot

  1. Write a Program to convert the Years to Hours

Hint: 1 Year=365 Days

1 Day=24 Hours

  1. Write a Program to convert the Hours to Seconds

Hint: 1 Hour=60 Minutes

1 Minute=60 Seconds

  1. Write a Program to convert Fahrenheit to Kelvin

Hint: Kelvin=5/9(Fahrenheit-32)+273

  1. Write a program to convert Celsius to Kelvin

Hint: K=Celsius+273

  1. Write a Program to print the following

****

**

*

*

**

****

  1. Write a program to print the following

" " " " " "

"

" " " " " "

"

" " " " " "

  1. Write a program to print the following

?

??

??????????????

?

??????????????

??

?

  1. Write a program to print the following

*

***

*****

*******

*

*

*

*

*

  1. Write a program to print the following

**************

**************

**

**

**

**

**************

**************

  1. Write a program to print the following

#

#

#

#

#####

#

#

#

#

  1. Write a program to print the following

------

------

------

------

------

  1. Write a program to print the following

======

======

= =

= =

= =

= =

= =

= =

  1. Write a program to print the following

*

*

*

**

****

*****

****

**

*

*

*

  1. Write a program to print the following

I I I I I I I I I

I I I I I I I I

I I I I I I I

I I I I I I

I I I I I

I I I I

I I I

I I

I

  1. Write a program to find the given year is leap or not
  2. Write a program to find the given number is divisible by 5 and 10
  3. Write a program to find the given number is divisible by 5 or 10
  4. Write a program to find the given number is less than 100 or not
  5. Write a program to find the given character is ‘M’ or ‘S’ or ‘K’
  6. Write a program to display the discount rate of customers based on their purchased amount as given below

Purchased amount Discount rate

>800 OMR30%

>500 and <=80020%

>100 and <=50010%

OtherwiseNo discount

  1. Write a program to display the category of employees based on their monthly income in an industry as given below

Monthly income Category

Above 1000 OMR A

>800 and <=1000 B

>500 and <=300 C

>300 and <=150 D

Otherwise E

  1. Write a program to display the people category based on their age as given below

Age Category

Above 60Really Old

>=45 and <=60Old

>=30 and <45Middle age

>=20 and <30Adults

>12 and <20Teens

OtherwiseChildren

  1. Write a program to display the people category based on their weight as given below

Weight Category

>=80Very Fat

>=70 and <80Fat

>=60 and <70Some What Fat

>=50 and <60Smart

>=40 and <50Lean

OtherwiseUnder Weight

  1. Write a program to display the rank of the football team as per the points gathered by them

PointsRank

Above 100Very Good

>=75 and <=100Good

>=50 and <75Average

>=35 and <50Poor

OtherwiseVery Poor

1