Final Exam Solution - Computing Fundamentals and Programming
Final Exam Solution - Computing Fundamentals and Programming
Marks: 120
1 January, 2018
Rules:
1. All questions are compulsory.
2. Solve all questions on the answering sheet/s
3. Plagiarism, cheating or any act that can result in a possible cheating is prohibited
4. Anybody found talking, having eye contact, looking at other students' work or
showing/sharing his work will be immediately disqualified from the exam.
5. Calculator/ Cell phone/ Digital diary/No gadgets allowed.
6. Work on your exam to the best of your knowledge. No questions require elaboration.
7. If you think a question has a mistake, mention the mistake and don't raise a query
8. Read the statement carefully before you start attempting the question.
Q.1: Write a program in C++ that will: (1+1+4+1+2+1) = 10 Marks
Solution:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char name[30];
cout << "Please enter a string: ";
cin.getline(name, 30);
if (strlen(name) == 0)
{
cout << "String is empty. Try again";
return 0;
}
ofstream fout("output.txt");
}
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char rollno[10];
cout << "Please enter your rollno: ";
cin.getline(rollno, 10);
if (strlen(rollno) != 9)
{
cout << "Invalid roll number.";
return -1;
}
int rn = 0;
for (int i = 6; i < 9; i++)
{
rn+= (rollno[i] - 48) * pow(10, 2 - (i - 6));
}
cout << endl << "Roll Number: " << rn << endl;
cout << endl << "Graduation Year: " << year + 4 << endl;
return 0;
}
Q.3: Write a program in C++ that does following without using square brackets notation:
20 Marks
1. Declare an array of 30 float numbers (you can use square brackets here) 3 marks
2. Assign 2.71 to 0th element 3 marks
3. Assign all elements of the array incrementing previous number by 0.23 3 marks
4. Create a function called average that will receive this array as parameter 3 marks
5. Calculate average and return the result to main 3 marks
6. Make sure you call this function in main to show its working. 3 marks
7. De-allocate the array at the end of the program. 2 marks
Solution
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
float *num = new float[30];
*(num + 0) = 2.71;
float sum = 0;
for (int i = 0; i < 30; i++)
{
sum += *(f + i);
}
cout << "Sum" << sum << endl;
return sum / 30.0;
}
Q.4: Write a function in C++ named reverse. The function will take a character array as input and will
return the character array reversed in order. For example if given parameter “Super” returns “repuS”.
10 marks
Solution
Q.5: Write a program in C++ that will take your name as input, and will convert each character of
your name to its ascii number, then add 20 to it and multiply it with 3. After that it will print sum of all
such numbers and it will also print it in a form of Sum equation 0of all numbers. For example, for input
name “Basit” the output will be:
10 Marks
Solution:
#include <iostream>
using namespace std;
int main()
{
char name[100];
cout << "Please enter your name: ";
cin.getline(name, 100);
int sum = 0;
int temp;
for (int i = 0; i < strlen(name); i++)
{
temp = (name[i] + 20) * 3;
cout << temp;
sum += temp;
if (strlen(name) - 1 == i)
cout << "=";
else
cout << "+";
}
Q.6: What will be the output of the program if you write your name as input. Replicate above output
box for your name input and changed output on the answer sheet.
10 Marks
Solution
For example:
255+354+360+411+384+384+351+372+156+258+375+390+156+294+351+411+366+351+384=6363
• Persistent memory
Computer memeory that stores information and does not get deleted even after you shut down the
computer.
• CPU
Central Processing unit, the component of the computer that performs the arithmetic and logical
operations. It’s the brain of the computer.
• Compiler
A software that converts the source code in a programming language to machine executable code
(machine language).
• Array
Array is a set of consecutive variables declared in the memory of same type. Array has a name and is
declared with one statement.
• Pointer
A special variable that is used to store the address of another variable. It can be used for accessing the
content of the original variable.
int main(){
char d[10];
return 0;
}
Solution
d<h=l>p?t@
Q.9: Please explain what this program does and what does the output screen look like. More
details you give, better it is, including what does the program do, how does user interact with the
program (input/output), when does the program stop, what is the motive of the program etc.
10 marks
#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;
void checkInput();
int main(){
score = enemyx = enemyy = 0;
myx = 25;
myy = 19;
bulletx = bullety = -1;
while (true){
if (i == 19)
cout << "-";
else
cout << " ";
checkInput();
}
cout << endl;
}
Sleep(10);
system("cls");
}
}
// code continued
void checkInput(){
char ch;
if (_kbhit()){
ch = _getch();
switch (ch) {
case 65:
myx = (myx > 0) ? --myx : myx;
break;
case 83:
myx = (myx < 49) ? ++myx : myx;
break;
case 32:
bulletx = myx;
bullety = myy - 1;
break;
case 81:
exit(0);
}
}
}
Solution:
struct Employee
{
int employeeNumber;
char name[45];
float salary;
int age;
};
If you write a C++ program to store such employees in a file using sorted data (Hint: Random access
filing, remember the hackathon?), answer the following questions:
4+45+4+4 = 57
2. What will be the size of each record if we save one employee on each line of file.
Assuming
11 characters for an int, 10 for data, one space
11 characters for a float, 10 for float and one space
46 characters for name including one space
2 for new line
11+46+11+11+2 = 81
3. What will be the size of file if we have to save 10,000 employees in the file (no data yet) ?
810,000 bytes
4. Will the file size change when we add or delete records to this file? Why?
No change, all data will be over-written, file size will remain the same. Empty file had just spaces.
5. What is the drawback of this fixed length field random access filing approach?
Random access filing approach with fixed data will occupy data in file even when it is not being used. So
if the file is expected to save 10,000 records, but there is no data, it will still use 810,000 bytes.
Technically all these bytes are wasted.
1. Declares two char arrays for file names called file1 and file2
2. Take input of two file names from user into file1 and file 2.
3. Open file1 for reading, open file 2 for writing
4. Read everything in file1
5. Reverse the whole file’s text
6. Writes the reversed data into file2
Solution:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char file1[40], file2[40];
ifstream fin(file1);
ofstream fout(file2);
if (!fin)
{
cout << "File reading failed" << endl;
return 0;
}
while (true)
{
if (fin.get() == -1)
break;
filesize++;
// reading file
fout.close();
fin.close();
return 0;
}
Ascii Table (You’ll need it)
DEC Symbol Description
0 NUL Null char 42 * Asterisk 85 U Uppercase U
1 SOH Start of Heading 43 + Plus 86 V Uppercase V
2 STX Start of Text 44 , Comma 87 W Uppercase W
3 ETX End of Text 45 - Hyphen 88 X Uppercase X
4 EOT End of Transmission 46 . full stop 89 Y Uppercase Y
5 ENQ Enquiry 47 / divide 90 Z Uppercase Z
6 ACK Acknowledgment 48 0 Zero 91 [ bracket
7 BEL Bell 49 1 One 92 \ Backslash
8 BS Back Space 50 2 Two 93 ] bracket
9 HT Horizontal Tab 51 3 Three 94 ^ Caret
10 LF Line Feed 52 4 Four 95 _ Underscore
11 VT Vertical Tab 53 5 Five 96 ` Grave accent
12 FF Form Feed 54 6 Six 97 a Lowercase a
13 CR Carriage Return 55 7 Seven 98 b Lowercase b
14 SO Shift Out / X-On 56 8 Eight 99 c Lowercase c
15 SI Shift In / X-Off 57 9 Nine 100 d Lowercase d
16 DLE Data Line Escape 58 : Colon 101 e Lowercase e
17 DC1 Device Control 1 59 ; Semicolon 102 f Lowercase f
18 DC2 Device Control 2 60 < Less than 103 g Lowercase g
19 DC3 Device Control 3 61 = Equals 104 h Lowercase h
20 DC4 Device Control 4 62 > Greater than 105 i Lowercase i
21 NAK Negative Ack 63 ? Question mark 106 j Lowercase j
22 SYN Synchronous Idle 64 @ At symbol 107 k Lowercase k
23 ETB End of Transmit Block 65 A Uppercase A 108 l Lowercase l
24 CAN Cancel 66 B Uppercase B 109 m Lowercase m
25 EM End of Medium 67 C Uppercase C 110 n Lowercase n
26 SUB Substitute 68 D Uppercase D 111 o Lowercase o
27 ESC Escape 69 E Uppercase E 112 p Lowercase p
28 FS File Separator 70 F Uppercase F 113 q Lowercase q
29 GS Group Separator 71 G Uppercase G 114 r Lowercase r
30 RS Record Separator 72 H Uppercase H 115 s Lowercase s
31 US Unit Separator 73 I Uppercase I 116 t Lowercase t
32 Space 74 J Uppercase J 117 u Lowercase u
33 ! Exclamation mark 75 K Uppercase K 118 v Lowercase v
34 " Double quotes 76 L Uppercase L 119 w Lowercase w
35 # Number 77 M Uppercase M 120 x Lowercase x
36 $ Dollar 78 N Uppercase N 121 y Lowercase y
37 % Percent 79 O Uppercase O 122 z Lowercase z
38 & Ampersand 80 P Uppercase P 123 { Opening brace
39 ' Single quote 81 Q Uppercase Q 124 | Vertical bar
40 ( Open bracket 82 R Uppercase R 125 } Closing brace
41 ) Close bracket 83 S Uppercase S 126 ~ tilde
84 T Uppercase T 127 Delete