Cs 201 Solved Midterm Papers 1
Cs 201 Solved Midterm Papers 1
Cs 201 Solved Midterm Papers 1
www.virtualstudysolutions.blogspot.com
CS201-introduction to programing
Latest Solved subjective from Midterm Papers May 10,2011
MIDTERM EXAMINATION
Spring 2010
Question No: 17 ( Marks: 2 )
What is the difference between switch statement and if statement.
Answer:
1.if statement is used when we have to check two conditions while switch is a multi conditional control statement
2. SWITCH statement can be executed with all cases if the “break” statement is not used whereas IF statement has to be true
to be executed further.
Answer:
Yes we can assign the one value of C-string to another using assignment operator. We can
assign the value of one string to another string through this method.
A[0]=B[0]
A[1]=B[2]
A[3]=B[3]
And we can assign the whole string to another C string using Assignment operator by using loops.
#include <iostream.h>
union mytypes_t {
char c;
int i;
float f;
} mytypes;
int main(){
mytypes.c = 'H';
mytypes.i = 15;
system("PAUSE");
return 0;
}
Question: What operator do you use to assign a pointer the address of another variable or constant?
(2marks)
Question: Identify each of the following function as string conversion function or string manipulating
function; double atof (const char *nptr)
Answer: page 191
Converts the string nPtr to double.
Question: What is the purpose and syntax of the const keyword? (5 marks)
Answer:
Through "const", if we have to change the size of the array, we only have to change the value of arraySize where it is declared.
Syntax of const is
const variable-name [ = value];
Question: When a pointer is incremented then how many bytes will it move to change its address? 2 Marks
Answer: page 160
If an integer occupies four bytes in the memory, then the yptr++; will increment its value by four. when we increment the yptr, it
points to the next integer in the memory
Q4: If there are 2n element in an array then what would be the number of iterations required to
search a number using binary and linear search? 3 Marks
Answer: page 160
If an array has elements 2n, then the maximum number of iterations required by binary search will be n
Q5: write down the functions definition if we want to pass the argument to a function by reference
without changing the values stored at address. 5 Marks
Q6: What will be the output of the following code segment 5 Marks
, int x= 6;
int y;
x = x << 1;
y = x >> 1;
cout << “x = ” << x << “\n”;
cout << “y = ” << y;
Answer:
x=12
y=6
MIDTERM EXAMINATION 2009
Question No: 17 ( Marks: 1 )
To Which category of the software “Compiler and Interpreter” belongs?
Question: What will be the output of the following segment of C++ code?
int A[5] = {1 , 2, 3, 4};
int i;
for (i=0; i<5; i++)
{
A[i] = 2*A[i];
cout << A[i] << " ";
}
Answer:
24680
Loops will run 5 times as its starting from zero. It will multiply the value of each item in array as last time
is not initialized so it will multiply it with zero to give zero as output
Question No: 22 ( Marks: 10 )
Write a C++ program that will determine if a departmental store customer has exceeded the credit
limit on a charge account.
Program should input the following facts in five variables
1. Account number
2. Balance at the beginning of month (Beginning balance)
3. total of all items charged by customer this month (charges)
4. total of all credits (credits)
5. allowed credit limit
Calculate the new balance
New balance = Beginning balance + charges – credits
Determine if new balance exceeds the allowed credit limit. For those customers whose credit limit is
exceeded. The program should display the message “Credit Limit exceeded.”
Answer:
arrary[4] = 200;
arrary[8] = 300;
Answer:
Program should not compile due to missing <> from following statement
#include iostream.h
if we ignore this then output should be
Roll No is 8
Answer:
if ( num % 2 = 0 ) There should be extra = sign following is right statement
if ( num % 2 = =0 )
Answer:
#include <iostream.h>;
void PrintArray(int arrayInput[], int &s, int &e);
main ( )
{
int pause;
int TestArray [6] = {1,2,3,4,5,6};
int StartPoint = 0;
int EndPoint = 5;
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cin >> pause;
}
void PrintArray(int arrayInput[], int& s, int& e)
{
for (int i = s; i<= e; i++)
{
cout<< arrayInput[i];
}
s=s+1;
}
struct Customer
int custnum;
int salary;
float commission;
};
A programmer wants to assign 2000 for the structure member salary in the above example of
structure Customer with structure variable cust1 What line of code should he write
Answer:
Float
Question: 18 Why should goto statement be avoided in C/C++?
Answer: page 73
When structured programming was started, it was urged not to use the goto statement. Though goto is there
in C language but we will not use it in our programs. It will adopt the structured approach. All of our
programs will consist of sequences, decisions and loop. Because loop provide best platform to manipulate
the data.
Question: 19 What operator do you use to assign a pointer the address of another variable or
constant? Marks: 2
& sige
i.e.
int i;
int * ptri;
ptri = &i;
switch (operator)
{
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case 'x':
case '*':
result = op1 * op2;
break;
case '/':
result = operand1 / operand2;
break;
default:
cout << "Unknown operator" ;
}
Answer:
if(operator==’ +’)
{
result = op1 + op2;
}
else
if(operator==’ -’)
{
result = op1 - op2;
}
else
if (operator==’ *’)
{
result = op1 * op2;
}
else
if (operator==’ /’)
{
result = op1 / op2;
}
else
{
cout << "Unknown operator" ;
Question: 22 Write a recursive function that takes character array and starting subscript as arguments. In
each recursive call, the function should display the string from subscript to the end of string. The starting
subscript in first call should be 0. In each successive call, the subscript should increse by one and function
should print the array from subscript to the end of string. The function should stop processing and return
when null character encounters.
Suppose the char string passed to the function is,
"SampleString", then the function will print output as follows,
SampleString
ampleString
mpleString
pleString
so on....
Marks: 10
Question: 18 Is it possible to evaluate the size of structure, if yes then how? (1)
Answer: YES
#include <iostream.h>
#include <stdlib.h>
struct VehicleParts
{
int wheels;
int seats;
VehicleParts()
{
cout << "\n VehicleParts - default constructor";
}
VehicleParts(int wheels, int seats)
{
this->wheels = wheels; this->seats = seats; cout << "\n VehicleParts - parameterized constructor";
}
Question: 23 (10)
Write a recursive function that takes three arguments (an integer array, starting subscript ‘s’ and ending
subscript ‘e’ ).
In first recursive call, the function should display the array from subscript ‘s’ (s = 0) to ‘e’ (e = size of
array). In each successive call, the function should print the array from index s+1 to e. The function should
stop processing and return when starting subscript becomes equal to ending subscript.
For example, if user enters values for array 2, 3, 4, 5, 6 then the recursive function must display the
following output.
23456
3456
456
56
6
Answer:
#include<iostream.h>
#include<conio.h>
void recursive(int [],int,int);
void main()
{
int array[5];
for(int i=0;i<5;i++)
{
cout<<"\nEnter the "<<i<<" Index number :";
cin>>array[i];
}
recursive(array,0,4); //0 is the starting index and 4 is the ending subscript
getche();
}
void recursive(int arr[],int s,int e)
{
if(s!=e+1)
{
for(int i=s;i<=e;i++)
cout<<arr[i]<<"\t";;
cout<<endl;
s++;
recursive(arr,s,e); //Recursive call
}
}
If the aggregate of a student is less than 150 then the program should display message
“You can not be admitted to VU” otherwise display the message “Congratulation! You admitted in VU
“
Answer:
#include <iostream.h>
main ()
{
int Matrix_marks,Fsc_marks;
float Aggregate;
cout << "Please enter your Matric numbers: ";
cin >> Matrix_marks;
cout << "Please enter your FSC numbers: ";
cin >> Fsc_marks;
Aggregate=((Matrix_marks*2)+(Fsc_marks*4))/24;
if(Aggregate < 150)
{
cout <<"You cannot be admitted to VU";
}
else
{
cout <<"Congratulation! You admitted in VU ";
}
//Aggregate = (Matrix_marks*2 + Fsc_marks*4) / 24
system("pause");
}
Question: 18 Which bitwise operator returns true if both bits are different and returns false if both
bits are same? (1)
ANS
1. !=
http://web.cs.mun.ca/~michael/c/op.html
Question: 21 The statement int Name [2][2]; define a 2x2 array, Write the code which read data
from keyboard for this array. (5)
Answer:
for(int i=o; i<2; i++)
{
for (w=0; w<2; w++)
{
cin >>Name[i][w];
}
cout <<endl;
}
for(int i=o; i<2; i++)
{
for (w=0; w<2; w++)
{
cout<<Name[i][w]<<” ”;
}
cout <<endl;
}
Question: 22 Write a program which reads a text file “PlayersInfo.txt” residing in the current directory.
Open the file PlayersInfo.txt in read mode and assign these values to the struct Player; assume order of the
data in the file to be exactly the same as the order of struct attributes. The struct Player has following
attributes
i) Name
ii) Height
iii) Age
iv) Score
v) Game
After reading the file and assigning values to the struct, in the end close the file PlayersInfo.txt.
Question: 18 What will be the value of x after the execution of the following code segment? (1)
int x =10;
int y =30;
int *xptr = &x;
x = *xptr + 10;
Question: 19 (2)
What is the output of the code given below?
void main()
{
int a=10,b=20;
char x=1,y=0;
if(a,b,x,y)
{
cout << "EXAM";
}
}
Answer:
This code has no output, because if statement did not have any condition. So the next line cout
<< “EXAM”; will not execute.
Question: 20
What is Overflow condition? (3)
Answer: page49
When we try to store larger information in a variable, than a data type can store, overflow
condition occurs. When overflow condition occurs either a run time error is generated or wrong
value is stored.
Question: 21 Write code which read a string not greater than 20 characters from keyboard stored it
in an array Name and display it on the screen. (5)
Answer:
#include <iostream.h>
main ()
{
char name[21];
int i;
cout << "Please Enter your name: ";
system("pause");
}
Question: 22 (10)
Write a C++ program which contains a user-define function named convertHeight which takes height
of person in centimeter as an argument. This function converts the height in centimeter into feet and
inches and displays them on the screen.
Program should prompt the user to enter height in centimeter and pass it to function convertHeight as an
argument which displays height in feet and inches.
Hint:
1 foot = 12 inches
1 inch = 2.5 cm
Answer:
#include<iostream.h>
#include<conio.h>
getche();
}
int i,c;
i = f*12;
c = i*2.5;
cout <<"high in Feet = "<<f<<endl;
cout <<"high in inches = "<<i<<endl;
cout <<"high in CM = "<<c<<endl;
Question: 17 What is the functionality of the function:char *strncat (char *s1, const char *s2, size_t
n) (1)
Answer:
Appends at most n character of string s2 to array s1. The first character of s2 overwrites the
terminating null character of s1. The value of s1 is returned.
Question: 18 Write a piece of code that outputs three values of type int, long and float to a stream.
(1)
Answer:
main()
{
int a;
long b;
float c;
a = 10;
b= 124568979;
c = 6.57;
cout <<a<<"\t"<<b<<"\t"<<c;
getche();
}
Question: 21 Write down the C++ program that calculates the Zakat on the amount entered by the
user
Question: 18 Suppose there is a pointer to structure *sPtr. How can we access the data member
‘name’ with sPtr? [1]
Answer: 233
Structure data members using pointers Using the * operator;
(*sptr).name
Question: 21 Write down the function definition if we want to pass the arguments to a function by
reference without changing the values stored at that addresses. [5]
Question: 22
From writing to execution of the program following software are used explain for what purpose each
is used. [10]
Editor
Compiler/Interpreter
Linker
Loader
Answer:
An editor is a tool for writing the code of a program. For this purpose we used Editors in which we write
our code. We can use word processor too for this, but word processors have many other features like bold
the text, italic, coloring the text etc, so when we save a file written in a word processor, lot of other
information including the text is saved on the disk. For programming purposes we don’t need these things
we only need simple text. Text editors are such editors which save only the text which we type. So for
programming we will be using a text editor
Compiler and Interpreter
Compilers translate the English like language (Code written in C) into a language (Machine language)
which computers can understand. The Compiler read the whole program and translates it into machine
language completely. The difference between interpreter and compiler is that compiler will stop translating
if it finds an error and there will be no executable code generated whereas Interpreter will execute all the
lines before error and will stop at the line which contains the error. So Compiler needs syntactically correct
program to produce an executable code. We will be using compiler in our course
As we write the code in English and we know that computers can understand only 0s and 1s. So we need a
translator which translates the code of our program into machine language. There are two kinds of
translators which are known as Interpreter and Compilers. These translators translate our program which is
written in C-Language into Machine language. Interpreters translates the program line by line meaning it
reads one line of program and translates it, then it reads second line, translate it and so on. The benefit of it
is that we get the errors as we go along and it is very easy to correct the errors. The drawback of the
interpreter is that the program executes slowly as the interpreter translates the program line by line. Another
drawback is that as interpreters are reading the program line by line so they cannot get the overall picture of
the program hence cannot optimize the program making it efficient.
.
Linker Most of the time our program is using different routines and functions that are located in different
files, hence it needs the executable code of those routines/functions. Linker is a tool which performs this
job, it checks our program and includes all those routines or functions which we are using in our program to
make a standalone executable code and this process is called Linking
Loader after a executable program is linked and saved on the disk and it is ready for execution. We need
another process which loads the program into memory and then instruct the processor to start the execution
of the program from the first instruction (the starting point of every C program is from the main function).
This processor is known as loader. Linker and loaders are the part of development environment. These are
part of system software.
Question: 21 What are similarities and differences between Structures and Unions? (5)
Answer:
In structures, we have different data members and all of these have their own memory space. In union, the
memory location is same while the first data member is one name for that memory location. However, the
2nd data member is another name for the same location and so on. Consider the above union (i.e.
intOrChar) that contains an integer and a character as data members. What will be the size of this union?
The answer is the very simple. The union will be allocated the memory equal to that of the largest size data
member. If the int occupies four bytes on our system and char occupies one byte, the union intOrChar will
occupy four bytes
OR
Structure
In structures, the data members are public by default. It means that these are visible to all and anyone can
change them. Is there any disadvantage of this? Think about the date.
syntax
struct student
{
char name[60];
char address[100];
float GPA;
};
Unions We have another construct named union. The concept of union in C/C++ is: if we have something
in the memory, is there only one way to access that memory location or there are other ways to access it.
We have been using int and char interchangeably in our programs. We have already developed a program
that prints the ACSII codes. In this program, we have stored a char inside an integer. Is it possible to have a
memory location and use it as int or char interchangeably? For such purposes, the construct union is used.
The syntax of union is:
union intOrChar
{
int i;
char c;
};
Answer: repeat
Answer: repeat
Answer: repeat
Question No: 22 ( Marks: 10 )
Write a void function( ); that takes integer numbers from the user and then displays the sum of odd and
even numbers entered by the user. Your program should terminate if user enters a negative number
Q21
What are the advantages of random access file over sequential access file? (5)
Answer:
In comparison to sequential access files, you may significantly save on the amount of disk space required
by the file by using random access.
With random access, files can be opened for both read/write at the same time. This is a great advantage over
using sequential access.
Q20
If there are 2n elements in an array then what would be the number of iterations required to search a
number using binary search and linear search? Marks: 3
Answer: repeat
www.virtualstudysolutions.blogspot.com