0% found this document useful (0 votes)
386 views13 pages

Introduction To Programming in C++ Summer II 2002: Midterm Test

This document provides instructions for a midterm exam in C++ programming. It instructs students to print their name and ID number, choose one answer per question, and not discuss exam content with others. Failure to follow instructions could result in penalties. The exam contains 40 multiple choice questions and 1 extra credit question.

Uploaded by

Ishan Jawa
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)
386 views13 pages

Introduction To Programming in C++ Summer II 2002: Midterm Test

This document provides instructions for a midterm exam in C++ programming. It instructs students to print their name and ID number, choose one answer per question, and not discuss exam content with others. Failure to follow instructions could result in penalties. The exam contains 40 multiple choice questions and 1 extra credit question.

Uploaded by

Ishan Jawa
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/ 13

Introduction to programming in C++

Summer II 2002: Midterm Test

Failure to read and


LYT
ECHNI
C follow the
PO
instructions below

IN
IA

ST
may result in severe
VI RGI N

I TUTE
penalties. Failure to
adhere to these
AN

Y
IT directions will not
D

ST U T P R O S I M
S

AT
E U NI V E
R constitute an excuse
or defense.

• Print your name and 5 digit course index number in the space provided below.
• Print your name and ID number on the Opscan form; be sure to code your ID number on the Opscan form.
Code Form A on the Opscan.
• Choose the single best answer for each question — some answers may be partially correct. If you mark
more than one answer, it will be counted wrong.
• Unless a question involves determining whether given C++ code is syntactically correct, assume that it is.
The given code has been compiled and tested, except where there are deliberate errors. Unless a question
specifically deals with compiler #include directives, you should assume the necessary header files have
been included.
• Note that questions about printed values disregard formatting completely.
• In questions/answers which require a distinction between integer and real values, integers will be
represented without a decimal point, whereas real values will have a decimal point, [1044 (integer), 1044.0
(real)].
• When you have completed the test, sign the pledge at the bottom of this page and turn in the test and your
Opscan.
• This is a closed-book, closed-notes examination. No calculators or other electronic devices may be
used during this examination. You may not discuss (in any form: written, verbal or electronic) the
content of this examination with any student who has not taken it. You must return this test form
when you complete the examination. Failure to adhere to any of these restrictions is an Honor Code
violation.
• There are 40 multiple-choice questions and 1 extra credit question.
• Mark your answers on the test form, for future reference, and on the Opscan. The answers you mark on the
Opscan form will be considered your official answers.

Do not start the test until instructed to do so!

Name ID
print name

Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.

Signature
1. Which of the following represents the correct logic for a read-until-input-failure
loop?
a. while ( input succeeded )
{
// read next data item
// process last data read
}

b. // read first data item


while ( input succeeded )
{
// process last data read
}

c. while ( input succeeded )


{
// process last data read
// read next data item
}
d. // read first data item
while ( input succeeded )
{
// process last data read
// read next data item
}
e. 1 and 3 only
f. 2 and 4 only
g. None of these

2. How does the computer store the letter ‘s’?


a. In binary format
b. In ASCII representation
c. The letter is mapped onto some number and this number is stored
d. All of the above

3. What is the output of this expression?


1.0 / 2 + 3
a. 3.0
b. 3.5
c. 0.2
d. 1
e. Not allowed
f. None of the above

4. What is the value stored in the integer variable intVar?


int intVar = 24.2 / 2;
a. 12
b. 12.1
c. 13
d. 24
e. None of the above

5. If the input file looks like:


10 20 30 c++ 40
23 12 11 java 12
34 11 34 c 12
66 34 12 prolog 2

What is the value of the variables “language”, “cost1”, “cost2”,


“cost3”,”cost4” after executing the following code segment?
int cost1,cost2,cost3,cost4;
string language;
for(int i=0;i<2;i++)
inFile.ignore(INT_MAX,’\n’);
inFile >> cost1 >>cost2 >> cost3>> language >> cost4;

a. c++, 10, 20,30 and 40


b. java, 23, 12, 11 and 12
c. prolog, 66, 34,12 and 2
d. c, 34, 11, 34 and 12
e. None of the above

6. What is wrong with the following code fragment?

int main()
{
int x=4,y=3; // line 1
const int i = 10; // line2

x = x + i; // line 3
i = y*2; // line 4
}
a. Variables are not declared before use
b. There cannot be 3 variables in such a simple program
c. There is an error in line 3
d. There is an error in line 4
e. Both c and d

7. If the input file looks like:


Name<tab>age<tab>address,
Which of the following statements reads in the complete name of the person
properly assuming that Name corresponds to first and last name separated by a
space?
a. InFile >> name;
b. getline(inFile, name);
c. getline(inFile, name, ‘\t’);
d. a and b only
e. b and c only
f. a, b and c

8. When the following statement is executed:


string name;
InFile >> name;
and with the input from the file being,
Bjarne Stroustroup is the inventor of c++
What is the contents of the variable “name”?

a. Bjarne Stroustroup is the inventor of c++


b. Bjarne Stroustroup
c. Bjarne
d. None of the above

[Questions: 9- 13] If the input file looks as (This is the complete file – the file ends
after this information.)
12.34 GGG23

and the following code was executed,


int a,b,c;
char c1;
string str;
inFile >> a >> c1 >> b >> str >>c;
9. What is the value stored in variable a?
a. 12.34
b. 12
c. 1
d. 12.
e. None of the above

10. What is the value stored in variable c1?


a. 1
b. 2
c. .
d. 12.34
e. 3
f. G
11. What is the value stored in variable b?
a. 12
b. 34
c. 12.34
d. None of the above

12. What is the value stored in variable str?


a. GGG
b. G
c. GGG23
d. 12.34 GGG
e. None of the above

13. What is the value stored in variable c?


a. 23
b. GGG23
c. Junk – The stream has already fails
d. 12
e. 34
f. None of the above

[Questions: 14,15]
For questions 14 and 15, consider execution of the following switch statement:
int Enter = 10;
cin >> Enter;
switch (Enter)
{
case 1: Enter = -4;
case 2: Enter = -6;
case 4: break;
case 6: Enter = -8;
break;
default: Enter = -1;
}

14. What would the value of Enter be after execution of this code if the value read for
Enter were 4?
a. –4
b. –6
c. –8
d. –1
e. 10
f. None of these
15. What would the value of Enter be after execution of this code if the value read for
Enter were 1?
a. –4
b. –6
c. –8
d. –1
e. 10
f. None of these

16. What is the logical condition under which the following while loop will
terminate?
int Beta = 5;
while (Beta >= 0 && Beta < 10)
{
cout << Beta << endl;
cin >> Beta;
}

a. Beta <0 &&Beta >= 10


b. Beta <= 0 && Beta > 10
c. Beta <0 ||Beta > 10
d. Beta <= 0 || Beta >= 10
e. Beta <0 ||Beta >= 10
f. None of these

17. When designing a specification for an input file, which of the following would be
a poor choice for a sentinel value?
a. a value of 29 for voter ages
b. a value of 1025 for SAT scores
c. a value of -1 for student heights
d. a value of "No one" for student names
e. All of them
f. a and b only
g. a and c only
h. b and c only
i. c and d only
j. None of these

[Questions 18-21] Which of the following can be used to describe the following
identifiers?

18. Area Of Circle - b (No spaces allowed)


19. AreaOfCircle -a
20. 23QW - b (Identifiers cannot begin with a digit)
21. case - b (Reserved Words cannot be identifiers)

a. Valid and good


b. Invalid
c. Valid and not good
d. None of the above
e. Both a and b

22. Any line in your program beginning with a # symbol is information given to a ___
a. Compiler
b. Operating system
c. Preprocessor
d. RAM
e. None of the above

23. What is wrong with the following piece of code fragment?


string name;
name = “John”; // line 2
switch(name)
{
case “Smith”: cout << “smith”;
break;
case “John”: cout <<”john”;
break;
}

a. Default statement is missing


b. Strings cannot be used with switch statement
c. Semi colon is missing at the end of the switch statement
d. There is an error in line 2
e. None of the above

24. What is the value of temp1 and temp2 after executing the function sum?
void sum(int,int&);
int main()
{
int temp1=5, temp2 = 23;
sum(temp1,temp2);
}

void sum(int x, int& y)


{
y = y+x;
}
a. 5 and 28
b. 28 and 23
c. 23 and 5
d. 5 and 23
e. 23 and 23
f. None of the above

25. Which of the following is true?


a. Actual parameters are present in the call to the function and Formal
parameters are present in the definition of the function
b. Formal parameters are present in the call to the function and the actual
parameters are present in the definition of the function
c. Actual and formal parameters are both present in the call to the function
d. Actual parameters are present in the function declaration and formal
parameters are present in the function definition

[Questions 25 - 28] The following code fragment uses a function sum to calculate the
sum of two numbers.

void sum(int, const int&, int& );


int main()
{
int a = 5;
int b = 10;
int result;

sum(a,b,result);
}
void sum(int a, const int& b, int& c)
{
c = a+b;
}

26. The actual parameters to the function sum are


a. a , b and result
b. a, b and c
c. a and b
d. result
e. a and result
f. None of the above

27. Which of the parameters are passed by constant reference?


a. a
b. b
c. result
d. None of the above

28. Which of the parameters is passed by value?


a. a
b. b
c. c
d. result
e. None of the above

29. For the following code fragment, how many times does sum get incremented? In
other words how many times is line X executed in the following program?

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
sum++; // line X
}
}
a. 4
b. 3
c. 9
d. 2
e. None of the above

30. What kind of a loop is this?

while(inFile && data != “end of data”)


{
// perform some work here.
}
a. Sentinel controlled loop
b. End of file controlled loop
c. Hybrid between sentinel controlled and end of file controlled loop
d. Count controlled loop
e. None of the above

31. What is the value printed for variable delta if the following code is executed?

int delta =0, x=3;


if(x%2 == 1)
delta = delta + x;
x++;
if(x%2 == 0)
delta = delta + x;
x++;
if(x%2 == 0)
delta = delta +x;

cout << delta;

a. 0
b. 1
c. 2
d. 6
e. 4
f. 7

32. What is the output of the following code fragment?


int main()
{
string str=”John Smith”;
cout << str.length();
}
a. 9
b. 10
c. 4
d. 12
e. 3

33. How many times is the body of the loop executed?


int i=1;

while(true)
{
cout << i;

if(i==5)
break;

i++;
}

a. Forever
b. 5 times
c. 10 times
d. 0 times
e. None of the above

34. What does the following piece of code print?


for(int i=0;i<3;i++)
{
for(int j=5;j<7;j++)
{
cout << ‘*’;
}
cout <<endl;
}
a. **
**
**

b. ***
***

c. ******

d. *
**
***

35. What is stored in the variable smallerName?

string name1= “John Smith”;


string name2 = “John Doe”;

bool smallerName = (name1 < name2);

a. True
b. False
c. Junk
d. None of the above

36. If I wanted to execute some piece of code based on what range my angle variable
is, which of the following statements would I use?
a. if statement
b. switch statement
c. while statement
d. for statement
e. None of the above

37. What is the value stored in c?


int a=4,b=9,c;

c = a++ + ++b;
a. 13
b. 14
c. 15
d. 12
e. None of the above

[Questions38 - 40] consider the following code fragment:


void someFunction();
int main()
{
int a=5;
int i=0;
while(i<5)
{
int a = 4;
cout << a; // line X
i++;
}
cout << a; // line Y
someFunction();
}
void someFunction()
{
int a = 10
cout << a; // line Z
}

38. What is output on line X?


a. 4
b. 5
c. 10
d. junk

39. What is output on line Y?


a. 4
b. 5
c. 10
d. junk
e.
40. What is output on line Z?
a. 4
b. 5
c. 10
d. junk
[Extra Credit]

Match the following:

1. Processor a. Part that runs executable program


2. RAM – main memory b. Part that stores files permanently
3. Hard disk c. Part that stores instructions and data
temporarily for the program to use
4. Compiler d. Part that translates your program to an
executable program

a. 1 - > a; 4->b; 3->c; 2->d


b. 2 -> a; 3->b; 4->c;1->d
c. 1->a; 2->c; 3->b;4->d
d. 2->c;3->b;1->a;4->d
e. None of the above

//******* c or d is correct *******************//

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy