computer engineering department cmpe110 midterm sample

15
Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/2018 - Fall Q1) Give the correct answer for following: 1. What is the value of x after the following statement? double x; x = 3.0 / 4.0 + 3 + 2 / 4 a. 5.75 b. 4.25 c. 1.75 d. 3.75 2. What is the correct way to write the condition y < x < z in C++ form? a. (y < x < z) b. ( (y < x) && z) c. ((y > x) || (y < z)) d. ((y < x) && (x < z)) 3. What is the output of the following code fragment? int x=0; while( x < 5) cout << x << endl; x ++; cout << x << endl; a. 0 b. 5 c. 4 d. unable to determine 4. Given the following code fragment, which of the following expressions is always true? int x; cin >> x; a. if( x < 3) b. if( x==1) c. if( (x / 3) >1 ) d. if( x = 1) 5. What is wrong with the following switch statement? int ans; cout <<"Type y for yes on n for no\n"; cin >> ans; switch (ans) { case 'y': case 'Y': cout << "You said yes\n"; break; case 'n': case 'N': cout << "You said no\n"; break; default: cout <<"invalid answer\n"; } a. ans is an int b. break; is illegal syntax c. nothing d. there are no break statements on 2 cases.

Upload: others

Post on 24-Feb-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Engineering Department CMPE110 Midterm Sample

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/2018 - Fall

Q1) Give the correct answer for following:

1. What is the value of x after the following statement? double x; x = 3.0 / 4.0 + 3 + 2 / 4

a. 5.75 b. 4.25 c. 1.75 d. 3.75

2. What is the correct way to write the condition y < x < z in C++ form? a. (y < x < z) b. ( (y < x) && z) c. ((y > x) || (y < z)) d. ((y < x) && (x < z))

3. What is the output of the following code fragment? int x=0; while( x < 5) cout << x << endl; x ++; cout << x << endl; a. 0 b. 5 c. 4 d. unable to determine

4. Given the following code fragment, which of the following expressions is always true?

int x; cin >> x;

a. if( x < 3) b. if( x==1) c. if( (x / 3) >1 ) d. if( x = 1)

5. What is wrong with the following switch statement? int ans; cout <<"Type y for yes on n for no\n"; cin >> ans; switch (ans) { case 'y':

case 'Y': cout << "You said yes\n"; break; case 'n': case 'N': cout << "You said no\n"; break; default: cout <<"invalid answer\n";

} a. ans is an int b. break; is illegal syntax c. nothing d. there are no break statements on 2 cases.

Page 2: Computer Engineering Department CMPE110 Midterm Sample

6. int a = 5, b = 8, t;

t = a;a = b;b = t;

cout << a<< ", " << b; A. 5,8

B. 5,5 C. 8,8 D. 8,5

7. int x=5;

if( x > 5)

cout << "x is bigger than 5. ";

cout <<"That is all. ";

cout << "Goodbye\n";

a. x is bigger than 5. That is all

b. x is bigger than 5 c. That is all. Goodbye d. Goodbye

8. int FD, Year=2019;

FD=Year%4 == 0 ?29 :28; cout<<FD;

A. 29

B. 28 C. 1 D. 0

9. int a = 5, b = 8;

a = b;b = a;

cout << a<< ", " << b; A. 5,8 B. 5,5 C. 8,8 D. 8,5

Q2) Consider the code given below. Show its output and explain what does the code do? #include <iostream>

using namespace std;

int main(){

int x;

for(x=325;x > 0; x /= 10)

{cout<< (x % 10);}

return 0;}

Output:

……………………………………………………………….

The main function of this code is

……………………………………………………………………………………………….

Page 3: Computer Engineering Department CMPE110 Midterm Sample

Q3) Write a program that reads the radius (r) and the height (h) of a cylinder and then computes

its volume and surface area. The volume and the surface area are computed as

hrvolume 2 222 rhrareasurface

Note: Let all variables be of type float. A sample run of the code should be as

#include<iostream>

using namespace std;

int main(){

//declare the variables

………………………………

// write necessary statements to read the radius from keyboard

………………………

………………………

// write necessary statements to read the height from keyboard

………………………

………………………

// perform the required operations

………………………………………

………………………………………

// show the outputs on the monitor

………………………………………

………………………………………

return 0;}

Enter the radius: 6.5

Enter the height: 9.5

The volume = 1275.86

The surface area = 658.37

Page 4: Computer Engineering Department CMPE110 Midterm Sample

Q4) What is the output of the following code segments:

I/ int Year;

cout<< ”Enter a year: ”;

cin>> year;

FebDays=year%4 == 0 ?29 :28; cout<< ”FebDays= ”<< FebDays;

II/

int grade;

cout<< ”Enter your Grade: ”;

cin>> grade;

if(grade >= 60) {cout<< ”You passed”<< endl;

cout<< ”Congratulations\n”;}

else;

cout<< ”You failed”<< endl;

cout<< ”SORRY\n”;

III/ Section II may contain logical error(s). If any, introduce

your correction(s) on the code segment.

Q5) What is the output of the following code segments:

1. int i, j=2;

for(i=3; i<=8; i+=2)

{j+=i;

if (j>6)

break;

}

cout<<“J=”<<j;

2. int i, Sum=2;

for(i=3; i<=10; i+=2)

{if (i%3==0)

continue;

Sum+=i;

}

cout<<“Sum=”<<Sum;

Enter your grade: 80

……………………………………………………….

……………………………………………………….

……………………………………………………….

……………………………………………………….

Enter a year: 2017

……………………………………………………….

Page 5: Computer Engineering Department CMPE110 Midterm Sample

Q6) The following code segment is designed to read the age of a person, and display

his/her class as shown in the given table. Fill in the blanks to complete the required task.

A sample run of the code is:

int Age;

cout<<”How old are you? ”;

cin>>Age;

switch( ………………………………….){

case 0 : …………………………………………………………………………………..

case 1 :

case 2 : …………………………………………………………………………………..

case 3 : …………………………………………………………………………………..

case 4 :

case 5 :

case 6 :

case 7 :

case 8 :

case 9 :

case 10:

case 11:

case 12: …………………………………………………………………………………..

default: …………………………………………………………………………………..}

Q7) Write the equivalent C++ condition to implement the following statements.

Statement Equivalent C++ condition

x greater than 0 and not more than 10

x is even and y is odd

x is outside the interval [-5,20]

X and y are less than or equal to 30

Age Class

0 – 4 Baby

5 –14 Child

15 –19 Teenager

20 –64 Adult

above

64

Old

How old are you? 18

You are a teenager

Page 6: Computer Engineering Department CMPE110 Midterm Sample

Q8) The following flowchart is designed to read from the keyboard the Grades of N

students in CMPE110 course, and then it computes and prints on the monitor the

minimum grade and the average grade. Implement this flowchart in C++ language.

Note: let variable average be of type double, and other variables are of type integer.

READ Grade

BEGIN

END

Min=Grade

Print Min

READ N

i <= N

i = 1 Min=100 Sum=0

True

i = i + 1

False

Grade < Min

True

False

#include<iostream>

using namespace std;

int main(){

…………… …………… ……………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

………………………………………

return 0;}

Sum+=Grade

Average=sum/N

Design your code to have the following sample run:

Enter number of students (N): 3

Enter grade of student 1: 18

Enter grade of student 2: 19

Enter grade of student 3: 17

The minimum grade is 17

The average grade is 18.0

Print Average

Page 7: Computer Engineering Department CMPE110 Midterm Sample

Q9) Complete the following code that reads two integer numbers (N1 and N2) and then

finds the sum of all integers, between N1 and N2, that can be divided by 5 without a

reminder. Do not include in the sum those integers that are divided by 3 without a

reminder. Note: first, you should find the minimum and the maximum of N1 and N2. For

example, if N1=23 and N2=4, then the program should find Min=4, Max=23 and then

prints on the monitor: Sum = 35 (5+10+20, 15 is not included because it can be divided

by 3 without a reminder).

#include <iostream>

using namespace std;

int main(){int N1, N2, Min, Max,i, Sum=0;

cin>>N1>>N2;

// Find the minimum (Min) and the maximum (Max) of N1 and N2

………………………………………………………….

………………………………………………………….

………………………………………………………….

………………………………………………………….

………………………………………………………….

………………………………………………………….

// Find the required sum

………………………………………………………….

………………………………………………………….

………………………………………………………….

………………………………………………………….

………………………………………………………….

// Print the result on the monitor

cout<<”Sum =”<< Sum;

return 0;}

Page 8: Computer Engineering Department CMPE110 Midterm Sample

Q10) Consider the following code: Line 1

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

Line 11

#include<iostream>

using namespace std;

int main(){

int Digit;

cin>>Digit;

switch(Digit)

{case 0 :

case 1 : cout<<"This digit is a binary.";

default: cout<<"This digit is not a binary.";

} return 0;}

A/ Trace this code for the following input:

Input Output

2 …………………………………………………………………………………………………………

1 …………………………………………………………………………………………………………

B/ This code may contain logical error(s), indicate which line(s), and if any, give below the correct one(s) Line(if any) …………… should be

………………………………………………………………………………………………………

Q11) Write a program that reads the lengths of two sides of a triangle (SideA, SideB) and the angle between them (A) and calculates the length of the third side (SideC) using the following formula:

2 2= 2 cos( )SideC SideA SideB SideA SideB A

Note: The angle A is entered from keyboard in degree and should be translated into

radian as =180

AA

#include <iostream>

#include <cmath>

using namespace std;

// define the constant with the name of PI and value of 3.1459

……………………………………………………………………………………………….

int main(){// Variable declarations

……………………………………………………………………………………………….

// read SideA and SideB and angle A from keyboard

……………………………………………………………………………………………….

// tranlate the angle A into radian

……………………………………………………………………………………………….

// compute SideC

……………………………………………………………………………………………….

// print SideC on monitor

……………………………………………………………………………………………….

return 0;}

Page 9: Computer Engineering Department CMPE110 Midterm Sample

Q12) It is required to write a code to read from the keyboard the lab (Lab), the midterm

(Mid), and the final (Final) points of a student and then computes his letter grade

(Lgrade). Let the letter grades be computed as

Total point (Tpoint) Letter grade

85 ≤ Tpoint ≤ 100 A

75 ≤ Tpoint < 85 B

65 ≤ Tpoint < 75 C

55 ≤ Tpoint < 65 D

Otherwise F

where Tpoint is computed from the Lab, the Mid, and the Final points according to the

following weights:

Complete the following code to do the above task.

#include <iostream>

using namespace std;

int main(){

// Declaration

…………………………………………………………….

…………………………………………………………….

// Get the input from the keyboard

cin>>Lab>>Mid>>Final;

// Compute Total point (Tpoint)

…………………………………………………………….

// Compute Letter Grade (Lgrade)

…………………………………………….. ……………………………………………..

…………………………………………….. ……………………………………………..

…………………………………………….. ……………………………………………..

…………………………………………….. ……………………………………………..

…………………………………………….. ……………………………………………..

// Display Letter grade on monitor

cout<<”Your letter grades is ”<< Lgrade;

return 0;}

Lab 10 %

Midterm 40 %

Final 50 %

Page 10: Computer Engineering Department CMPE110 Midterm Sample

Q13) Write a C++ code that reads two integer numbers and one operator (+, -, *, /, %)

and then perform the required operation. Note: If the user enters a wrong operator, then

your code should display “wrong operator” as an output. Write the code using switch

selective structure (do not use if/else). A sample run of the code must be as follows:

#include <iostream>

using namespace std;

int main(){

// Declaration

…………………………………………………………….

…………………………………………………………….

// Get the input from the keyboard

…………………………………………………………….

…………………………………………………………….

…………………………………………………………….

…………………………………………………………….

// Perform the required task

…………………………………………………

……………………………… ……………………………… ………………………………

……………………………… ……………………………… ………………………………

……………………………… ……………………………… ………………………………

……………………………… ……………………………… ………………………………

……………………………… ……………………………… ………………………………

……………………………… ……………………………… ………………………………

return 0;}

*** Calculator Problem ***

Enter two numbers: 4 3

Enter one operator: +

4 + 3 = 7

*** Calculator Problem ***

Enter two numbers: 4 3

Enter one operator: >

Wrong operaor

Page 11: Computer Engineering Department CMPE110 Midterm Sample

Q14) Re-write the following if/else using switch statement: if(Digit==0)

cout<<“ZERO”;

else if(Digit==1)

cout<<“ONE”;

else

cout<<”Not a Binary Digit”;

Q15) Re-write the following if/else using conditional expression operator (?:) if(Number % 2 = = 0)

cout<<“EVEN Number”;

else

cout<<“ODD Number”;

a) Re-write the following if/else using switch statement:

if(Digit==0 || Digit==1)

cout<<“Binary”;

else

cout“Not Binary”;

b) Re-write the following code using for-loop:

year=1900;

sum=0;

while(year<=4002)

{

sum+=year;

cout<<"Year="<<year;

year+=sum; }

Q16) Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and

displays their distances. The formula for computing the distance (d) is

2 2

2 1 2 1( ) ( )d x x y y Note you can use pow(a,0.5) to compute a . Let all

variables be of type double.

Here is a sample run:

Enter x1 and y1: 1.5 -3.4

Enter x2 and y2: 4 5

The distance of the two points is 8.764131445842194

Page 12: Computer Engineering Department CMPE110 Midterm Sample

Q17)The following FLOWCHART computes the area of a triangle. Write the

corresponding C++ code.

#include <iostream>

#include <cmath>

using namespace std;

int main(){

double A,B,C,P,S, Area;

………………………………………………………………

………………………………………………………………

………………………………………………………………

………………………………………………………………

………………………………………………………………

………………………………………………………………

………………………………………………………………

………………………………………………………………

………………………………………………………………

return 0;}

BEGIN

A+B>C and B+C>A and C+A>B

False

P = A + B + C S =P/2

Area =

READ A, B,C

True

End

PRINT “Not Triangle”

PRINT Area

Page 13: Computer Engineering Department CMPE110 Midterm Sample

Q18)

a) Given int i = 4 , j = 3 , k = 6 , m = 4; what is the output of

i * = j + k;

cout<<“i=”<<i;

b) Given int i = 4 , j = 3 , k = 6 , m = 4; what is the output of

k =++m+ 5;

cout<<“k=”<<k;

c) Find the result of the following expressions:

Expression

Result

6 >= 4 >= 2

(double) (10/4) / (int) 2.5

int a=4, b=7;

a++ * --b

7!=4!=1

int year=2016;

year%4 == 0 ? 29 :28;

d) Show the output of the following code segments:

A/ int x1=1, x2=1, i, y, z; float f;

y = 5 + x1--;

z = 5 + ++x2;

i=35/10;

f = 10./4 * i;

cout << "Y= " << y << endl;

cout << "Z = " << z << endl;

cout << "F= " << f;

B/ char ch = 'b';

switch (ch)

{case 'a':

case 'A': cout << ch << endl; break;

case 'b':

case 'B': cout << ch << endl;

case 'c':

Page 14: Computer Engineering Department CMPE110 Midterm Sample

case 'C': cout << ch << endl; break;

case 'd':

case 'D': cout << ch << endl; }

C/ bool even = false;

cout << (even ? "true" : "false") << endl;

Q19) Consider the definition of the following f(x) function . Write a code read x, and then

calculate and display the value of f(x). The value of input x must be entered from keyboard.

𝑓(𝑥) = {

𝑥 − 3 𝑖𝑓 𝑥 < 50

𝑥2 𝑖𝑓 50 ≤ 𝑥 < 100𝑥/2 𝑖𝑓 𝑥 = 100

0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Page 15: Computer Engineering Department CMPE110 Midterm Sample

Q20) Trace the following programs and write the output in the space provided

below.

int k=0, x=0; for (k=1; k<7; ++k) { if (k%3 == 1) x=x+k; else x- -; cout<< x<<endl; }

int k=0, m=0, x=0; for (k=1; k<4; ++k) { for (m=0; m<k; ++m) { if (k%2 == 0) x++; else x- -; cout<< x<<endl; } }

Output:

Output:

int k=0, x=0; while (k<5) { switch ( (k+x) % 4 ) { case 0: case 1: x = x + 2; break; case 2: x = x - 1; break; default: x = x * 2; } cout<< x<<endl; k++; }

int k=5, x=0; do { if (x==1) x=0; else if (x==2) x=2; else x=3; cout<< x<<endl; x- -; k- -; } while (k>=0);

Output:

Output: