0% found this document useful (0 votes)
344 views16 pages

Unit 1 - IV Practice Questions SASTRA University First Year

This document contains 16 practice questions related to C++ programming concepts. The questions cover topics like structures, classes, functions, object-oriented programming, and more. For each question, there are typically 4 answer options to choose from.

Uploaded by

star
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views16 pages

Unit 1 - IV Practice Questions SASTRA University First Year

This document contains 16 practice questions related to C++ programming concepts. The questions cover topics like structures, classes, functions, object-oriented programming, and more. For each question, there are typically 4 answer options to choose from.

Uploaded by

star
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Unit 1 - Practice questions

Q. No Questions Column1 Option 2 Option 3 Option 4 Marks

Find the output of the following code #include<iostream>


#include<iomanip>
using namespace std;
int main()
{
17.61, 10, -6 and all outputs 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; 4
are 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 } (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? Yes No Compilation Error Run time Error 1
4 In place of structure which other data type can be used in programming? int float class enum 1
5 Which of the following specifiers need not be honored by the compiler? 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 I, II and III I, II , IIIand IV II , IIIand IV I only 3
iii) Static data members
iv) Reference variables

A referenced does not need to Whether a reference should be


A referenced has to be de- A referenced has to be double
7 Which of the following statement is correct? be de-referenced to access a de-referenced or not depends on 4
referenced to access a value de-referenced to access a value.
value. the type of the reference.
Choose the valid statements.
(I) int sum(int a, int b=20, int c);
8 (II) int sum(int a, int b=20, int c=30); I, II and III II only II , IIIand IV I only 3
(III) int sum(int a=10, int b=20, int c);
(IV)int sum(int a=10, int b, int c=30);

Because return by reference Because return by reference


Because compiler can’t create Because the temporary object
9 Why temporary object is not created in return by reference? just make the objects points to just make the object point to 4
temporary objects is created within the function
values memory location null
Consider the following statements
(i) Class is a description of a number of similar objects
10 (ii) Object is an instance of a class (i) and (ii) are correct (ii) and (iii) are correct (i) and (iii) are correct All three 1
(iii) The relation between derived class and base class is very similar to the
relation between objects and class.

Consider the the following code


#include<iostream>
using namespace std;
int main()
{
11 {0,5,5,5} {0,0,5,5} {0,0,0,5} {0,0,0,0} 3
int ftemp=41;
cout<<"The celsius temp is: "<<5/9*(ftemp-32); //out1
cout<<"\nThe celsius temp is: "<<5/9*static_cast<float>(ftemp-32); //out2
cout<<"\nThe celsius temp is: "<<static_cast<float>(5/9)*(ftemp-32); //out3
cout<<"\nThe celsius temp is: "<<static_cast<float>(5)/9*(ftemp-32); //out4
return 0;
}
The values of out1, out2, out3 and out4 are:
Consider the following C++ program
#include
<iostream>
using
namespace std;
int main() { cout
<<
"Test
Program" All four must be done.
12 ;return (i) and (ii) must be done. (i), (ii) and (iii) must be done. (i) and (iv) must be done Otherwise the error(s) won't be 2
0; corrected
}
The complier signals error when compiling the code. Choose the best answer to
correct the errors
(i) Write #include<iostream> in a single line
(ii) Write using namespace std; in a single line
(iii) Write { cout in the next line (iv)
Write "Test Program" in a single line
makes the program easier to may reduce the size of the
13 Dividing a prpgram into funcations. is the key to OOP. makes the program run faster. 1
conceptualize. program.
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? 2
types. than in procedural languages. to correct its own errors. OO program.
15 The data elements in the structure are also identified as 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 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;
}

If a function omits return


A function may have more than A function can return more A function can return only one
20 Pick correct answer statement, it may proceed other 4
one return statement than one value value to it's calling environment
statement without error
21 main(){ static int v=5; v--; if(v){main(); main();} Error 5 54321 0 5
Find output:
22 main(){ static int v=5; v--; if(v){main(); main();} 6,5 7,8 5,6 8,7 4

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;
}

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,4 5,4 Compilation Error:constant objecCt ocmcapnilnaotitobneearrsosirg:C
n otom{p2le,4x}::re is a read-only object
22
Consider the program below.
#include <iostream>
using namespace std;
int set_a(int i) {
cout << "init a :"<<i<<endl;
return i ;}
int set_b(int i) {
cout << "init b :"<<i<<endl;
return i ;}
int set_c(int i) {
cout << "init c :"<<i<<endl;
return i ; }
class MyC1ass {
int------------// LINE-I:declare the data members
public :
MyC1ass (int _a, int _b, int _ c): a(set_a(_a)),b(set_b(_b)), c(set_c(_c)){} };
int main() {
MyC1ass obj (1, 2, 3);
return 0;}
Fill in the blank at LINE—I such that the output is as follows:
init c:3
init b:2
init a:1 _a,_b,_c _c,_b,_a a,b,c c,b,a
23 Consider the program below.
#include <iostream>
using namespace std;
class MyC1ass {
public:
MyC1ass() { }// default constructor
MyC1ass (const MyC1ass& c2) {} // copy constructor
MyC1ass& func (MyC1ass &obj2) {
MyC1ass &obj3 = obj2;
return obj3;}
};
int main() {
MyC1ass objl;
objl . func(objl) ;
return 0;
}
How many times copy constructor will be invoked? 0 1 2 3
24 What is the default access specifier for structure? private public protected none of the above
25 #include<iostream> using
namespace std;
class find
{
static int y;
public:
static void Set(int xx)
{ y = xx;
}
void Display()
{ cout<< y ;
}
};
int find::x = 0;

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.

36 #include <iostream> 5 10 Error Segmentation fault


#include <string>
using namespace std;
class A
{
int a;

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.

44 #include <iostream> 00 55 05 Error


using namespace std;

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

Q.No. Questions Option1 Option2 Option3 Option4 Marks


Number of arguments to be passed for unary operator overloading
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 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 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 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;}

What is the output of the program?


#include <iostream>
using namespace std;
class BaseClass {
public:
void disp(){
cout<<"Function of Parent Class";
}
};
class DerivedClass: public BaseClass{ Function of Parent Class Function of Child Class
12 Function of Parent Class Function of Child Class 4
public: Function of Child Class Function of Parent Class
void disp() {
cout<<"Function of Child Class";
}
};
int main() {
DerivedClass obj = DerivedClass();
obj.disp();
return 0;
}
What is the output of the program?
#include<iostream>
using namespace std;
class A
{ public:
class B
{ private:
int num;
public:
void getdata(int n)
Nested classes in C++ The
13 { num = n; } The number is 9 Nested classes in C++ Error 5
number is 9
void putdata()
{ cout<<"The number is "<<num; }
};
};
int main()
{ cout<<"Nested classes in C++";
A :: B obj;
obj.getdata(9);
obj.putdata();
return 0; }
What happens when objects s1 and s2 are added?
Error because s1+s2 will Segmentation fault as two
string s1 = "Hello"; The statements runs
14 result into string and no string cannot be added in Run-time error 2
string s2 = "World"; perfectly
string has substr() function C++
string s3 = (s1+s2).substr(5);

Which of the following operators are overloaded by default by the


compiler in every user defined classes even if user has not written?
15 Both 1 and 2 Only 2 Only 1 None of the two 3
1) Comparison Operator ( == )
2) Assignment Operator ( = )
In case of operator overloading, operator function must be ______ .

16 1. Static member functions Only 2 Only 1, 3 Only 2 , 3 All 1 , 2, 3 4


2. Non- static member functions
3. Friend Functions
explicit Distance(float meters) : MTF(3.280833F)
{
float fltfeet = MTF * meters; Data conversion and Type conversion by
17 Not a valid Keyword None of the above 2
feet = int(fltfeet); invokes constructor compiler
inches = 12*(fltfeet-feet);
} Impact of explicit keyword in the declarator restricts
Analyse the code and predict the output:#include<iostream>
using namespace std;
class Base1 {
public:
Base1()
{ cout << " Base1's constructor called" << endl; }
};
class Base2 {
public:
Base2()
Base1′s constructor called Base2′s constructor called
{ cout << "Base2's constructor called" << endl; }
Base2′s constructor called Base1′s constructor called
18 }; Compiler dependent Compiler error 3
Derived’s constructor Derived’s constructor
class Derived: public Base1, public Base2 {
called called
public:
Derived()
{ cout << "Derived's constructor called" << endl; }
};

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>

using namespace std;


class P {
public:
void print() { cout <<" Inside P"; }
};

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

Q.No. Questions Option1 Option2 Option3 Option4 Marks


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 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
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 In base class In derived claEssrror due to Base class object creEartriorndue to virtual function definition Error due to b.show() 2
};
void main()
{
Base b;
b.show();
Derv dv;
Base *a;
a=&dv;
a->show();
}

4 If base class destructor is declared as virtual tBhaesneocrldaesrs odfesdtersutcrtuocrtothr eenxeDceurtivoeD


nde(fcrilrvasestsdtoD
cleasststr)udcetsotructor then Base class DeO strnulyctoDrerived class destructor Only Base class destructor 1
Predict the output
Class C
{private: int data;
Public : friend int frfn(C,Cpp);
};
Class Cpp
{private: int data;
public: friend int frfn(C,Cpp);
5 Error since existence of Cpp is not known toPC rin. ts someErrarnodr odm
uedtaotafriend function definition is gEirvreonr boeuctsaiudseethCeacnldasC
sepsp are not Friend classes3.
};
Int frfn(C,Cpp)
{return C.data*Cpp.data;}
Int main()
{ C c;Cpp p;
cout<<frfn(c,p);
}

6 Wehnetsn data member is non static, only one data valueWweicllabneptahsesrevNafolu.roeafalplsatahrraegmculem


Identify the correct statem atsesrnsot abinrjeccootnsp.eym
coonrestrinucFtorirend versioUnsionfga*ftuhcistio(pnotihnatenra),swaem
caenmabveorifducnrcetaiotinon of extra o3bjects.
Consider the code snippet
int buff[MAX];
int main(){
……..
reinterpret_cast<char*>(buff) reinterpret_cast<char*>(buff)
7 ofstream os(“edata.dat”,ios::binary) (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. 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

What will be the output of the following code?


#include <iostream>
using namespace std;
int main()
{
10 5 10 15 it will return some random number 4
int a = 5, b = 10, c = 15;
int *arr[ ] = {&a, &b, &c};
cout << arr[1];
return 0;
}
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class where
{
private:
char charray[10];
public: My object’s address is My object’s address is My object’s address is
void reveal() 0x8f4effec 1000 chararray1000
{ cout << “\nMy object’s address is “ << this; } My object’s address is My object’s address is My object’s address is
11 }; 0x8f4effe2 1000 chararray2000 none of the mentioned 3
My object’s address is My object’s address is My object’s address is
int main() 0x8f4effd8 1000 chararray3000
{
where w1, w2, w3;
w1.reveal();
w2.reveal();
w3.reveal();
cout << endl;
return 0;
}
Predict the output for following code. #include
<iostream>
using namespace std;
class alpha
{
private:
int data;
public:
alpha()
{}
alpha(int d)
{ data = d; }
alpha(alpha& a) Assignment operator invoked Invalid operator
{ 37 37
12 data = a.data; Copy constructor invoked Copy constructor invoked Segmentation fault invalid 3
cout <<"\nCopy constructor invoked"; 37 37
}
void display()
{ cout << "\n"<<data; }
void operator = (alpha& a)
{
data = a.data;
cout << "\nAssignment operator invoked";
}
};
int main()
{
alpha a1(37);
myfile:open ("example.bin",
13 Select the correct syntax myfile.open ("example.bin", iosm
::oyufti)le; ::open ("example.bin", ios:m
:oyufti)l;e.open ("example.bin", ios:out); 1
ios::out);
What is the output of the following program
#include <iostream>
using namespace std;
int main ()
{
char str[] = "Sastra";
int val = 100;
14 Sastra 100 A Error 3
char ch = 'A';
cout.width (5);
cout.setf(ios::right);
cout << val << endl;
return 0;
}

4. Find the output of the program :


#include<iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream outfile ("text.txt");
for (int n = 0; n < 100; n++)
15 { Success Error 100 SuccessSuccess 3
outfile << n;
outfile.flush();
}
cout << "Success";
outfile.close();
return 0;
}

What is the output of the program? (4 marks)


#include<iostream>
#include<fstream>
using namespace std;

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;
}

5. While executing the following program,


#include<fstream>
#include<iostream>
using namespace std;
main()
{
int rollno;
char name[20];
int marks;
ofstream out_file("stud.txt");
if(!out_file)
{
cerr<<"file cannot open correctly"; details copied to a file
} file named stud is opened in
17 details entered by the user associated with out_file all of the above 5
cout<<"enter student details\n"; write mode
object i.e. in stud
cout<<"enter roll no";
cin>>rollno;
cout<<"enter name:";
cin>>name;
cout<<"enter marks:";
cin>>marks;
cout<<"writing student details into file";
out_file<<rollno<<endl;
out_file<<name<<endl;
out_file<<marks<<endl;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy