40 Programming in C and C++ Questions with Answers

UGC NET

This post consists approx. 40 questions from Programming in C and C++ from previous years UGC NET papers. this will help you to understand the pattern of questions comes under this section. Generally 5 out 50 questions comes from Programming in C and C++. New syllabus issued by the NTA is given in this post. This post also highlight the questions from each years. Try to solve the questions.

Best of luck for your NTA UGC NET  preparation.

Programming in C and C++ and Questions from Computer Science old Papers:




UGC Syllabus:

Programming in C : Elements of C – Tokens, identifiers, data types in C. Control structures in C. Sequence, selection and iteration(s). Structured data types in C-arrays, struct, union, string, and pointers.
O – O Programming Concepts : Class, object, instantiation. Inheritance, polymorphism and overloading.
C++ Programming : Elements of C++ – Tokens, identifiers. Variables and constants, Datatypes, Operators, Control statements. Functions parameter passing. Class and objects. Constructors and destructors. Overloading, Inheritance, Templates, Exception handling.

OBJECT ORIENTED USING C++ NOTES with Programs

Classes and Objects
A class is a way to bind the data and its associated functions together. It allows the data and functions to be hidden from external use.
General Syntax of the class declaration is:-
Class class_name
{
Private:
variable declarations;
function declarations;
Public:
variable declarations;
function declarations;
Protected:
variable declarations;
function declarations;
};
The keywords private, public & protected are used to specify the three levels of access protection for hiding data and function members internal to the class.
Private:– The private data members is not accessible to the outside world(out of class) a member data in private section, can only be accessed by the member functions and friends of this class. The member functions and friend of this class can always read or write data members.
Protected:-Members in protected section, can only be accessed by member functions and friend of this class. Also, these functions can be accessed by the member functions and friend derived from this class. It is not accessible to the outside world.
Public:-The members which are declared the Public section, can be accessed by any function in the outside world(out of class).
Encapsulation :-The binding of data and functions together into a single class type variable is known as encapsulation.

The class specification has two parts:
I. Class declaration
II. Class function definitions
The syntax for Class declaration and function definitions is
Class declaration
class item
{
Private:
int number;
float cost;
Public:
void getdata( );
void putdata( );
}

Class function definitions
Void item: :getdata( )
{
——-
}
void item: :putdata( )
{
——-
}

Program for object & class
#include
#include
class item
{
Private:
int number;
float cost;
Public:
void getdata(int a,float b);
void putdata( )
{
cout<<”Number:”<<number<<endl;
cout<<”Cost”<<cost<<endl;
}
};
void item::getdata(int a,float b)
{
number=a;
cost=b;
}
void main( )
{
item x;
cout<<”Object x”<<endl;
x.getdata(100,299.85);
x.putdata( );
item y;
cout<<”Object y”<<endl;
y.getdata(200,175.50);
y.putdata( );
}
Ques: What are Constructor and Destructor? Explain with example?
Ans: A constructor is a special member function whose task is to initialize the objects of its class. Its name is the same as the class name. It is invoked automatically when an object of its associated class is created.
Characterstics of Constructor
1. They should be declared in the Public section.
2. They are invoked automatically when the objects are created.
3. They don’t have return types, not even void.
4. They cannot be inherited, though a derived class can call the base class constructor.
5. They can have default arguments.
6. Constructors cannot be virtual.
7. We cannot refer to their addresses.
8. They make implicit calls to the operators new and delete when memory allocation is required.
9. It may not be static.

Default Constructor
A Constructor that accepts no parameters is called Default Constructor .e.g
Class A::A( ) //default Constructor
Parameterized Constructor
The Constructor that can take arguments are called parameterized Constructors .e.g.
Class Integer::Integer (int x.int y) //Parameterized Constructor
Destructor:-The destructor is a member function whose name is the same as the class name but preceded by a tilde sign. It is used to destroy the objects that have been created by a constructor. A class has only one destructor. e.g.
Class Integer:: ~Integer( )
Rules for destructors
1. It is preceded by ~ (tilde) sign.
2. It is declared with no return types (not even void),since it cannot ever return a value.
3. It cannot be declared static, const or volatile.
4. It takes no arguments and therefore cannot be overloaded.
5. It should have public access in the class declaration.

e.g:-
C++ program for Constructor and Destructor
#include
Int count=0;
class alpha
{
Public: alpha( )
{
count++;
cout<<”No of objects created”<<count;
}
~alpha( )
{
cout<<”No of objects destroyed”<<count;
count–;
}
};
void main( )
{
cout<<”Main”;
alpha A1,A2,A3,A4;
{
cout<<”Block 1”;
alpha AS;
}
{
cout<<”Block2”;
alpha A6;
}
cout<<”Main”;
}
Constructor Overloading & its C++ Program
Constructor Overloading:- When more than one Constructor function is defined in a class, that is known as Constructor Overloading. e.g.
C++ Program for Overloaded Constructor
#include
#include
class length
{
Private:
int feet;
float inches;
Public:
Length( )
{
}
length(int ft,float in)
{
feet=ft;
inches=in;
}
void getln( )
{
cout<”Enter feet:”; cin>>feet;
cout<<”Enter inches:”; cin>>inches;
}
void showdata( )
{
cout<<feet<<”!-“<<inches”; } void add_len(length, length); }; void length::add_len(length d2,length d3) { inches=d2.inches+d3.inches; feet=0; if(inches>=12.0)
{
Inches=inches-12.0;
feet++;
}
feet=feet+d2.feet+d3.feet;
}
void main( )
{
length len1,len3;
length len2(11,6.25);
len1.getlen( );
len3.getlen( );
len3.add_len(len1,len2);
cout<<”Len1=”;
len1.showdata( );
cout<<”len2=”;
len2.showdata( );
cout<<”len3=”;
len3.showdata( );
getch( );
}

Function Overloading

It is a logical method of calling several functions with different arguments and data types that perform basically identical things by the same name.
Function Overloading is done with two ways:-
1. Different Number of Arguments.
2. Different Kind of Arguments.

Example according to 1 way
#include
#include
class example
{
Public:
void repchar( );
void repchar(char);
void repchar(char,int);
};
void example::repchar( )
{
for(int j=0;j<45;j++)
cout<<”*”;
cout<<endl;
}
void example::repchar(char ch)
{
for int j=0;j<45;j++)
cout<<ch;
cout<<endl;
}
void example::rechar(char ch,int n)
{
for(int j=0;j<n;j++)
cout<<ch;
cout<<endl;
}
void main( )
{
example e1;
e1.repchar( );
e1.repchar(‘=’);
e1.repchar(‘+’,30);
getch( );
}

Example according to 2 way
#include
#include
struct length
{
int feet;
float inches;
};
void disp(length);
void disp(float);
void main( )
{
length d1;
float d2;
cout<<”Enter feet”; cin>>d1.feet;
cout<<”Enter inches”; cin>>d1.inches;
cout<<”Enter entire length of class”; cin>>d2;
cout<<”d1=”;
disp(d1);
cout<<”d2=”;
disp(d2);
}
void disp(length dd)
{
cout<<dd.feet<<”-“<<dd.inches;
}
void disp(float dd)
{
int feet=dd/12;
float inches=dd-f2;
cout<<feet<<”-“<<inches; }

Operator Overloading The mechanism of giving special meaning to an operator is known as Operator Overloading. The operators which are not overloaded in C++ are:

1. Dot operator

2. , . *

3. ::(scope resolution operator)

4. sizeof

5. ? ::(conditional operator)

syntax return type classname::operator op(arg_list)

{ function Body } e.g. operator+(length l1,length l2);

Rules for Overloading operators :-

1. Only existing operator can be overloaded. New operator cannot be created.

2. The overloaded operator must have at least one operand that is of user-defined type.

3. We cannot change the basic meaning of an operator.

4. Overloaded operator follow the syntax rules of original operators. 5. We cannot use friend function to overload certain operators like =,( ),[],-> .
6. Binary arithmetic operators such as +,-,*, / must explicitly return a value. They must not attempt to change their arguments.
7. Unary operators, overloaded by means of a member function, take no explicit arguments and return no explicit values.
8. Binary operator overloaded the one operator and takes one explicit argument and those which are overloaded through a friend function take two explicit arguments.

Example of Overloading binary operator
#include
#include
class length
{
Private:
Int feet;
Float inches;
Public:
Length( )
{
feet=0;
inches=0.0;
}
length(Int ft, Float in)
{
feet=ft;
inches=in;
}
void getlen( )
{
cout<<”Enter feet”; cin>>feet;
cout<<”Enter inches”; cin>>inches;
}
void showlen( )
{
cout<<feet<<”—“<<inches; } length operator+(length) }; length length::operator+(length l1) { Int f=feet+l2.feet; Float i=inches+l2.inches; If(i>=12.0)
{
i=i-12.0;
f++;
}
return length(f,i);
}
void main( )
{
length len1,len3,len4;
len1.getlen( );
length l2(11,6.25);
len3=len1+len2;
len4=len1+len2+len3;
len1.showlen( );
len2.showlen( );
len3.showlen( );
len4.showlen( );
getch( );
}

Inline Member Function:- The inline specifier is a hint to the compiler that inline substitution of the function body is to be preferred to the usual function call implementation.
Advantage:-
1. The size of the object code is considerably reduced.
2. It increases the execution speed.
3. The inline member functions are compact function calls.
4. It save memory space, which becomes appreciable when a function is likely to be called many times.

Some of the situations where inline expansion may not work.
1. For functions returning values, if a loop, a switch or goto exists.
2. If functions contain static variables.
3. If inline functions are recursive.
Syntax:-
Inline function_header
{
function-body
}

C++ Program
#include
class sample
{
Public:
Void getdata( );
Void display( );
};
Inline void sample::getdata( )
{
cout<<”Enter a number”<<endl; cin>>x;
}
Inline void sample::display( )
{
cout<<”Entered Number is=”<<x;
cout<<endl;
}
void main( )
{
sample obj;
obj.getdata( );
obj.display( );
}

StaticVariables
Static variables are defined within a function and they have same scope rules of the automatic variables but in the case static variables,the contents of the variables will be retained throughout the program.
Syntax:-
Static datatype variable1,variable2,—- variablen
Static Data Member:- Static data members are data objects that are common to all the objects of a class. They exist only once in all objects of this class. static members can be of any one of groups:-Public, Private & Protected.
Rules:-
1. The access rule of the data member of a class is same for the static data member also.
2. Whenever a static data member is declared and it has only a single copy, it will be shared by all the instance of class.
3. The static data member should be created and initialized before the main( ) function control block begins.
C++ Program
#include
class sample
{
Private:
Static int counter;
Public:
Void display( );
};
Int sample::counter;
Void sample::display( );
{
cout<<”Contents of counter are”;
cout<<counter; void main( ) { sample obj; obj.display( );

}

StaticMember Functions:- The static function is a member function of class and it can manipulate only on static data member of the class.The static member function acts as global for members of its class without affecting the rest of the program. It does not have a this pointer so it can access non-static members of its class only by(.)(dot) or –>.
1. The static member function cannot be virtual function.
2. It cannot be declared with keyword Const.
C++ Program :-
#include
class Sample
{
Private:
Static int count;
Public:
Sample( );
Static void display( );
};
Int Sample::Count=0;
Sample::Sample( )
{
++Count;
}
void Sample::display( )
{
cout<<”Counter Value=”<<Count;
}
void main( )
{
cout<<”Before creation of Objects”;
Sample::display( )
Sample obj1,obj2,obj3,obj4;
Cout<<”After creation of objects”;
Sample::display( );
}
friend Function
Friend is a special mechanism for letting non-member functions access private data. A friend function may be either declared or defined within the scope of class definitions. The keyword friend informs the compiler that it is not a member of the class.
Characteristics of friend function:-
1. It is not in the scope of the class to which it has been declared as friend.
2. It cannot be called using the objects of that class.
3. It cannot access the member names directly and has to use an object name and dot membership operator with each member name.
4. It cannot declare either in Public or the Private part of a class without affecting its meaning.
5. Usually, it has the objects as arguments.

Friend function can be used for two different purpose
1. It is used to access the private data members of any class.
2. It is used to increase the versatility of overloaded operators.

C++ Program for Ist way:-
#include
class beta;
class alpha
{
Private:
Int data;
Public:
Alpha( )
{
data=3;
}
friend int frifunc(alpha,beta);
class beta{
Private:
Int data;
Public:
Beta( )
{
data=0;
}
frifune(alpha,beta)
int frifunc(alpha a,beta b)
{
return(a.data+b.data);
}
void main( )
{
alpha aa;
beta bb;
cout<<frifunc(aa,bb);
}
C++ Program for 2nd way :-
#include
class length
{
Private:
Int feet;
Float inches;
Public: length( )
{
feet=0;
inches=0.0;
}
length(float fltfeet)
{
feet=int(fetfeet);
inches=12*(fltfeet_feet);
}
length(int ft,float in)
{
feet=ft;
inches=in;
}
void showlen( )
{
cout<<feet<<”—-“<<inches; } friend length operator+(length,length); }; length operator+(length l1,length l2) { int f=l1.feet+l2.feet; float i=l1.inches+l2.inches; if(i>=12.0)
{
i=i-12.0;
f++;
}
return length(f,i);
}
void main( )
{
length l1=2.5;
length l2=1.25;
length l3;
l1.showlen( );
l2.showlen( );
l3=l1+10.0;
l3.showlen( );
l3=10.0+l1;
l3.showlen( );
}
Virtual Function :- When we use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword Virtual preceding its normal declaration which function is made virtual C++ determines which function to use at run time based on the type of object pointed to by base pointer, rather than the type of the pointer.
e.g.
#include
class Base
{
Public:
Void display( )
{
cout<<”Display Base”;
}
Virtual void show( )
{
cout<<”Show Base”;
}
};
class derived:Public Base
{
Public:
Void display( )
{
cout<<”Display derived”;
}
void show( )
{
cout<<”Show derived”;
}
};
main( )
{
Base B;
Derived D;
Base *ptr;
Cout<<”Ptr points to Base”;
Ptr=&B;
Ptrdisplay( );
Ptrshow( );
Cout<<”P?TR Points to Derived”;
Ptr=&D;
Ptrdisplay( );
Ptrshow( );
}

As function display( )is a virtual .

Both time ptr points to Base Base function display( ) is called, but as
Display base
Show Base show( ) function is virtual compiler will resolve and know that first time Base::show( ) is called and second time Derived ::show( ) to call.
Ptr points to Derived
Display base
Show derived

Rules/Characteristics of Virtual functions:-
1. The virtual functions must be members of some class.
2. It cannot be static members.
3. They are accessed by using object pointers.
4. The Virtual function can be a friend of another class.
5. A virtual function in Base class must be defined, even if it may not be used.
6. The Prototype of Base class version and all of derived class version must be identical.
7. We cannot have virtual Constructor.
8. While a base pointer can points to any type of derived object, reverse is not true.
9. If a virtual function is defined in the base class, it need not be redefined in the derived class.

Pure Virtual Function :- Because a virtual function in Base class must be defined, even if it may not be used, so we defined a “do-nothing” function of the form:-
Virtual void display( )=0;
These types of functions which are empty are known as Pure Virtual functions.
Copy Constructors :-
Copy Constructor is always used when the compiler has to create a temporary object of a class object.
Uses:-
1. The initialization of an object by another object of the same class.
2. Return of objects as a function value.

A copy Constructor is declared with one parameter, a reference to the type of same class. Usually this parameter is Const.

C++ Program
#include
class code
{
Int id;
Public:
Code( )
{
}
Code(int a)
{
id=a;
}
Code(Code &x) //Copy Constructor
{
id=x.id;
}
void display( )
{
cout<<id;
}
};
void main( )
{
code A(100);
Code B(A); //Copt Constructor called
Code C=A; //Copy Constructor called again
Code D;
D=A;
A.display( );
B.display( );
C.display( );
D.display( );
}
This pointer: – This pointer is a pointer to an object of the type of the class and its points to the object for which the member function is currently executing.
Or this pointer is a variable which is used to access the address of the class itself. The this pointer does not exist in static member functions.

C++ program to access member data with this pointer
#include
class Sample
{
Private:
int value;
Public:
void display( );
};
void Sample::display( )
{
thisvalue=20;
cout<<”Contents of value are=”<<thisvalue;
cout<<endl;
}
void main( )
{
Sample obj1;
obj1.display( );
}

Streams
Stream classes: – A stream is a general name given to a flow of data.
The class for file I/O are ifstream for input files, ofstream for output files and fstream for files that are used for both input and output.The ifstream ,ofstream and fstream classes are declared in the fstream.h file.
String I/O
C++ program for writing string
#include
void main( )
{
ofstream outfile(“Test.txt”);
outfile<<”Welcome”;
outfile< outfile<<”writing strings to file”;
}
C++ program for reading string
#include
void main( )
{
const int Max=80;
char buffer[Max];
ifstream infile(“Test.txt”);
while(infile)
{
infile.getline(buffer,Max);
cout<<buffer;
}
}

Object I/O
The following program shows how to write and read objects in/from file
#include
#include
class Person
{
Protected:
Char name[40];
int age;
Public:
void getdata(void)
{
cout<<”Enter Name”; cin>>age;
}
void Showdata(void)
{
cout<<”Name=”<<name;
cout<<”Age”<<age;
}
};
void main(void)
{
char ch;
Person pers;
Fstream file;
file.open(“Person.dat”,ios::out||ios::in);
do
{
cout<<”Enter Person’s data”;
Pers.getdata( );
file.write((char*)&Pers,sizeof(Pers));
cout<<”Enter another Person y/n?”; cin>>ch;
}
while(ch==’y’);
file.seekg(0);
file.read((char*)&pers,size);
while(!file.of( ))
{
cout<<”Person”; Pers.showdata( ); File.read((char*)&Pers,sizeof()>>
}
}

INHERITANCE
Inheritance is the mechanism of creating new classes, called derived classes from existing or base classes. The derived class inherits some or all of the traits of the base class but can add refinements and any additional features of its own properties.
The main advantages of Inheritance are:
• Re-usability of the code
• Increase reliability of the code
• Addition of some enhancements to the base class

Types of Inheritance
1. Single Inheritance

2. Hierarchical Inheritance

3. Multiple Inheritance

4. Multilevel Inheritance

5. Hybrid Inheritance

C++ program
//Single Inheritance
#include
class B
{
int a;
Public:
int b;
void get_ab( );
int get_a;
void show_a( );
};
class D:Public B
{
int c;
Public:
void mul( );
void display( );
};
void B::get_ab( )
{
a=5;
b=10;
void B::get_a( );
{
return a;
}
void B::show_a( )
{
cout<<”a=”<<a;
}
void D::mul( )
{
c=b*get_a( );
}
void D::display( )
{
cout<<”a=”<<get_a( );
cout<<”b=”<<b;
cout<<”c=”<<c;
}
main( )
{
D d;
d.get_ab( );
d.mul( );
d.show_a( );
d.display( );
d.b=20;
d.mul( );
d.display( );
}

Visibility of Inherited Members

Base Class          Derived Class                Visibility
Visibility             Public derivation        Private derivation

Private                Not Inherited              Not Inherited

Protected            Protected                     Private

Public                   Public                           Private

Multilevel Inheritance
#include
class student
{
Protected:
int roll_number;
Public:void get_number(int);
Void put_number( );
};
void student: :get_number(uint a)
{
roll_number=a;
}
void student::put_number( )
{
cout<<”Roll Number:”<<roll_number<<”\n”;
}
class Test:Public student //First Level Derivation
{
Protected:
Float sub1;
Float sub2;
Public:
Void get_marks(float,float);
Void put_marks(void);
};
void Test::get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
void Test::put_marks( )
{
cout<<”Marks in Sub1=”<<sub1<<”\n”;
cout<<”Marks in Sub2=”<<sub2<<”\n”;
}
class result:Public Test //Second Level Derivation
{
float Total; //Private by default
Public:
Void display(void);
};
void result::display(void)
{
Total=sub1+sub2;
Put_number( );
Put_marks( );
cout<<”Total=”<<Total<<”\n”;
}
main( )
{
result student1; //student1 created
student1.get_number(111);
student.get_marks(75.0,59.5);
student1.display( );
}

Multiple Inheritance
#include
class r
{
Protected:
int m;
Public:
void get_m(int);
};
class N
{
Protected:
int n;
Public:
void get_n(int);
};
class P:public M,public N
{
public:void display(void);
};
void M:get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
}
void P::display(void)
{
cout<<”m=”<<m<<”\n”;
cout<<”n=”<<n<<”\n”;
cout<<”m*n=”<<m*n<<”\n”;
}
main( )
{
P b;
b.get_m(10);
b.get_n(20);
b.display();
}

Virtual base

#include
class student
{
Protected:
Int roll_number;
Public:
Void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<”RollNo:”<<roll_number<<”\n”;
}
};
class Test:virtual Public student
{
Protected:
float part1,part2;
Public:
Void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<”marks obtained:”<<”\n”;
cout<<”Part1=”<<part1<<”\n”;
cout<<”Part2=”<<part2<<”\n”;
}
};
class Sports: Public virtual student
{
Protected:
float score;
Public:
Void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<”Sports Wt:”<<score<<”\n\n”;
}
};
class result:public Test,public sports
{
float Total;
public:
void display(void);
};
void result::display(void)
{
Total=part1+part2+score;
put_number( );
put_marks( );
put_score( );
cout<<”Total Score:”<<Total<<”\n”;
}
main( ){
result student_1;
student_1.get_numbver(678);
student_1.get_marks(30.5,25.5);
student_1.get_score(7.0);
student_1.display( );
}

UGC NET PROGRAMMING IN C AND C++ QUESTIONS FROM OLD PAPERS

DEC 2015



Q1.  Consider the following program :
#include<stdio.h>
main( )
{
int i, inp;
float x, term=1, sum=0;
scanf(“%d %f “, & inp, &x);
for(i=1; i<=inp; i++)
{
term = term * x/i;
sum = sum + term;
}
printf(“Result = %f\n”, sum);
}
The program computes the sum of which of the following series ?
(1)     x +  x2/2  +  x2/3 + x2/4 +…    (2)   x +  x2/2!  +  x2/3! + x2/4! +…
(3)     1 +  x2/2  +  x2/3 + x2/4 +…    (4)  1 +  x2/2  +  x2/3 + x2/4 +…

Q2.    Consider the following two statements :
(a)    A publicly derived class is a subtype of its base class.
(b)    Inheritance provides for code reuse.
Which one of the following statements is correct ?
(1)    Both the statements (a) and (b) are correct.
(2)    Neither of the statements (a) and (b) are correct.
(3)    Statement (a) is correct and (b) is incorrect.
(4)    Statement (a) is incorrect and (b) is correct.

 

JUNE 2015

Q3. What is the output of the following program ?
(Assume that the appropriate preprocessor directives are included and there is no syntax error)
main ()
{
charS[]=’ABCDEFGH”;
printf (“%C”, * (& S[3]));
printf (“%s”,S+ 4);
printf (“%u”, S);
/* Base address of S is 1000 */
}
(1) ABCDEFGH1000
(2) CDEFGH1000
(3) DDEFGHH1000
(4) DEFGH1000

Q4.  Which of the following, in C++, is inherited in a derived class from base class ?
(1) constructor (2) destructor (3) data members (4) virtual methods

Q5.  Given that x = 7.5, j = P1.0, n = 1.0, m = 2.0
thevalueofPPx I j==x>n> = mis:
(1) 0 (2) 1 (3) 2 (4) 3

Q6.  Which of the following is incorrect in C++ ?
(1) When we write overloaded function we must code the function for each usage.
(2) When we write function template we code the function only once.
(3) It is difficult to debug macros
(4) Templates are more efficient than macros

Q7. When the inheritance is private, the private methods in base class are in the
derived class (in C++).
(1) inaccessible (2) accessible (3) protected (4) public

DEC 2014

Q8. What will be the output of the following ‘C’ code ?
main ( )
{ int x = 128;
printf (“\n%d”, 1 + x ++);
}
(A) 128          (B) 129
(C) 130          (D) 131

Q9. What does the following expression means ?
char *(*(* a[N]) ( )) ( );
(A) a pointer to a function returning array of n pointers to function returning character pointers.
(B) a function return array of N pointers to functions returning pointers to characters
(C) an array of n pointers to function returning pointers to characters
(D) an array of n pointers to function returning pointers to functions returning pointers to characters.

Q10.  Which of the following is not a member of class ?
(A) Static function       (B) Friend function
(C) Const function       (D) Virtual function

Q11. When an array is passed as parameter to a function, which of the following statements is correct ?
(A) The function can change values in the original array.
(B) In C, parameters are passed by value, the function cannot change the original value in the array.
(C) It results in compilation error when the function tries to access the elements in the array.
(D) Results in a run time error when the function tries to access the elements in the array

Q12. Which of the following differentiates between overloaded functions and overridden functions ?
(A) Overloading is a dynamic or runtime binding and overridden is a static or compile time binding.
(B) Overloading is a static or compile time binding and overriding is dynamic or runtime binding.
(C) Redefining a function in a friend class is called overloading, while redefining a function in a derived class is called as overridden function.
(D) Redefining a function in a derived class is called function overloading, while redefining a function in a friend class is called function overriding.

JUNE 2014

Q13. Match the following :
List -I                                                   List – II
a. Automatic storage class           i. Scope of the variable is global.
b. Register storage class              ii. Value of the variable persists between different function calls.
c. Static storage class                   iii. Value stored in memory and local to the block in which the variable is defined.
d. External storage class              iv. Value stored in CPU registers.
Codes :
a b c d
(A) iii iv i ii
(B) iii iv ii i
(C) iv iii ii i
(D) iv iii i ii

Q14. When we pass an array as an argument to a function, what actually gets passed ?
(A) Address of the array
(B) Values of the elements of the array
(C) Base address of the array
(D) Number of elements of the array

Q15. While (87) printf(“computer”); The above C statement will
(A) print “computer” 87 times
(B) print “computer” 0 times
(C) print “computer” 1 times
(D) print “computer” infinite times

Q16. A friend function can be used to
(A) avoid arguments between classes.
(B) allow access to classes whose source code is unavailable.
(C) allow one class to access an unrelated class.
(D) None of the above

DEC 2013

Q17. What does the following declaration mean ?
int (*ptr) [10];
(A) ptr is an array of pointers of 10 integers.
(B) ptr is a pointer to an array of 10 integers.
(C) ptr is an array of 10 integers.
(D) none of the above.

Q18. Which of the following has compilation error in C ?
(A) int n = 32 ;
(B) char ch = 65 ;
(C) float f= (float) 3.2;
(D) none of the above

Q19. Which of the following operators can not be overloaded in C+ + ?
(A) * (B) + =
(C) = = (D) ::

Q20. allows to create classes which are derived from other classes, so that they automatically include some of its “parent’s” members, plus its own members.
(A) Overloading
(B) Inheritance
(C) Polymorphism
(D) Encapsulation

Q21. The correct way to round off a floating number x to an integer value is
(A) y = (int)(.r + 0.5)
(B) y = int (x + 0.5)
(C) y = (int)*+ 0.5
(D) y = (int)((im> + 0.5)

JUNE 2013

Q22. When the following code is executed what will be the value of x and y?
int x = l, y=0;
y = x++;
(A) 2,1
(B) 2,2
(C) 1,1
(D) 1,2

Q23.. How many values can be held by an array A(-1,m;1 ,m) ?
(A) m
(B) m2
(C) m(m+l)
(D) m(m+2)

Q24. What is the result of the expression
(1 &2)+(3/4) ?
(A) 1
(B) 2
(C) 3
(D) 0

Q25.  How many times the word ‘print’ shall be printed by the following program segment?
for(i=1, i≤2, i++)
for(j=1, J≤2, J++)
for(k=1, k≤2, k++)
printf(“print/n”)
(A) 1
(B) 3
(C) 6
(D) 8

DEC 2012

Q26. The ‘C’ language is
(A)    Context free language
(B)    Context sensitive language
(C)    Regular language
(D)    None of the above
JUNE 2012

Q27. printf(“%c”, 100);
(A) prints 100
(B) prints ASCII equivalent of 100
(C) prints garbage
(D) none of the above

JUNE 2011

Q28. What features make C++ so powerful ?
(A) Easy implementation
(B) Reusing old code
(C) Easy memory management
(D) All of the above

Q29. . The goal of operator overloading is
(A) to help the user of a class
(B) to help the developer of a class
(C) to help define friend function
(D) None of the above

Q30. The scheme of which interpreter translates the source program is known as
(A) Paragraph by paragraph
(B) Instruction by instruction
(C) Line by line
(D) None of the above

Q31. Portable program means
(A) Program with wheels
(B) Independent from its authors
(C) Independent of platform
(D) None of the above

DEC 2010

Q32. How many of the following declarations are correct ?
int z = 7.0;
double void = 0.000;
short array [2] = {0, 1, 2};
char c = “\n”;
(A) None
(B) One is correct
(C) Two are correct
(D) All four are correct
Hint: Void is keyword, array[2] cannot hold 3 values, “” cannot be used in char

Q33. The value of the following expression (13 / 4 * 3) % 5 + 1 is
(A) 5.75
(B) 2.95
(C) 1.4875
(D) 5

Q34. Which one of the following will set the value of y to 5 if x has the value 3, but not otherwise ?
(A) if (x = 3) y = 5
(B) if x = = 3 (y = 5)
(C) if (x = = 3); y = 5
(D) if (x = = 3) y = 5

.Q35. Which one of the following sentences is true ?
(A) The body of a while loop is executed at least once.
(B) The body of a do … while loop is executed at least once.
(C) The body of a do … while loop is executed zero or more times.
(D) A for loop can never be used in place of a while loop.

JUNE 2010

Q36. The statement print f (“ % d”, 10 ? 0 ? 5 : 1 : 12); will print
(A) 10  (B) 0  (C) 12  (D) 1

Q37. What will be the output of the following c-code ?
void main ( )
{
char *P = “ayqm” ;
char c;
c = ++*p ;
printf (“%c”, c);
}
(A) a (B) c (C) b  (D) q

Q38. Member of a class specified as _______ are accessible only to method of the class.
(A) private (B) public (C) protected (D) derive

Q39. Match the following :
(a) Garbage collection in                          1. Java
(b) Nameless object                                 2. generic programming
(c) Template support                                3. defines a class
(d) A forward reference                           4. member function
(e) Derived class inherits from base class  5. within a statement
Codes :
(a) (b) (c) (d) (e)
(A) 1   5    4   2   3
(B) 1   5    2   3   4
(C) 5   1    2   3   4
(D) 5   4    3   1   2

Q40. The data type created by the data abstraction process is called
(A) class
(B) structure
(C) abstract data type
(D) user defined data type

EDUCATION JOCKEY

educationjockey@gmail.com



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.