Professional Documents
Culture Documents
FA20 BCS 094 (TERM PF Theory)
FA20 BCS 094 (TERM PF Theory)
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]
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]
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
Ans: #include<iostream>
#include<string.h>
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>
int main(){
for(i=0;i<5;i++){
cin>>arr[i];}}
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)
[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>
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 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:
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 << ".";