Upload
iium
View
585
Download
0
Embed Size (px)
Citation preview
INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA
MID-TERM EXAMINATION
SEMESTER 2, 2014/2015 SESSION
KULLIYYAH OF INFORMATION AND COMMUNICATION TECHNOLOGY
Programme : BIT Level of Study : Undergraduate
Time : 10.00 a.m. – 12.15 p.m Date : 13th
March 2015
Duration : 2 Hours and 15 Minutes
Course Code : CSC1100 Section(s) : All
Course Title : Elements of Programming
This Question Paper consists of 17 Printed Pages including cover page containing 4 sections
Section 1: Multiple-Choice (10 questions)
Section 2: Fill-in-the-blank (10 Questions)
Section 3: Short Answers (3 Questions)
Section 4: Programming (1 Question)
You are required to answer ALL questions.
You are allowed to bring a calculator into the exam hall.
INSTRUCTION(S) TO CANDIDATES
DO NOT OPEN UNTIL YOU ARE ASKED TO DO SO
ANSWER ALL QUESTIONS IN THIS QUESTION PAPER
YOU ARE ONLY ALLOWED TO ASK QUESTIONS DURING THE FIRST 15
MINUTES OF THE EXAMINATION PERIOD
Any form of cheating or attempt to cheat is a serious offence, which may lead to dismissal.
Name:
Matric No:
Section:
Lecturer’s Name:
Page 2 of 16
SECTION 1: Multiple-Choice
Circle the correct answer in this question paper [1 marks x 10 = 10 marks].
1. Which of the following is NOT TRUE about C++ ?
a. C++ is an extension of the C language
b. C++ is a low-level programming language
c. C++ is an object-oriented language
d. C++ is a high-level programming language
2. When a declaration statement is used to store a value in a variable, the variable is said to
be ______.
a. created c. initialized
b. declared d. referenced
3. The C++ statement ‘cout << (6 + 15);’ yields the result ____.
a. 6 + 15 c. (6 + 15)
b. 21 d. (21)
4. A data ____ is defined as a set of values and a set of operations that can be applied to
these values
a. type c. base
b. set d. dictionary
5. What will be the value of the variable n4 after running the following codes?
int main ()
{
int n1 = 3, n2 = 4, n4;
double n3 = 2.2;
n4 = (n1/n2) * n3 + abs(n1-n2);
cout << double(n4) << endl;
return 0;
}
a. 2.65 c. 1
b. 0.65 d. 0
Page 3 of 16
6. The use of default in a switch-case statement is to _______
a. exit from the program
b. exit from the case structure
c. cover unhandled possibilities
d. continue with the remaining case conditions
7. Which of the following is a valid call to a C++ cmath function?
a. sqr(9.0) b. ab(-y) c. exp(2.5) d. power(x,2)
8. What will be the output of the following code?
int main ()
{
int num = -5;
bool sign = false;
for(int i = 1; i <= 4; i++)
{
if (sign && num > 0) {
cout << ++num << endl;
break;
}
else if (!sign || num < 0) {
cout << --num ;
continue;
}
else
cout << num++ ;
}
return 0;
}
a. -4 c. -5-4-3-2
b. -5-6-7-8
d. -4-3-2-1
9. If an expression is false to begin with, then !expression is true and evaluates to a
value of ____.
a. 0 c. 2
b. 1 d. 3
Page 4 of 16
10. Using nested if statements without including _______ to indicate the structure is a
common programming error.
a. parentheses c. braces
b. quotation marks d. switches
SECTION 2 : Fill-in-the-blank
Choose the correct term from the box given below and complete each of the following
statement. Write your answers in the blank column next to each statement .
[1 marks x 10 = 10 marks]
namespace main( ) robust compound pre-processor
sentinel pseudocode relational compiler
loader class algorithm sizeof( )
i. _______ is the file accessed by compiler when looking
for prewritten classes or functions pre-processor
ii. A _______program detects and responds effectively to
unexpected user inputs. robust
iii. The ______ operator determines the amount of storage
reserved for a variable. sizeof( )
iv. An _______ is a procedure or step by step sequence of
instructions to perform a computation in a program algorithm
v. A _______ places programs into main memory for
execution. loader
vi. In programming, a _______ is a special value that can be
used as a flag or signal to start or end a series of data. sentinel
vii. One or more individual statements enclosed within
braces is also known as a ______ statement. compound
viii. _________________ expressions are used to compare
operands. relational
ix. In object-oriented programming, a _______ is a set of
objects that share similar attributes. class
x. _______ forces the conversion of a value to another type casting
Page 5 of 16
SECTION 3: Short Answer Questions
Answer all questions in the spaces provided.
[40 marks]
Question 1: Short Answers
i. A source program, cannot be executed until it is translated into machine language. Draw
a diagram that demonstrates the process of program translation.
[2 marks]
ii. List down two important rules for evaluating arithmetic expressions.
[2 marks]
Both operands are integers: result is integer
One operand is floating-point: result is floating-point
iii. What is a symbolic constant? Provide a C++ example declaration of a symbolic constant
of type double.
[2 marks]
Symbolic constants are identifiers whose values cannot be changed throughout the
program.
Example:
const PI = 3.142 OR
#define PI 3.142
iv. Describe the meaning of an ‘infinite loop’ and a ‘fixed count loop’ in repetition.
[2 marks]
- An infinite loop is a loop with an expression that is always true and never ends.
- A fixed count loop is a loop with a tested expression which is a counter that
checks for a fixed number of repetitions.
Page 6 of 16
v. Explain with examples the three (3) types of logical operators in C++.
[5 marks]
AND operator, &&:
Compound condition is true (has value of 1) only if both conditions are true
Example: (age > 40) && (term < 10)
OR operator, || (double vertical bar):
Compound condition is true if either one of the expressions is true or if both
conditions are true
Example: (age > 40) || (term < 10)
NOT operator, !:
Changes an expression to its opposite state
If expression is true, then !expression is false
Question 2: Output Identification
Determine the outputs for the following code segments (assume all necessary predecessor
commands have been defined).
i. For the following codes, assume that the number entered is 42339.
[5 marks]
int main()
{
int num;
cout << "Enter a five-digit number: ";
cin >> num;
cout << num / 10000 << " ";
num = num % 10000;
cout << num / 1000 << " ";
num = num % 1000;
cout << num / 100 << " ";
num = num % 100;
cout << num / 10 << " ";
num = num % 10;
cout << num << endl;
return 0;
}
Page 7 of 16
ii. What is the printout of the following switch-case statement?
[3 marks]
int main()
{
int val = 22;
switch(val%3)
{
case 2: cout<< "Good, ";
break;
case 1: cout<< "Better, ";
case 0: cout<< "Best, ";
default: cout<< "Thank you!";
}
return 0;
}
4 2 3 3 9
Output:
Better, Best, Thank you!
Output:
Page 8 of 16
i. What is the output for the following codes?
[3 marks]
int main()
{
int x = 9, y=11;
if ( x < 10 )
if ( y > 10 )
cout << "%%%%" << endl;
else
cout << "#####" << endl;
cout << "$$$$$" << endl;
return 0;
}
ii. Determine the output of the following codes.
[4 marks]
int main()
{
int x, y;
for(x = 5; x >= 1; x--)
{
for(y = 6; y > x; y--)
{
cout << x + y;
}
cout << char('a' + 2) << endl ;
}
return 0;
}
%%%%
$$$$$
Output:
Page 9 of 16
Question 3: Find and correct the error(s) in the following programming statements:
i.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main ()
{
int a = 1; b = 2;
double c;
c = (a + b) * 2 + (a - b)/2;
cout >> c >> endl;
return 0;
}
[3 marks]
11c
109c
987c
8765c
76543c
Output:
Line No. Correction 5 int a=1, b=2;
9 cout << c << endl;
Page 10 of 16
ii.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
int a, b, c;
cout << "Enter 3 numbers: ";
cin >> a >> b >> c;
if ( c * c = a * a + b * b )
cout << The are 3 numbers
<< endl;
if else
cout << "THERE ARE 3 NUMBERS"
<< endl;
return 0;
}
[3 marks]
iii.
1 #include <iostream>
2
3 using namespace std;
4 int main()
5 {
6 float average;
7 average = 45/2.0;
8 cout << setw[6] << setfill('*') << average << endl;
9
10 if (average = 22.5)
11 cout << setiosflags(ios::showpoint) << average
12 << endl;
13 cout << setprecision(2) << average << endl;
14 return 0;
15 }
Line No. Correction
6 if ( c * c == a * a + b * b )
7 cout << "The are 3 numbers" 9 else
Page 11 of 16
[6 marks]
SECTION 4: Programming Question
Answer all questions in the spaces provided.
[25 marks]
Question 1:
In Japan, vending machines are not only used to buy drinks or snacks, but also to buy books or
magazines. You are required to create a C++ program that acts as vending machine that is able
to dispense novels, magazines and newspapers. Each category of item consists of 3
different titles to be represented by any characters between a-z (ie., name the titles as ‘a’, ‘b’,
‘c’, etc….). Given that the initial number of items in all categories is 9 units (3 units for each
item), use the ‘while’ and ‘switch-case statements to perform the following:
a. Draw a simple flow chart for your program (refer to the relevant symbols in the
attachment)
b. Your program should prompt the user to enter the amount of items they want to
purchase (e.g, entering 5 means user can buy 5 items in any combination of categories)
and display the titles selected (you may just use a cout for titles).
c. Your program should calculate the total price to be paid for all items purchased by
the user at the end of the program.
Line No. Correction
2 #include <iomanip>
6 cout << setw(6) << setfill('*') << average << endl;
10 if (average == 22.5) {
13 cout << fixed << setprecision(2) << average << endl; }
Page 12 of 16
d. Once an item in each category is purchased, the number of items will decrease by one.
Your program should be able to print the balance of each category of item at the end of
the program.
The following is the sample output of your program:
Please enter number of items: 2
Item 1
Choose the type of item (1 – novel, 2 – magazine, 3 – newspaper) : 1
Enter title : ‘a’
You have selected the novel “In the eyes of the night”
The price of this item is RM 10.
Item 2
Choose the type of item (1 – novel, 2 – magazine, 3 – newspaper) : 3
Enter title : ‘z’
You have selected the newspaper “The New Straits Time”
The price of this item is RM 2.50.
Total price of all items is RM 12.50
Balance for novel is 2
Balance for newspaper is 2
Press any key to continue . . .
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Page 13 of 16
Flowchart (Simplified)
Start
Input
numItem
Compute
balance = balance-1
count < numItem
Input
category
category is
1 or 2 or 3
title between
‘a’-‘z’
Input title
Compute
totalPrice = totalPrice + price
End
Display totalPrice,
balance
true
false
false
false
true
true
Display
title, price
Page 14 of 16
//Solution Midterm Exam (S4)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int limit,count;
bool select1=false,select2=false,select3=false;
double totalp = 0.0,p_novel,p_mag,p_news,priceCat = 0.0;
int balance1 = 3, balance2 = 3, balance3 = 3;
int category;
char title;
cout << "************************************************" << endl;
cout << "Books & Magazines Vending Machine\n " << endl;
cout << "Please enter the number of items you wish to purchase: " ;
cin >> limit;
count = 1;
while (count <=limit)
{
cout << "\nItem " << count << endl;
cout << "Please enter the category of item to purchase\n" ;
cout << "Press '1' for novels\n" ;
cout << "Press '2' for magazines\n" ;
cout << "Press '3' for newspapers\n\n" ;
cin >> category;
switch(category)
{
case 1:
cout << "You have selected 1 for novels\n";
cout << "Select 'a' for : In the eyes of the night\n";
cout << "Select 'b' for : I love Programming, not!\n";
cout << "Select 'c' for : Exam, exam, go away...\n";
cin >> title;
select1 = true;
switch(title)
{
case 'a': case 'A':
p_novel = 10.00;
cout << "You have selected the novel 'In the eyes "
<< "of the night'\n";
cout << "The price of the novel is RM " << fixed
<< setprecision(2) << p_novel << endl;
break;
case 'b': case 'B':
p_novel = 12.00;
cout << "You have selected the novel 'I love "
<< "Programming, not!' " << endl;
Page 15 of 16
cout << "The price of the novel is RM " << fixed
<< setprecision(2) << p_novel << endl;
break;
case 'c': case 'C':
p_novel = 8.50;
cout << "You have selected the novel 'Exam, exam, "
<< "go away...' " << endl;
cout << "The price of the novel is RM " << fixed
<< setprecision(2) << p_novel << endl;
break;
default:
cout << "Wrong selection, please try again..."
<< endl;
continue;
}//inner switch1
balance1 = balance1 - 1 ;
priceCat = p_novel;
break;
case 2:
cout << "You have selected 2 for magazines\n";
cout << "Select 'j' for : Home Improvement\n";
cout << "Select 'k' for : Cooking with Madam\n";
cout << "Select 'l' for : Programming Bits and Bytes\n";
cin >> title;
select2 = true;
switch(title)
{
case 'j': case 'J':
p_mag = 5.00;
cout << "You have selected the magazine 'Home "
<< "Improvement' " << endl;
cout << "The price of the magazine is RM "
<< fixed << setprecision(2) << p_mag << endl;
break;
case 'k': case 'K':
p_mag = 7.00;
cout << "You have selected the magazine 'Cooking "
<< "with Madam' " << endl;
cout << "The price of the magazine is RM "
<< fixed << setprecision(2) << p_mag << endl;
break;
case 'l': case 'L':
p_mag = 6.50;
cout << "You have selected the magazine "
<< "Programming Bits and Bytes' " << endl;
Page 16 of 16
cout << "The price of the magazine is RM "
<< fixed << setprecision(2) << p_mag << endl;
break;
default:
cout << "Wrong selection, please try again..."
<< endl;
continue;
} //inner switch2
balance2 = balance2 - 1;
priceCat = p_mag;
break;
case 3:
cout << "You have selected 3 for newspapers\n";
cout << "Select 'x' for : Berita Harian\n";
cout << "Select 'y' for : New Straits Time\n";
cout << "Select 'z' for : Utusan Malaysia\n";
cin >> title;
select3 = true;
switch(title)
{
case 'x': case 'X':
p_news = 1.50;
cout << "You have selected the newspaper 'Berita "
<< "Harian' " << endl;
cout << "The price of the newspaper is RM "
<< fixed << setprecision(2) << p_news
<< endl;
break;
case 'y': case 'Y':
p_news = 2.00;
cout << "You have selected the newspaper 'The New "
<< "Straits Time' " << endl;
cout << "The price of the newspaper is RM "
<< fixed << setprecision(2) << p_news
<< endl;
break;
case 'z': case 'Z':
p_news = 1.80;
cout << "You have selected the newspaper 'Utusan "
<< "Malaysia' " << endl;
cout << "The price of the newspaper is RM "
<< fixed << setprecision(2) << p_news << endl;
break;
default:
cout << "Wrong selection, please try again..."
<< endl;
Page 17 of 16
continue;
} //inner switch 3
balance3 = balance3 - 1;
priceCat = p_news;
break;
default:
cout << "Wrong selection, please try again..." << endl;
continue;
} //outer switch
totalp = totalp + priceCat;
count++;
} // end while
cout << "\nThe total price for your items is: " << fixed
<< setprecision(2) << totalp << endl ;
if (select1 == true)
cout << "Balance for novels is now: " << balance1 << endl;
if (select2 == true)
cout << "Balance for magazines is now: " << balance2 << endl;
if (select3 == true)
cout << "Balance for newspapers is now: " << balance3 << endl;
cout << endl;
return 0;
}
- End of question –