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

Test 1 CS111 Solution Ver 1

Uploaded by

Rahul Narayan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
60 views13 pages

Test 1 CS111 Solution Ver 1

Uploaded by

Rahul Narayan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

The University of the South Pacific

School of Computing, information and Mathematical


Sciences

CS111: Introduction to Computing

Short Test 1, Semester 1, 2020

Time Allowed: 50 minutes

Total Marks: 30 (10% of final grade)

Paper Version 1

Name: ________Solution__________________

Student ID: ______________________


Campus: _______________________

The test is composed of two sections with an overall test score of 30 Marks. You will have 50
minutes to complete the test.

Section A: Multiple Choice (20 Marks)

There are 5 multiple choice questions with one point awarded for each correct choice.
Use the grid provided on the following page to circle the answer corresponding to correct choice.
Each question has exactly one correct answer.

Section B: Short Answer Questions (10 Marks)

Write C++ code for the problem given. Answers written with pencils will not be accepted.

NOTE:
This test covers two learning outcomes:
1. Examine code for its syntax and semantic validity.
2. Identify the functionality of existing codes.

Page 1
Multiple Choice Answering Grid

1 A B C D E 16 A B C D E

2 A B C D E
17 A B C D E

3 A B C D E
18 A B C D E

4 A B C D E
19 A B C D E
5 A B C D E
20 A B C D E
6 A B C D E

7 A B C D E

8 A B C D E

9 A B C D E

10 A B C D E

11 A B C D E

12 A B C D E
Total Marks:

13 A B C D E

14 A B C D E

15 A B C D E

Page 2
Section A: Multiple Choice
Part 1: Introduction

1. What does CPU stand for?


A. Central Pacific University
B. C++ Programming Unit
C. Central Processing Unit
D. Core Program Uni cation
E. Central Programming Union

2. Which are typical steps in software development:

A. (1) Analysis of the design, (2) Synthesis of thesis and antithesis, and (3) Dialysis of
an analgesic solution.
B. (1) Testing of the product, (2) Experimenting with input, (3) Checking of output and
(4) Wrapping the product.
C. (1) Editing the machine code, (2) Saving the left, (3) Compiling to pseudo code, and
(4) Running the source code.
D. (1) Understanding the problem, (2) Developing an algorithm, (3) Testing the
algorithm, (4) Translating the algorithm to a program, (5) Compiling and testing the
program.
E. (1) Creation of product specification (2) Review of product specification (3)
Selection of product developer (4) Development of prototype (5) Hardware review of
product (6) Manufacture of product.

3. What is the main function of a compiler?

A. It checks source code for semantic errors.


B. It detects runtime errors in programs.
C. It corrects syntax errors in C++ code.
D. It warns about missing test cases.
E. It translates high-level code to machine code.

4. What is a Universal Turing Machine?

A. The first digital computer build by NASA in the 1940s.


B. A hypothetical device that defines the concept of computer and program.
C. A super computer by IBM, capable to play chess at world championship level.
D. A mechanical computing device invented by Charles Babbage in the 1820s.
E. The Algorithmic and Logic Unit (ALU) found on most modern computers.

5. Consider the following two figure:

0001 0001 0011 1011 0100 1010 1001 1110 int x = 0;


0010 0110 0111 1010 0000 0100 1011 1011 if ( number > 0)
0011 0101 1101 1111 1010 0110 0001 1101 x = 2 * number;
0100 0000 1011 0010 1011 1111 1110 0010 return 0;

Which statement is correct:


A. The left figure depicts machine code, the right figure high-level code.
B. The left figure depicts object oriented code, the right figure byte code.
C. The left figure depicts compiled code, the right figure assembly code.
D. The left figure depicts C++ code, the right figure Java code.
E. The left figure depicts test code, the right figure production code.

Page 3
Part 2: Fundamentals of Data Types

6. Suppose you want to write a program that reads in body temperatures, like 36:9, 37:5, or
38:1. Which variable type do you take?

A. Type short
B. Type int
C. Variable body temperature
D. Type char
E. Type double

7. What is the problem with the following piece of code.

int x = 0;
int y;
cout << "Please enter x";
cin >> x;

if (x > 0)
x = y / 2;

A. The variable declaration int x = 0; is incorrect, and will not compile.


B. Variable y is not initialized in line x = y / 2 .
C. Variable x is undeclared in line if (x > 0).
D. There is an unintended integer division in line x = y / 2.
E. There is a possible division by zero in line x = y / 2.

8. Which of the following variable declarations is syntactically correct, i.e. it will compile:

A. long number = 5 ;
B. float return = 6;
C. unsigned string firstname;
D. int score == 7;
E. real diameter = 3.14;

9. How many lines of output will the following program code print?

cout << " This program statements demonstrate the use of \n and << endl "
"in C++ programs." << endl << " In \\n the ’\\’ means the next character"
" has a special meaning hence \n means a newline. \n The command ’endl’ on the ";
cout << " other hand also means newline.\n When used it instructs the computer"
<< "to print the second\n "
"instruction on a newline. Therefore any string after \"<< endl\" "
"or \n written within the string will be printed on a new line.";

A. 2
B. 5
C. 8
D. 10
E. 12

Page 4
10. What is the output of this code snippet:

int number = 2;
int counter = 3;
int result = number/counter;
cout << "The result is " << result << endl;

A. The result is 0.666667


B. The result is 0
C. The result is 1
D. The result is 2/3
E. The program will not compile, because of an unintended integer division.

Part 3: Decisions

11. Consider the following code snippets:

A. B.
if(volume == 100){ if(number = 100){
apx = volume * 3.14; average = number / NUM_ENTRIES;
} number --;
else{ }
apy = volume / 2.79; else
} average = number / (NUM_ENTRIES +1);
volume++; number = 101;

C. D.
if(degree =< MAX_DEGREE;){ if(volume > MIN_VOLUME){
temperature = degree * FACTOR; length = volume / diameter;
}; }
else{ else{
temperature = degree; length = diameter;
degree = MAX_DEGREE; }
} cout << "The length is " << length;

Which of these code snipptes


• is syntactically correct and compiles, and
• does not use magic constants, and
• uses proper layout and variable names, and
• uses correct comparisons (relational operators) as conditions.

A. Code snippet (A).


B. Code snippet (B).
C. Code snippet (C).
D. Code snippet (D).
E. Snippet (A), (B), (C), and (D).

Page 5
12. Suppose you want to check if score is not equal to 42, which condition is correct?

A. score !== 42
B. score >= 42
C. score =! 42
D. !score == 42
E. score != 42

13. Assuming that a user enters 10, 20, and 30 as input values one after another, what is the
output of the following code snippet?

int main()
{
int num1, num2, num3;
int result=0;

cout << "Enter a number: ";


cin >> num1;
cout << "Enter a number: ";
cin >> num2;
cout << "Enter a number: ";
cin >> num3;

if (num1 < num2)


{
if (num1 < num3)
{
result = 3 * num1;
}
else
{
result = num2 + num3;
}
}

result = result + num1;


cout << "The result is " << result << endl;

system("pause");
return 0;
}

A. The result is 10
B. The result is 20
C. The result is 30
D. The result is 40
E. The result is 50

Page 6
14. Consider the following code snippets:

I. II.
int mark = 20; int mark = 20;
int score = 30; int score = 30;

if(mark >= 10){ if(mark >= 10){


if(mark >=20){ score = 17;
score = 21; }
} else
else{ if(mark >=20){
score = 17; score = 21;
} }
} else
else{ if(mark >=7){
if(mark >=7){ score = 8;
score = 8; }
} else{
else{ score = 4;
score = 4; }
}
}
cout << "The score is " << score; cout << "The score is " << score;

Which of the following is true:

A. Both have as output The score is 17.


B. Both have as output The score is 21.
C. Snippet I has as output The score is 21, and snippet II has as output The score is
17.
D. Snippet I has as output The score is 17, and snippet II has as output The score is
21.
E. Both have as output The score is 30.

15. Suppose you defined bool is_student = true; and bool is_local = true;

Which of the following boolean expressions evaluates to true?

A. (is_student && !is_local)


B. (!is_student && is_local)
C. !(is_student || is_local)
D. (!is_student || !is_local)
E. (is_student || !is_local)

Page 7
Part 4: Loops

16. Consider the following code snippets:

B. C.

for ( int i = 0 ; i <= 3 ; i++ ) for ( int i = 0 ; i < 3 ; i++ )


{ {
cout << "Iteration: " << i << endl; cout << "Iteration: " << i << endl;
} }

C. D.

for (int i = 1 ; i < 3 ; i++ ) for( int i = 1 ; i <= 3 ; i++ )


{ {
cout << "Iteration: " << i << endl; cout << "Iteration: " << i << endl;
} }

Which of these code snippets has as output:

Iteration 0
Iteration 1
Iteration 2

A. Code snippet (A).


B. Code snippet (B).
C. Code snippet (C).
D. Code snippet (D).
E. None of the above.

17. Consider the following code snippet:

int number = 3;
while(number < 5){
if(number > 2){
break;
}
number = 7;
}

cout << "The number is " << number << endl;

Page 8
What is the output?

A. The number is 2
B. The number is 3
C. The number is 5
D. The number is 6
E. The number is 7

18. Consider the following code snippets:

(I) (II)
int element = 0; int element = 0;
while (element < 3) while (element <= 3)
{ {
element = element + 3; element = element + 3;
} }
cout << "This is element " << element; cout << "This is element " << element;

(III) (IV)
int element = 0; int element = 0;
do{ do{
element = element + 3; element = element + 3;
} }
while(element < 3); while(element <= 3);
cout << "This is element " << element;
cout << "This is element " << element;

Which of these code snippets has as output “This is element 3"?

A. Code snippet (I) and (II).


B. Code snippet (I) and (III).
C. Code snippet (II) and (III).
D. Code snippet (II) and (IV).
E. Code snippet (III) and (IV).

19. Considering the three types of Loops; while loop, for loop and do while loop, Which of the
following statements is incorrect?

A. All three types of loops { while, for, and do } continue with the loop body, if the
condition is true.
B. The body of a do loop will be executed at least once.
C. The body of a while loop may not be executed at all.
D. A for loop first initializes, then checks the condition, then updates, and then
executes the body.
E. The for loop will update at the end of a loop, and then check the condition.

Page 9
20. A programmer wants to ask if the user is male or female. For male the user type “M" for
male, and “F" for female. The programmer wants to validate the input, which means, ask
again if the user does not type “M" or “F". What is wrong with the following program?

#include <iostream>

using namespace std;

int main()
{
char response;

cout << "Please enter \‘male\’ [M] or \‘female\’ [F]: ";


cin >> response;

while(response != ‘M’ || response != ‘F’){


cout << "Sorry. Please enter \"M\" or \"F\": ";
cin >> response;
}

cout << "Your response was " << response ;


return 0;
}

A. The loop is infinite, and the program will never stop.


B. The program will never print "Sorry. Please enter "M" or "F".
C. The program will print exactly once "Sorry. Please enter "M" or "F".
D. The program has a syntax error and will not compile.
E. The program is correct.

Page 10
Section B. Short Answer Questions - Alice and Bob (10 marks)

Alice and Bob play a number guessing game. Bob thinks of a number between 1 and 15.

 Alice’s first guess is 8. Bob replies “My number is higher".


 Alice’s second guess is 12. Bob replies “My number is lower".
 Alice’s third guess is 10. Bob replies “My number is lower".
 Alice’s fourth guess is 9. Bob replies “My number is lower".
 Alice’s calls Bob a cheat.

a. Has Alice good reasons to call Bob a cheat? What is a good reason? (2 marks)

Yes bob is a cheat. There exists no number that meets this


specifications both lower than 10 and higher than 9.

b. Assuming that you want to create a computer program that will play the role of Bob
without cheating. The program will randomly choose a number between 1 and 15 and ask
the user to guess that number. The user will be given 5 tries at guessing the number.
i. What will be the input(s) to this program? Declare appropriate variable(s) for the
input(s) identified. (2 marks)

int number = 0

deduct 1 mark if more than one input is createed


data-type should be integer (0.5 marks)
deduct 0.5 marks if variable not initialised

Page 11
ii. Draw a flow chart that will model the program that you have to create. (6 marks)

Page 12
//extra sheet

End of Exam 

Page 13

You might also like