SlideShare uma empresa Scribd logo
1 de 18
Baixar para ler offline
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 Machinen " << 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 purchasen" ;
cout << "Press '1' for novelsn" ;
cout << "Press '2' for magazinesn" ;
cout << "Press '3' for newspapersnn" ;
cin >> category;
switch(category)
{
case 1:
cout << "You have selected 1 for novelsn";
cout << "Select 'a' for : In the eyes of the nightn";
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 magazinesn";
cout << "Select 'j' for : Home Improvementn";
cout << "Select 'k' for : Cooking with Madamn";
cout << "Select 'l' for : Programming Bits and Bytesn";
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 newspapersn";
cout << "Select 'x' for : Berita Hariann";
cout << "Select 'y' for : New Straits Timen";
cout << "Select 'z' for : Utusan Malaysian";
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 –
Page 18 of 16
Attachment (Symbols for flow chart)

Mais conteúdo relacionado

Mais procurados

Intro perolehan
Intro perolehanIntro perolehan
Intro perolehan
aimm reka
 
Laporan kewangan
Laporan kewanganLaporan kewangan
Laporan kewangan
ArDah ZyEd
 
Modul p&p nombor dan operasi tahun 2
Modul p&p nombor dan operasi tahun 2Modul p&p nombor dan operasi tahun 2
Modul p&p nombor dan operasi tahun 2
Nurul Akmal Ab Aziz
 
Manual prosedur pk1
Manual prosedur pk1Manual prosedur pk1
Manual prosedur pk1
Kz Awang
 
kelemahan dan prog intervensi math
kelemahan dan prog intervensi mathkelemahan dan prog intervensi math
kelemahan dan prog intervensi math
Ammy Achan
 
Kegiatan dan sumbangan di luar tugas rasmi
Kegiatan dan sumbangan di luar tugas rasmiKegiatan dan sumbangan di luar tugas rasmi
Kegiatan dan sumbangan di luar tugas rasmi
mahandren mahandren
 
Contoh Kertas Kerja Lawatan...
Contoh Kertas Kerja Lawatan...Contoh Kertas Kerja Lawatan...
Contoh Kertas Kerja Lawatan...
Nur Dalila Zamri
 

Mais procurados (20)

Intro perolehan
Intro perolehanIntro perolehan
Intro perolehan
 
Elaun & tuntutan
Elaun & tuntutanElaun & tuntutan
Elaun & tuntutan
 
Senarai program dan aktiviti panitia matematik bagi sesi persekolahan 2014
Senarai program dan aktiviti panitia matematik bagi sesi persekolahan 2014Senarai program dan aktiviti panitia matematik bagi sesi persekolahan 2014
Senarai program dan aktiviti panitia matematik bagi sesi persekolahan 2014
 
Menjalankan ujian fungsi unit
Menjalankan ujian fungsi unitMenjalankan ujian fungsi unit
Menjalankan ujian fungsi unit
 
Lengkap Microsoft Access Langkah Demi Langkah
Lengkap Microsoft Access Langkah Demi LangkahLengkap Microsoft Access Langkah Demi Langkah
Lengkap Microsoft Access Langkah Demi Langkah
 
Pengurusan stor
Pengurusan storPengurusan stor
Pengurusan stor
 
Senarai semak akhir tahun
Senarai semak akhir tahunSenarai semak akhir tahun
Senarai semak akhir tahun
 
Laporan kewangan
Laporan kewanganLaporan kewangan
Laporan kewangan
 
Panduan Penyediaan Sasaran Kerja Tahunan
Panduan Penyediaan Sasaran Kerja TahunanPanduan Penyediaan Sasaran Kerja Tahunan
Panduan Penyediaan Sasaran Kerja Tahunan
 
Modul p&p nombor dan operasi tahun 2
Modul p&p nombor dan operasi tahun 2Modul p&p nombor dan operasi tahun 2
Modul p&p nombor dan operasi tahun 2
 
PENGURUSAN ASET KERAJAAN
PENGURUSAN ASET KERAJAANPENGURUSAN ASET KERAJAAN
PENGURUSAN ASET KERAJAAN
 
Kit Penerangan Catch-Up-Plan Fasa 3 - Program Berfokus
Kit Penerangan Catch-Up-Plan Fasa 3 - Program BerfokusKit Penerangan Catch-Up-Plan Fasa 3 - Program Berfokus
Kit Penerangan Catch-Up-Plan Fasa 3 - Program Berfokus
 
Slaid taklimat my portfolio0
Slaid taklimat my portfolio0Slaid taklimat my portfolio0
Slaid taklimat my portfolio0
 
Konsep sistem fail
Konsep sistem failKonsep sistem fail
Konsep sistem fail
 
Manual prosedur pk1
Manual prosedur pk1Manual prosedur pk1
Manual prosedur pk1
 
My portfolio.docx
My portfolio.docxMy portfolio.docx
My portfolio.docx
 
kelemahan dan prog intervensi math
kelemahan dan prog intervensi mathkelemahan dan prog intervensi math
kelemahan dan prog intervensi math
 
Kegiatan dan sumbangan di luar tugas rasmi
Kegiatan dan sumbangan di luar tugas rasmiKegiatan dan sumbangan di luar tugas rasmi
Kegiatan dan sumbangan di luar tugas rasmi
 
Contoh Kertas Kerja Lawatan...
Contoh Kertas Kerja Lawatan...Contoh Kertas Kerja Lawatan...
Contoh Kertas Kerja Lawatan...
 
Pengurusan fail
Pengurusan failPengurusan fail
Pengurusan fail
 

Semelhante a Mid term sem 2 1415 sol

Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
rohassanie
 
Name _______________________________ Class time __________.docx
Name _______________________________    Class time __________.docxName _______________________________    Class time __________.docx
Name _______________________________ Class time __________.docx
rosemarybdodson23141
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
RSathyaPriyaCSEKIOT
 
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docxSpring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
rafbolet0
 

Semelhante a Mid term sem 2 1415 sol (20)

Bis 311 final examination answers
Bis 311 final examination answersBis 311 final examination answers
Bis 311 final examination answers
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Name _______________________________ Class time __________.docx
Name _______________________________    Class time __________.docxName _______________________________    Class time __________.docx
Name _______________________________ Class time __________.docx
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
LMmanual.pdf
LMmanual.pdfLMmanual.pdf
LMmanual.pdf
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************
 
Cp manual final
Cp manual finalCp manual final
Cp manual final
 
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
 
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docxSpring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
 
Intro
IntroIntro
Intro
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
c programing
c programingc programing
c programing
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.com
 
C chap02
C chap02C chap02
C chap02
 
C chap02
C chap02C chap02
C chap02
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.com
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.com
 
I PUC CS Lab_programs
I PUC CS Lab_programsI PUC CS Lab_programs
I PUC CS Lab_programs
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 

Mais de IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Heaps
HeapsHeaps
Heaps
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 

Mid term sem 2 1415 sol

  • 1. 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:
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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.
  • 6. 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; }
  • 7. 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:
  • 8. 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:
  • 9. 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;
  • 10. 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
  • 11. 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; }
  • 12. 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 . . . ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________
  • 13. 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
  • 14. 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 Machinen " << 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 purchasen" ; cout << "Press '1' for novelsn" ; cout << "Press '2' for magazinesn" ; cout << "Press '3' for newspapersnn" ; cin >> category; switch(category) { case 1: cout << "You have selected 1 for novelsn"; cout << "Select 'a' for : In the eyes of the nightn"; 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;
  • 15. 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 magazinesn"; cout << "Select 'j' for : Home Improvementn"; cout << "Select 'k' for : Cooking with Madamn"; cout << "Select 'l' for : Programming Bits and Bytesn"; 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;
  • 16. 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 newspapersn"; cout << "Select 'x' for : Berita Hariann"; cout << "Select 'y' for : New Straits Timen"; cout << "Select 'z' for : Utusan Malaysian"; 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;
  • 17. 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 –
  • 18. Page 18 of 16 Attachment (Symbols for flow chart)