You are on page 1of 9

Name:Muhammad Salahuddin

Reg: FA20-BCS-094
PF

SUBJECTIVE (40
PART Marks)
Question No 02: Errors (Total Marks:
identification. 14)
a) [4 Marks]

Which of the following array definitions are invalid, and why? If a line does not contain error, clearly
state “NO ERROR!”.

Ans:
1) No Error
2) Error Abdullah has more characters than the limit that is put on
the array
3) No Error
4) No Error
b) [5 Marks]

Given the declaration:

char string15[16];
Identify which of the following statements have COMPILE-TIME error. If a statement has an error,
describe (in a single line) why? If a line does not contain error, clearly state “NO ERROR!”.

Ans
1)No Error
2)Error no value of index Is given, the array takes input character by
character and the index has to also increase character by character,
since the array takes values character by character you cannot enter a
whole string without an index
3)No Error
4)No Error
5)No Error

c) [5 marks]

Consider the following variables declaration.

int x=10, y=20, z=50;


double d=3.14;
Identify and state the problems (compile-time/run-time errors) with each of the following lines of code,
if any. If a line does not contain error, clearly state “NO ERROR!”.

i)No Error
ii) Error the address of a variable “y” is being stored in an int yPtr it can
be fixed by changing the yPtr into *yPtr
iii)Error value of an int variable is being stored in a double pointer
iv)No Error
i) No Error

Question No 03: Debug Errors. (Total Marks: 05)


a) [3 Marks]
Identify the error with the following code and rewrite after correcting it.

Ans: #include<iostream>
#include<string.h>

using namespace std;

void f(float a){

a=9;

cout<<a+a;

float a;

int main(){

f(a);}

b) [2 Marks]

Assuming we want to input 5 values in an array ‘a’, rewrite the following code after correcting it:
#include<iostream>

#include<string.h>

using namespace std;

int main(){

int i; int arr[5];

for(i=0;i<5;i++){

cin>>arr[i];}}

Question No 04: Short questions/code snippets. (Total Marks: 15)


a) [2 Marks]

Explain the difference between a value and a reference parameter.

Ans: Value parameter is data of any particular variable meanwhile reference parameter holds the
address of particular variable in memory.

b) [2 Marks]

Write down a statement, which defines an array of C-style strings and stores the names of four seasons
in it using the array initializer syntax (i.e. initialize the array at the time of its declaration). c)

Ans: string movie_name[4]={“season1”,”season2”,”season3”,”season4”};

[5 Marks]
Write down a code script to open a file (data.txt) in append mode. Then, validate the file-opening
operation. If the file is successfully opened, the write an integer new_employee_id value (input from
user) to the file and close the file.

#include<iostream>

#include <fstream>

using namespace std;

int main(){

int new_employee_id;

ofstream Filehandler("data.txt",ios::app);

if(Filehandler.is_open()){

while(!Filehandler.eof()){

cin>>new_employee_id;

Filehandler<<new_employee_id<<endl;

Filehandler.close();

break;

}}

else{

cout<<"PROB";

Filehandler.close();

d) [6 Marks]

Consider the following struct definition:


struct employee_t { int empid, grade; // grade can be within [1-20], e.g. 19
string name; // e.g. Muhammad Ali date_t joiningdate; address_t
homeaddress; float workhours[7]; // how many hours the employee worked on
each day of week , e.g. {4.5, 5, 6, 5.5, 8, 7, 0} };
struct date_t { int year, month, day; //
e.g. 1986, 2, 12 }; struct address_t {
string city_name; // E.g. Lahore town_t
town; };
struct town_t { string townname; //
e.g. Punjab Society int housenumber; //
e.g. 42 char sectorcode; // e.g. B
};

Consider the struct definitions given above. Assume that we have an array of 3 employees, which is
already initialized with some sample data. You have to write down a loop, which prints the data of all
the employees in a descriptive form. For your reference, the data of a single employee should be printed
in the descriptive form as follows.

Employee Muhammad Ali (ID: 786) has grade 19. He joined the company since 23-06-1984, and is
located currently at 42-B, Punjab Society, Lahore. His work hours during the last week were 36.

Ans:

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

cout << "Employee " << abc[i].name << "ID: " << abc[i].empid << "has grade " << abc[i].grade << ". He
joined the company since " << abc[i].year << " - " << abc[i].month << " - " << abc[i].day << " and is
currently located at" << abc[i].housenumber << "-" << abc[i].sectorcode << ", " << abc[i].townname << ",
" << abc[i].city << "His work hours during the last week were " << abc[i].workhours << ".";

Question No 05: Program output. (Total Marks: 06)


a) [6 Marks]

What is the output of the following program?


Ans:
Output
5, 10, 15
20,10, 15
25,30, 15
45,30, 60

You might also like