Unit 1 - IV Practice Questions SASTRA University First Year
Unit 1 - IV Practice Questions SASTRA University First Year
#include <iostream>
using namespace std;
struct Rectangle {
int width, height;
Rectangle(int w, int h)
{
width = w;
height = h;
16 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? for while do-whileall looping processes require that the iterations be1known
Find the output of the following
#include <iostream>
using namespace std;
int main()
{
18 int a = 10; 1010 10 infinitely print 10 compile time error 3
if (a < 20)
{ cout << a; }
break;
return 0;
}
Identify the output of a C++ program by passing an entire Employee struct to
printInformation()
#include <iostream>
using namespace std;
struct employee
{
short id;
int age;
double wage;
};
void printInformation(Employee employee)
19 The output will be displayed ID:14,Age:32,Wage: C
24
ompilation Error Joe Garbage Value 5
{
cout << "ID: " << employee.id << "\n";
cout << "Age: " << employee.age << "\n";
cout << "Wage: " << employee.wage << "\n";
}
int main()
{
Employee joe { 14, 32, 24};
printInformation(joe);
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 2
keyword inline treated as a inline function function function
Unit 2 - Practice questions
Q. No Questions Column1 Option 2 Option 3 Option 4 Marks
1 What is the output of the given code: 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 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;
}
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;
}
nobody does like this nobody
5 Objects of an array is called as 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 obAjercrat yth_eNna_m_e_[_4_]._d_a_ta___v_a_r_ia_b_l_
e_snyaD
nmtaatexa;_sT
hoyupledAbrerauys_eN d.ame[4].data_Avrarariya_bN lea_m
naem
[4e];.data_variable_naAmrrea.yv_alNuaem
; e[4].data_variable_name(value);
7 Is an array of characters always a string? Yes, always Yes, if each character is terminateNdob, ysincuelleach character is terminNaote, dnebvyenr ull
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;
} 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;
}
strauniversity. university. rauniversity. stra
10 Pick the incorrect statement about Character-Array. Character-Array can be terminateCdhbayraactneur-lA l crhraayrahcatesra(‘s\0ta’t)ic size Character-Array has a dynamic sC izhearacter-Array has a threat of array-decay
11 Pick the correct statement about string objects in C++. String objects must be terminatedSbtryinagnoubljlecchtsarhaacvtera(‘s\t0a’t)ic size String objects have a dynamic sizSetring 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;
}
H e Error o
13 What will be the output of the following code? 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? 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? 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? 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 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 What will be the output of the following program? 10 error Indeterminate value or Segmenta1tion 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? 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? 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? ST TR error inderminate value
#include<iostream>
#include<string>
using namespace std;
int main()
{
string aName="SASTRA";
cout << aName.substr(3, 2) << endl;
}
int main()
{ find::Set(33);
find::Display();
return 0;
}
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) 4 8 2 5
27 Which of the following type of data member can be shared by all instances of its cIlanshse?rited Public Friend Static
28 Constructor is executed when _____. 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 __________ . 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 wouldwork without any problem call itself recursively call one-argument constructor Run-time error
of the class
35 Which of the following statement is correct about destructors? A destructor has no return A destructors return type is A destructor has void return A destructor has integer return
type. 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? 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> 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> 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> 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> 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? By changing the number of By changing the parameters By changing both the number No chance for destructor
parameters type of parameters and their type overloading
43 What is the difference between struct and class in C++? 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 by default and members of public and structures don’t
have constructors and struct are public by default. have virtual functions
destructors 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? Static methods cannot be Static data members can only Non-static data members can Static methods can only access
overloaded. be accessed by static methods. be accessed by static methods. static members (data and
methods)
46 #include <iostream> 0 Linker Error: Undefined Linker Error: Cannot access Linker Error: multiple
using namespace std; reference B::a static a functions 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. 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;
}
48 #include <iostream> Compiler Error 10 0 Runtime Error
int main()
{
const int x;
x = 10;
cout<< x
return 0;
}
UNIT - 3
#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 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 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 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() 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 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
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"; }}; 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 { 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 }; 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 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"; } 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 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:
D(){cout<<"7";}
UNIT - 4
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. 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”); { 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 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;
}