MCQ (Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

 

Which of the following is the default return value of functions in C+


+?
a) int
b) char
c) float
d) void
Answer: a

 What happens to a function defined inside a class without any


complex operations (like looping, a large number of lines, etc)?
a) It becomes a virtual function of the class
b) It becomes a default calling function of the class
c) It becomes an inline function of the class
d) The program gives an error
Answer: c

 What is an inline function?


a) A function that is expanded at each call during execution
b) A function that is called during compile time
c) A function that is not checked for syntax errors
d) A function that is not checked for semantic analysis
Answer: a

 An inline function is expanded during ______________


a) compile-time
b) run-time
c) never expanded
d) end of the program
Answer: a

In which of the following cases inline functions may not word?


i) If the function has static variables.
ii) If the function has global and register variables.
iii) If the function contains loops
iv) If the function is recursive
a) i, iv
b) iii, iv
c) ii, iii, iv
d) i, iii, iv
Answer: d

When we define the default values for a function?


a) When a function is defined
b) When a function is declared
c) When the scope of the function is over
d) When a function is called
Answer: b

 Where should default parameters appear in a function prototype?


a) To the rightmost side of the parameter list
b) To the leftmost side of the parameter list
c) Anywhere inside the parameter list
d) Middle of the parameter list
Answer: a

 If an argument from the parameter list of a function is defined


constant then _______________
a) It can be modified inside the function
b) It cannot be modified inside the function
c) Error occurs
d) Segmentation fault
Answer: b

Which of the following feature is used in function overloading and


function with default argument?
a) Encapsulation
b) Polymorphism
c) Abstraction
d) Modularity
Answer: b
What will be the output of the following C++ code?

#include<iostream>using namespace std;


 int fun(int x = 0, int y = 0, int z){ return (x + y + z); }
 int main(){
cout << fun(10);
return 0;}

a) 10
b) 0
c) Error
d) Segmentation fault
Answer: c

What will be the output of the following C++ code?

#include<iostream>using namespace std;


 class Test{
protected:
int x;
public:
Test (int i):x(i) { }
void fun() const { cout << "fun() const " << endl; }
void fun() { cout << "fun() " << endl; }};
 int main(){
Test t1 (10);
const Test t2 (20);
t1.fun();
t2.fun();
return 0;}
Answer
fun()

fun() const

What will be the output of the following C++ code?

#include <iostream>using namespace std;


 int fun(int=0, int = 0);
 int main(){
cout << fun(5);
return 0;}int fun(int x, int y) { return (x+y); }
a) -5
b) 0
c) 10
d) 5

Answer: d

What will be the output of the following C++ code?

#include <iostream>using namespace std;void square (int *x, int *y)


{
*x = (*x) * --(*y);}int main ( ){
int number = 30;
square(&number, &number);
cout << number;
return 0;}
a) 870
b) 30
c) Error
d) Segmentation fault

Answer: a

From which function the execution of a C++ program starts?


a) start() function
b) main() function
c) new() function
d) end() function

Answer: b

Which of the following is important in a function?


a) Return type
b) Function name
c) Both return type and function name
d) The return type, function name and parameter list

Answer: c

 To which does the function pointer point to?


a) variable
b) constants
c) function
d) absolute variables

Answer: c
What will we not do with function pointers?
a) allocation of memory
b) deallocation of memory
c) both allocation & deallocation of memory
d) finds memory status

Answer: c

What is the default calling convention for a compiler in c++?


a) __cdecl
b) __stdcall
c) __pascal
d) __fastcall

Answer: a

#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);

int main()

{
int a;

int (*plus)(int, int) = add;

a = operation(15, 10, plus);

cout << a;

return 0;

a) 25
b) 35
c) 40
d) 45
Answer: c

#include <iostream>

using namespace std;


void func(int x)
{
cout << x ;
}
int main()
{
void (*n)(int);

n = &func;
(*n)( 2 );

n( 2 );
return 0; }

a) 2
b) 20
c) 21
d) 22

Answer: d

 What is the mandatory part to present in function pointers?


a) &
b) return values
c) data types
d) $

Answer: c

 which of the following can be passed in function pointers?


a) variables
b) data types
c) functions
d) objects
Answer: c

 What is the meaning of the following declaration?

int(*ptr[5])();
a) ptr is pointer to function
b) ptr is array of pointer to function
c) ptr is pointer to such function which return type is array
d) ptr is pointer to array of function

Answer: b

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[]){
const char *a = "Hello\0World";
cout<<a;
return 0;}
a) Hello World
b) Hello
c) World
d) Error

Answer: b

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) a
b) empty string
c) Error
d) Segmentation fault

Answer: a

 Which is the correct way of concatenating a character at the end of a


string object?

way 1:
string s;
s = s + 'a';
 
way 2:
string s;
s.push_back('a');
a) 1 only
b) 2 only
c) both of them
d) both are wrong

Answer: c

What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;int main


(){
std::string str ("Sanfoundry.");
str.back() = '!';
std::cout << str << endl;
return 0;}
a) Sanfoundry.!
b) Sanfoundry.
c) Sanfoundry!
d) Sanfoundry!.

Answer: c

What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;int main


(){
string str ("sanfoundry.");
str.front() = 'S';
cout << str << endl;
return 0;}
a) Sanfoundry
b) Sanfoundry.
c) sanfoundry
d) sanfoundry.

Answer: b

What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;int main


(){
string str ("sanfoundry.");
cout << str.substr(3).substr(4) << endl;
return 0;}
a) foundry.
b) dry.
c) oundry.
d) found

Answer: b

What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;int main


(){
string str = "Sanfoundry!";
cout<<str.capacity();
cout<<str.size();
return 0;}

a) 1511
b) 1111
c) 1115
d) 010

Answer: a

Which looping process is best used when the number of


iterations is known?
a. for
b. while
c. do-while
d. all looping processes r

Answer:for

What is the role of a constructor in classes?


a) To modify the data whenever required
b) To destroy an object
c) To initialize the data members of an object when it is created
d) To call private functions from the outer world

Answer: c

 Why constructors are efficient instead of a function init() defined by


the user to initialize the data members of an object?
a) Because user may forget to call init() using that object leading
segmentation fault
b) Because user may call init() more than once which leads to
overwriting values
c) Because user may forget to define init() function
d) All of the mentioned

Answer: d

What is a copy constructor?


a) A constructor that allows a user to move data from one object to
another
b) A constructor to initialize an object with the values of another
object
c) A constructor to check the whether to objects are equal or not
d) A constructor to kill other copies of a given object.

Answer: b

 What will be the output of the following C++ code?

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


int a;public:
A(int i){
a = i;
}
void assign(int i){
a = i;
}
int return_value(){
return a;
}};int main(int argc, char const *argv[]){
A obj;
obj.assign(5);
cout<<obj.return_value();}
a) 5
b) 55
c) Error
d) Segmentation Fault

Answer: c

What will be the output of the following C++ code?

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


int a;
A(){
a = 5;
}
 public:
void assign(int i){
a = i;
}
int return_value(){
return a;
}};int main(int argc, char const *argv[]){
A obj;
obj.assign(10);
cout<<obj.return_value();}
a) 5
b) 10
c) Error
d) Segmentation fault

Answer: c

In the following C++ code how many times the string “A’s
constructor called” will be printed?

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


int a;public:
A(){
cout<<"A's constructor called";
}};class B{
static A a;public:
B(){
cout<<"B's constructor called";
}
static A get(){
return a;
}};
A B::a;int main(int argc, char const *argv[]){
B b;
A a1 = b.get();
A a2 = b.get();
A a3 = b.get();}
a) 3
b) 4
c) 2
d) 1

Answer: d

What happens if a user forgets to define a constructor inside a class?


a) Error occurs
b) Segmentation fault
c) Objects are not created properly
d) Compiler provides a default constructor to avoid faults/errors

Answer: d

How many parameters does a default constructor require?


a) 1
b) 2
c) 0
d) 3

Answer: c

 How constructors are different from other member functions of the


class?
a) Constructor has the same name as the class itself
b) Constructors do not return anything
c) Constructors are automatically called when an object is created
d) All of the mentioned

Answer: d

. How many types of constructors are there in C++?


a) 1
b) 2
c) 3
d) 4

Answer: c

What will be the output of the following C++ code?

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


mutable int a;public:
A(){
cout<<"Default constructor called\n";
}
A(const A& a){
cout<<"Copy Constructor called\n";
}};int main(int argc, char const *argv[]){
A obj;
A a1 = obj;
A a2(obj);}

Default constructor called

Copy Constructor called

Copy Constructor called

What will be the output of the following C++ code?

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


mutable int a;public:
A(){
cout<<"A's default constructor called\n";
}
A(const A& a){
cout<<"A's copy Constructor called\n";
}};class B{
A obj;public:
B(){
cout<<"B's Constructor called\n";
}};int main(int argc, char const *argv[]){
B b1;
B b2;}
A's default constructor called

B's Constructor called

A's default constructor called

B's Constructor called

. What is the role of destructors in Classes?


a) To modify the data whenever required
b) To destroy an object when the lifetime of an object ends
c) To initialize the data members of an object when it is created
d) To call private functions from the outer world

Answer: b

 What is syntax of defining a destructor of class A?


a) A(){}
b) ~A(){}
c) A::A(){}
d) ~A(){};

Answer: b

When destructors are called?


a) When a program ends
b) When a function ends
c) When a delete operator is used
d) All of the mentioned

Answer: d

. What does a class in C++ holds?


a) data
b) functions
c) both data & functions
d) arrays

Answer: c
How many specifiers are present in access specifiers in class?
a) 1
b) 2
c) 3
d) 4

Answer: c

 Which is used to define the member of a class externally?


a) :
b) ::
c) #
d) !!$

Answer: b

Which other keywords are also used to declare the class other than
class?
a) struct
b) union
c) object
d) both struct & union

Answer: d

What will be the output of the following C++ code?


1.
#include <iostream>

using namespace std;

class rect

int x, y;

public:

void val (int, int);

int area ()
return (x * y);
}

};

void rect::val (int a, int b)


{

x = a;

y = b;
}

int main ()

rect rect;

rect.val (3, 4);

cout << "rect area: " << rect.area();


return 0;
}

a) rect area: 24
b) rect area: 12
c) compile error because rect is as used as class name and variable
name in line #20
d) rect area: 56

Answer: b

 What will be the output of the following C++ code?

#include <iostream>

using namespace std;


class CDummy

public:

int isitme (CDummy& param);

};

int CDummy::isitme (CDummy& param)

if (&param == this)

return true;
else

return false;
}

int main ()

{
CDummy a;
CDummy *b = &a;

if (b->isitme(a))

cout << "execute";

else

cout<<"not execute";

}
return 0;

a) execute
b) not execute
c) error
d) both execute & not execute

Answer: a
Which of the following is a valid class declaration?
a) class A { int x; };
b) class B { }
c) public class A { }
d) object A { int x; };

Answer: a

 The data members and functions of a class in C++ are by default


____________
a) protected
b) private
c) public
d) public & protected

Answer: b

 Constructors are used to ____________


a) initialize the objects
b) construct the data members
c) both initialize the objects & construct the data members
d) delete the objects

Answer: a

 When struct is used instead of the keyword class means, what will
happen in the program?
a) access is public by default
b) access is private by default
c) access is protected by default
d) access is denied

Answer: a
 What is a friend function in C++?
a) A function which can access all the private, protected and public
members of a class
b) A function which is not allowed to access any member of any
class
c) A function which is allowed to access public and protected
members of a class
d) A function which is allowed to access only public members of a
class

Answer: a

 What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;class


Box{
int capacity;
public:
Box(int cap){
capacity = cap;
}
 
friend void show();};
 void show(){
Box b(10);
cout<<"Value of capacity is: "<<b.capacity<<endl;}
 int main(int argc, char const *argv[]){
show();
return 0;}
a) Value of capacity is: 10
b) Value of capacity is: 100
c) Error
d) Segmentation fault

Answer: a

What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;class


Box{
int capacity;
public:
Box(int cap){
capacity = cap;
}
friend void show();};
 void Box::show(){
Box b(10);
cout<<"Value of capacity is: "<<b.capacity<<endl;}
 int main(int argc, char const *argv[]){
show();
return 0;}
a) Value of capacity is: 10
b) Value of capacity is: 100
c) Error
d) Segmentation fault

Answer: c
How many member functions are there in this C++ class excluding
constructors and destructors?

class Box{
int capacity;
public:
void print();
friend void show();
bool compare();
friend bool lost();};

Answer: b

What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;class B{


int b;
public:
B(int i){
b = i;
}};
 class C{
B b;
public:
C(int i){
b = B(i);
}
friend void show();};
 void show(){
C c(10);
cout<<"value of b is: "<<c.b.b<<endl;}
 int main(int argc, char const *argv[]){
show();
return 0;}
a) value of b is: 10
b) value of b is: 12345435
c) error
d) segmentation fault

Answer: c

 What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;class B{


int b;
public:
B(){}
B(int i){
b = i;
}
int show(){
return b;
}};
 class C{
B b;
public:
C(int i){
b = B(i);
}
friend void show();};
 void show(){
C c(10);
cout<<"value of b is: "<<c.b.show()<<endl;}
 int main(int argc, char const *argv[]){
show();
return 0;}
a) value of b is: 10
b) value of b is: 12345435
c) error
d) segmentation fault

Answer: a

What will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;class B{


int b;
public:
B(){}
B(int i){
b = i;
}
int show(){
return b;
}};
 class C{
B b;
public:
C(int i){
b = B(i);
}
friend void show(){
 
C c(10);
cout<<"value of b is: "<<c.b.show()<<endl;
}};
 int main(int argc, char const *argv[]){
show();
return 0;}
a) value of b is: 10
b) value of b is: 12345435
c) error
d) segmentation fault

Answer: c

hat will be the output of the following C++ code?

#include <iostream>#include <string>using namespace std;class B{


int b;
public:
B(){}
B(int i){
b = i;
}
int show(){
return b;
}};
 class C{
B b;
public:
C(int i){
b = B(i);
}
friend void show(){
 
C c(10);
cout<<"value of b is: "<<c.b.show()<<endl;
}
};
 int main(int argc, char const *argv[]){
C c(1);
c.show();
return 0;}
a) value of b is: 10
b) value of b is: 12345435
c) error
d) segmentation fault

Answer: c
 Pick the correct statement.
a) Friend functions are in the scope of a class
b) Friend functions can be called using class objects
c) Friend functions can be invoked as a normal function
d) Friend functions can access only protected members not the
private members

Answer: c

 Which of the following is correct about friend functions?


a) Friend functions use the dot operator to access members of a class
using class objects
b) Friend functions can be private or public
c) Friend cannot access the members of the class directly
d) All of the mentioned

Answer: d

 Which keyword is used to represent a friend function?


a) friend
b) Friend
c) friend_func
d) Friend_func

Answer: a

 Which of the following is not a data type?


a) Symbolic Data
b) Alphanumeric Data
c) Numeric Data
d) Alphabetic Data

Answer: a
@Ac# is a type of ________________ data.
a) Symbolic
b) Alphanumeric
c) Alphabetic
d) Numeric
Answer: b

Which of the following is not a valid representation in bits?


a) 8-bit
b) 24-bit
c) 32-bit
d) 64-bit
Answer: b

 What are the entities whose values can be changed called?


a) Constants
b) Variables
c) Modules
d) Tokens
Answer: b

Which of the following is not a basic data type in C language?


a) float
b) int
c) real
d) char
Answer: c

What does FORTRAN stands for?


a) Formula Transfer
b) Formula Transformation
c) Formula Translation
d) Format Transformation
Answer: c

The program written by the programmer in high level language is


called _____________
a) Object Program
b) Source Program
c) Assembled Program
d) Compiled Program
Answer: b
A standardized language used for commercial applications.
a) C
b) Java
c) COBOL
d) FORTRAN
Answer: c
 ______________ define how the locations can be used.
a) Data types
b) Attributes
c) Links
d) Data Objects
Answer: b

You might also like