Practice Questionswith Answers SASTRA University First Year
Practice Questionswith Answers SASTRA University First Year
Correct
Q. No Questions Column1 Option 2 Option 3 Option 4 Marks
Option
Find the output of the following code #include<iostream>
#include<iomanip>
using namespace std;
int main()
{ 17.61, 10, -6 and all outputs are 17.61, +10, -6 and all outputs 17.61, 11, -7 and all outputs 1761E-3,10, -7 and all outputs
1 double data1=413E-2,data2=52.4E-1; 3 4
right aligned are left aligned are right aligned are right aligned
data1+=++data2;
cout<<setw(10)<<static_cast<float>(++data1+data2++)<<endl;
cout<<setw(10)<<static_cast<int>(+data1--)<<endl;
cout<<setw(10)<<static_cast<int>(-data2--);
}
#include<iostream>
int main()
{
int x;
cout<<"Enter the value of x";
cin>>x;
2 } 2 (i) and (iv) have to be done (i) and (ii) have to be done (i), (ii), (iii) have to be done All have to be done 2
The complier signals error when compiling the code. Pick the options for the code
to be compiled successfully
(i) cout should be written as std::cout
(ii) cin should be written as std::cin
(iii) int x should be written as std::int x
(iv) return 0 should be included before '}'
3 Can a Structure contain pointer to itself? 1 Yes No Compilation Error Run time Error 1
4 In place of structure which other data type can be used in programming? 3 int float class enum 1
5 Which of the following specifiers need not be honored by the compiler? 3 register Inline static both 1 &2 3
Const qualifier can be applied to which of the following?
i) Functions inside a class
6 ii) Arguments of a function 2 I, II and III I, II , IIIand IV II , IIIand IV I only 3
iii) Static data members
iv) Reference variables
You can define your own data Program statements are simpler An OO program can be taught It is easier to conceptualize an
14 Which of the following are good reasons to use an object-oriented language? 1,4 2
types. than in procedural languages. to correct its own errors. OO program.
15 The data elements in the structure are also identified as 2 objects members data objects & data 1
What will be the output of the following C++ code
#include <iostream>
using namespace std;
struct Rectangle {
int width, height;
Rectangle(int w, int h)
{
width = w;
height = h;
16 3 258 253 252 262 3
};
int areaOfRectangle() {
cout<<"Area of Rectangle is: "<<(width*height); }
}
int main() {
Rectangle rec=Rectangle(4,6);
rec.areaOfRectangle();
return 0;
}
17 Which looping process is best used when the number of iterations is known? 1 for while do-while all looping processes require that the iterations be k1nown
Find the output of the following
#include <iostream>
using namespace std;
int main()
{
18 int a = 10; 4 1010 10 infinitely print 10 compile time error 3
if (a < 20)
{ cout << a; }
break;
return 0;
}
Inline function should have a Single return statement may be A class can not have inline A structure can not have inline
23 Choose the correct answer 1 2
keyword inline treated as a inline function function function
Unit 2 - Practice questions
Correct
Q. No Questions Column1 Option 2 Option 3 Option 4 Marks
Option
1 What is the output of the given code: 3 compile error no error no output Constructor called Constructor called Constructor called
#include<iostream>
using namespace std;
class test {public:
test() { cout << "Constructor called"; }
};
int main()
{
test t1, *t2;
return 0;
}
2 2 i-True, ii-True i-False, ii-True i-True, ii-False i-True, ii-True
Which of the following statements about member functions are True or False.
i) A member function can call another member function directly by using the dot
operator.
ii) Member function can access the private data of the class.
3
What will be the output of the following C++ code? #include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
strcpy( str3, str1);
strcat( str1, str2);
len = strlen(str1);
cout << len << endl;
return 0;
}
4 5 55 11 10
4
What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("nobody does like this");
string key ("nobody");
size_t f;
f = str.rfind(key);
if (f != string::npos)
str.replace (f, key.length(), "everybody");
cout << str << endl;
return 0;
}
4 nobody does like this nobody
5 Objects of an array is called as 1 Elements of an array Functions of an array Indexes of an array All of them
6 If an array of objects is of size 10 and a data value have to be retrieved from 5th object then1______A
__rr_a_y___N_a_m
__e[s4y]n.dtaaxtas_hvoauriladbble_unsaeD
m
d.aet;a_Type Array_Name[4].data_A varriaayb_leN_anmam
e[e4;].data_variable_naA
mrer.avya_luNea; me[4].data_variable_name(value);
7 Is an array of characters always a string? 4 Yes, always Yes, if each character is terminateN
d ob,ysninuclle each character is terminNatoed, nbeyvnerull
8 What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char const *argv[])
{
string s("a");
cout<<s;
return 0;
} 1 a empty string Error Segmentation fault
9 What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("sastrauniversity.");
cout << str.substr(2).substr(4) << endl;
return 0;
}
2 strauniversity. university. rauniversity. stra
10 Pick the incorrect statement about Character-Array. 3 Character-Array can be terminatedChbayracnteurll-A chrraaryachtears(‘a\0st’a)tic size Character-Array has a dynamic sizCeharacter-Array has a threat of array-decay
11 Pick the correct statement about string objects in C++. 3 String objects must be terminated Sbtyriangnuolbl jcehcatsrahcatvere (a‘\s0t’a)tic size String objects have a dynamic sizeString objects use extra memory than required
12 What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main()
{
char str[] = "Hello World";
cout<<str[0];
return 0;
}
1 H e Error o
13 What will be the output of the following code? 2 5 0 garbage value Segmentation fault
# include<iostream>
int main ()
{
int a[3][3]={1,2,3,4,5,6,7,8};
cout<<a[2][2];
return 0;
}
14 What will be the output of the following code? 4 cannot create const array Syntax error in declaring const array 2 const array can't be modified
# include<iostream>
int main ()
{
int const a[3]={1,2};
a[2]=2;
cout<<a[2];
return 0;
}
15 What will be the output of the following code? 3 Runtime error Syntax error arrays are not equal arrays are equal
# include<iostream>
int main ()
{
int a[3]={1,2,3};
int b[3]={1,2,3};
if (a==b)
{cout<<"arrays are equal";}
else
{cout<<"arrays are not equal";}
return 0;
}
16 How many times the constructor is called? 2 Runtime error 10 0 1
# include<iostream>
class Student{
int regno;
public:
Student(){}
};
int main ()
{
Student a[10];
return 0;
}
16 . Select the possible implementation/s for StrCmp() ,so that the program below 1,3 strcmp(str1.c_str() ,str2.c_str()); strcmp(str1,str2); ((str1<str2)?-1:((str1>str2)?+1:0))(;(str1<str2)? +1 ((str1>str2) ? -1:0));
prints the following output .
Output:
bat Less than cat
rat Greater than cat
hat Equal to hat
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int StrCmp(string& str1,string& str2)
{
return ……………………………….
}
int main()
{
string str1="???";
string str2="???";
str1="bat";str2="cat";
cout<<str1<<(((StrCmp(str1,str2)<0)?" Less than
":((StrCmp(str1,str2)>0)?"Greater than":"Equal to")))<<str2<<endl;
str1="rat";str2="cat";
cout<<str1<<(((StrCmp(str1,str2)<0)?" Less than":((StrCmp(str1,str2)>0)?"
Greater than ":"Equal to")))<<str2<<endl;
17 strh1a=t"w
W hailtl";bsetr2th=e"hoautt"p;ut of the following program? 3 10 error Indeterminate value or Segmentat1ion fault
#include<iostream>
using namespace std;
int main()
{
Int arr[1]={10};
Cout<<arr[1];
}
18 What will be the output of the following program? 3 c empty string error Segmentation fault
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s('c');
cout<<s;
return 0;
}
19 What will be the output of the following program? 4 arr==ptr arr==a a==b no output
#include<iostream>
using namespace std;
int main()
{
char arr[]= "ABC";
char ptr[]= "ABC";
char a[]={'A','B','C'};
char b[]={'A','B','C'};
if(arr==ptr)
cout<<"arr==ptr";
else if(arr==a)
cout<<"arr==a";
else if(a==b)
cout<<"a==b";
else
cout<<"no output";
}
20 What will be the output of the following program? 2 ST TR error inderminate value
#include<iostream>
#include<string>
using namespace std;
int main()
{
string aName="SASTRA";
cout << aName.substr(3, 2) << endl;
}
21
Consider the program below.
#include <iostream>
using namespace std;
typedef struct Complex {
mutable double re;
mutable double im;
} Complex;
int main() {
const Complex c={2,4};
c.re = 5;
cout << c.re<< ","<<c.im;
return 0;}
What will be the output / error? 2 2,4 5,4 Compilation Error:constant objectCcocmanpniloatiboen aesrsriogrn:Ctom
{2p,l4e}x::re is a read-only object
22
int main()
{ find::Set(33);
find::Display();
return 0;
}
1 Compile time error Output garbage value 0 33
26 #include<iostream> using
namespace std; class
First
{
int y;
}; int main() { First
f1,f2} How
much memory the above program will be occupied?(Assume integer datatype
occupies 4 bytes) 2 4 8 2 5
27 Which of the following type of data member can be shared by all instances of its class? 4 Inherited Public Friend Static
28 Constructor is executed when _____. 3 a class is declared an object is used an object is created an object goes out of scope.
33 Copy constructor must receive its arguments by __________ . 2 only pass-by-value only pass-by-reference either pass-by-value or pass-by- only pass by address
reference
34 If the copy constructor receives its arguments by value, the copy constructor would 2 work without any problem call itself recursively call one-argument constructor of Run-time error
the class
35 Which of the following statement is correct about destructors? 1 A destructor has no return type. A destructors return type is A destructor has void return A destructor has integer return
always same as that of main(). type. type.
public:
int assign(int i) const {
a = i;
}
int return_value() const {
return a;
}
};
int main(int argc, char const *argv[])
{
A obj;
obj.assign(5);
cout<<obj.return_value();
}
37 What is the correct syntax of accessing a static member of a Class? 2 A.value A::value A->value Any member of class can be
class A accessed only by its object
{
public:
static int value;
};
38 #include <iostream> 3 Constructor called Nothing printed Error Segmentation fault
using namespace std;
class A{
A(){
cout<<"Constructor called";
}
};
int main(int argc, char const *argv[])
{
A a;
return 0;
}
39 #include <iostream> 1 1010 Some Unknown Values Error Segmentation fault
using namespace std;
class A{
public:
int a;
};
int main(int argc, char const *argv[])
{
A a1 = {10};
A a2 = a1;
cout<<a1.a<<a2.a;
return 0;
}
40 #include <iostream> 2 Main function Constructor called Run time error Compile time error
using namespace std; Constructor called Main function
class A{
public:
int a;
A(){
cout<<"Constructor called\n";
}
} a;
int main(int argc, char const *argv[])
{
cout<<"Main function\n";
return 0;
}
41 #include <iostream> 3 A's Constructor called B's Constructor called Error Segmentation fault
using namespace std; B's constructor called A's constructor called
class A{
A(){
cout<<"A's Constructor called\n";
}
};
class B
{
public:
A a;
B(){
cout<<"B's constructor called\ns";
}
};
int main(int argc, char const *argv[])
{
B b;
return 0;
}
42 How destructor overloading is done? 4 By changing the number of By changing the parameters By changing both the number of No chance for destructor
parameters type parameters and their type overloading
43 What is the difference between struct and class in C++? 2 All members of a structure are Members of a class are private All members of a structure are All of the above
public and structures don’t have by default and members of public and structures don’t have
constructors and destructors struct are public by default. virtual functions
When deriving a struct from a
class/struct, default access-
specifier for a base class/struct
is public and when deriving a
class, default access specifier is
private.
class Test
{
static int x;
public:
Test() { x++; }
static int getX() {return x;}
};
int Test::x = 0;
int main()
{
cout << Test::getX() << " ";
Test t[5];
cout << Test::getX();
}
45 Which of the following is true? 4 Static methods cannot be Static data members can only be Non-static data members can be Static methods can only access
overloaded. accessed by static methods. accessed by static methods. static members (data and
methods)
46 #include <iostream> 2 0 Linker Error: Undefined Linker Error: Cannot access Linker Error: multiple functions
using namespace std; reference B::a static a with same name get()
class A
{
private:
int x;
public:
A(int _x) { x = _x; }
int get() { return x; }
};
class B
{
static A a;
public:
static int get()
{ return a.get(); }
};
int main(void)
{
B b;
cout << b.get();
return 0;
}
47 Predict the output of following program. 4 Garbage Values 00 Compiler Error in line cout << Compiler Error in line cout <<
#include <iostream> t.getX() << " "; t.gety();
using namespace std;
class Point
{
int x, y;
public:
Point(int i = 0, int j =0)
{ x = i; y = j; }
int getX() const { return x; }
int getY() {return y;}
};
int main()
{
const Point t;
cout << t.getX() << " ";
cout << t.gety();
return 0;
}
Correct
Q.No. Questions Option1 Option2 Option3 Option4 Marks
option
Number of arguments to be passed for unary operator overloading
1 1 one two one or two one and two 1
function, provided the function is friend function
New operators cannot be Numbers of Operands Associativity of an operator
2 Restriction(s) for implementing operator overloading function 4 All the 3 options 3
created cannot be changed cannot be changed
#include<iostream>
using namespace std;
class A
{
int x, y, z;
public:
A()
{ x=0, y=0; z=0; }
A(int a, int b, int c) { x=a, y=b; z=c; }
bool operator==(A obj1, A obj2);
};
bool operator==(A obj1, A obj2)
error, operator overloading
{ return ( obj1.x == obj2.x && obj1.y == obj2.y && obj1.z == no error and output is Both error, data member are to
3 3 function to be declared as None of the these 4
obj2.z );} the values are not equal be declared as the public
friend
int main()
{
A obj1(1,2,3);
A obj2(1,3,4);
if(obj1 == obj2)
{ cout << "Both the values are equal"; }
else
{ cout << "Both the values are not equal"; }
}
If no error what is the output. If error, identify required corrections
#include <iostream>
using namespace std;
class A
{
private:
int x;
public:
A() : x(0){ }
A(int c) : {x=c }
int get_x() { return x; }
A operator ++ () {return A(++x); }
};
4 2 a and b a and c b and c c and d 5
int main()
{
A c1, c2;
c2 = c1++;
return 0;
}
Which line number will give the error and what is the required
correction?
(a) 16, argument to be passed to the operator overloading function
(b) 10, get_x( ) not used, it has to be removed
(c) 11, return A(x++);
(d) 9, no need of parameterized constructor
Assume that an integer takes 4 bytes and predict the output.
#include<iostream>
using namespace std;
class base {
int arr[10];
};
class b1: public base { };
5 2 40 80 4 0 2
class b2: public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout << sizeof(derived);
return 0;
}
class x
{
};
class y: public x
{
};
6 2 class x -> class y -> class z class z -> class y -> class x class y-> class z -> class x None of the these 3
class z: public y
{
};
z obj;
In the above definitions, the destructors will be fired in the
following sequence classclass x
What is the output of the following code,
#include<iostream>
using namespace std;
class P {
public:
void print()
{ cout <<" Inside P"; }
};
class Q : public P {
public:
7 void print() 2 Inside P Inside Q Compile Error None of the these 4
{ cout <<" Inside Q"; }
};
class R: public Q {
};
int main()
{
R r;
r.print();
return 0;
}
What is the output of the following program,
#include <iostream>
using namespace std;
class A {
int a;
public: A( int aa) { a = aa;}
void print ( ) { cout << " A = " << a; }
};
class B : public A {
int b;
9 3 10 20 30 Compile Error 5
public: B ( int aa, int bb) : A (aa) { b = bb;}
void print ( ) { cout << " B = "<< b; }
};
int main( )
{ A objA(10);
B objB(30, 20);
objA = objB;
objA.print( );
return 0;
}
Which of the following statement is correct regarding destructor of Destructor of base class Destructor of base class Destructor of base class Destructor of base class
10 2 2
base class? should always be static. should always be virtual. should not be virtual. should always be private.
What is the output of the program?
#include<iostream>
using namespace std;
class Base1
{public: Base1() { cout << "1"; }};
class Base2
11 {public: Base2() { cout << "2"; }}; 1 123 321 132 231 3
class Derived: public Base1, public Base2
{public: Derived() { cout << "3"; }};
int main()
{Derived d;
return 0;}
int main()
{
Derived d;
return 0;
}
Analyse the code and predict the output:#include<iostream>
using namespace std;
class Base {
public:
int fun() { cout << "Base::fun() called"; }
int fun(int i) { cout << "Base::fun(int i) called"; }
};
19 class Derived: public Base { 2 Compiler error Base::fun(int i) called Base::fun() called Derived::fun() called 4
public:
int fun() { cout << "Derived::fun() called"; }
};
int main() {
Derived d;
d.Base::fun(5);
return 0;
}
Analyse the code and predict the output:#include<iostream>
using namespace std;
class P {
public:
void print() { cout <<" Inside P"; }
};
class Q : public P {
public:
void print() { cout <<" Inside Q"; }
Compiler Error:
20 }; 3 Inside P Inside Q Inside P Inside Q 5
Ambiguous call to print()
class R: public Q { };
int main(void)
{
R r;
r.print();
return 0;
}
The above program’s output is
Analyse the code and predict the output: #include<iostream>
using namespace std;
class A {
public:
class B {
private:
int num;
public:
void getdata(int n) {
num = n;
}
21 3 Compiler Error Runtime Error 9 0 3
void putdata() {
cout<<"The number is "<<num;
}
};
};
int main() {
A :: B obj;
obj.getdata(9);
obj.putdata();
return 0;
}
Analyse the code and predict the output: #include<iostream>
class Q : public P {
public:
22 void print() { cout <<" Inside Q"; } 2 Inside P Inside Q Compile Error Runtime Error 4
};
class R: public Q { };
int main(void)
{
R r;
r.print();
return 0;
}
#include<iostream>
using namespace std;
class A
{
public:
A(){ cout <<"1";}
A(const A &obj){ cout <<"2";}
};
class B: virtual A
{
public:
B(){cout <<"3";}
23 4 2 4 6 All of the these 5
B(const B & obj){cout<<"4";}
};
class C: virtual A
{
public:
C(){cout<<"5";}
C(const C & obj){cout <<"6";}
};
class D:B,C
{
public:
UNIT - 4
Correct
Q.No. Questions Option1 Option2 Option3 Option4 Marks
option
what will be the output of following code
#include <iostream>
using namespace std;
int add(int first, int second)
{ return first + second + 15; }
int operation(int first, int second, int
(*functocall)(int, int))
{ return (*functocall)(first, second); }
1 3 25 35 40 45 4
int main()
{
int a;
int (*plus)(int, int) = add;
a = operation(15, 10, plus);
cout << a;
return 0;
}
If you wanted to sort many large objects or place them in an array and place pointers to them in an place them in a linked list and place references to them in an
2 2 2
structures, it would be most efficient to sort the array. array and sort the array. sort the linked list. array and sort the array
Class Base
{public :
virual void show()=0;
{
cout<<”In base class”;
}
};
Class Derv: public Base
{
Public:
void show()
{cout<<”In derived class”;}
3 2 In base class In derived clE
asrsror due to Base class object crEearrtoiorndue to virtual function definition Error due to b.show() 2
};
void main()
{
Base b;
b.show();
Derv dv;
Base *a;
a=&dv;
a->show();
}
6 Identify the correct statements When data member is3n, o4n static, only one data valW
ueewciallnbpeatshsevN
reaolfu.oerfaapslalartarhgm
euecmtleaersnsstaoirnbejco
eocnptesy.mcornestirnucFtroier nd versUiosninogf a*tfhuicst(iopnoitnhtaenr),aws ea cmanemavboeridfucnrecatitoionn of extra3objects.
Consider the code snippet
int buff[MAX];
int main(){
……..
reinterpret_cast<char*>(buff) reinterpret_cast<char*>(buff)
7 ofstream os(“edata.dat”,ios::binary) 1 (char *)buff,MAX*sizeof(int) none of the above 1
,MAX*sizeof(int) ,sizeof(int)
os.write(_______,_________)………….} .what is
the first argument and second argument in the write
function respectively.
person pers;
infile.seekg(0,ios::in)
ifstream infile; infile.seekg(0,ios::end);
int endpos=infile.tellg();
infile.open(“GROUP.DAT”,ios::in|ios::binary); int endpos=infile.tellg();
int
8 Assume the GROUP.DAT file has 4 person objects. 1 int none of the above invalid 4
n=endposition/sizeof(person)
To determine the number of objects in the file ,the n=endposition/sizeof(person)
cout<<n;
corresponding cpp code is
Consider overloading of >> and<< operators for
reading from file and writing in the files. Now the
code for getting Distance object from user and
write it in the file object is
istream&operator
Distance dist1; ostream&operator<<(ostream
>>(istream&s,Distance &d)
ofstream ofile; &s,Distance &d)
{
9 ofile.open(“DIST.DAT”); 1 { none of the above invalid 4
s>>d.feet>>d.inches;
cout<<”\nenter distance”; s<<d.feet<<”\’-
return s;}
cin>>dist1; “<<d.inches<<’\”’;
ofile<<dist1; return s;}
for overloading the extraction operator, the code
required is
int main()
{
ofstream ofile;
char data[100];
ifstream ifile;
ofile.open ("text.txt");
ofile << "C++ Programming" << endl; Data written to file C++ Programming Data
16 3 C++ Programming Data written to file 4
cout << "Data written to file" << endl; C++ Programming written to file
ofile.close();
ifile.open ("text.txt");
while ( !ifile.eof() )
{
ifile.getline (data, 100);
cout << data << endl;
}
ifile.close();
return 0;
}