C++ MCQ
C++ MCQ
2. What is C++?
a) C++ is an object oriented programming language
b) C++ is a procedural programming language
c) C++ supports both procedural and object oriented programming language
d) C++ is a functional programming language
View Answer
Answer: c
Explanation: C++ supports both procedural(step by step instruction) and object
oriented programming (using the concept of classes and objects).
3. Which of the following is the correct syntax of including a user defined header
files in C++?
a) #include [userdefined]
b) #include “userdefined”
c) #include <userdefined.h>
d) #include <userdefined>
View Answer
Answer: b
Explanation: C++ uses double quotes to include a user-defined header file. The
correct syntax of including user-defined is #include “userdefinedname”.
10. What happens if the following C++ statement is compiled and executed?
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
char s1[6] = "Hello";
char s2[6] = "World";
char s3[12] = s1 + " " + s2;
cout<<s3;
return 0;
}
a) Hello
b) World
c) Error
d) Hello World
View Answer
Answer: c
Explanation: There is no operation defined for the addition of character array in C++
hence the compiler throws an error as it does not understood what to do about this
expression.
#include <stdio.h>
int main(void)
{
int new = 5;
printf("%d", new);
}
#include <stdio.h>
void func(void)
{
printf("Hello");
}
void main()
{
func();
func(2);
}
1. #include <iostream>
2. #include <string>
3. #include <algorithm>
4. using namespace std;
5. int main()
6. {
7. string s = "spaces in text";
8. s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
9. cout << s << endl;
10. }
a) spacesintext
b) spaces in text
c) spaces
d) spaces in
View Answer
Answer: a
Explanation: In this program, We formed a algorithm to remove spaces in the string.
Output:
$ g++ dan.cpp
$ a.out
spacesintext
17. Which of the following C++ code will give error on compilation?
================code 1=================
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout<<"Hello World";
return 0;
}
========================================
================code 2=================
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout<<"Hello World";
return 0;
}
========================================
a) Code 1 only
b) Neither code 1 nor code 2
c) Both code 1 and code 2
d) Code 2 only
View Answer
Answer: b
Explanation: Neither code 1 nor code 2 will give an error as both are syntactically
correct as in first code we have included namespace std and in second one we have
used scope resolution operator to resolve the conflict.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int p;
6. bool a = true;
7. bool b = false;
8. int x = 10;
9. int y = 5;
10. p = ((x | y) + (a + b));
11. cout << p;
12. return 0;
13. }
a) 12
b) 0
c) 2
d) 16
View Answer
Answer: d
Explanation: | means bitwise OR operation so x | y (0101 | 1010) will be evaluated
to 1111 which is integer 15 and as a is true and b is false so a+b(1 + 0) = 1. So final
value of expression in line #10 will be 15 + 1 = 16.
20. By default, all the files in C++ are opened in _________ mode.
a) Binary
b) VTC
c) Text
d) ISCII
View Answer
Answer: c
Explanation: By default, all the files in C++ are opened in text mode. They read the
file as normal text.
1. int main()
2. {
3. register int i = 1;
4. int *ptr = &i;
5. cout << *ptr;
6. return 0;
7. }
a) Runtime error may be possible
b) Compiler error may be possible
c) 1
d) 0
View Answer
Answer: b
Explanation: Using & on a register variable may be invalid, since the compiler may
store the variable in a register, and finding the address of it is illegal.
#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin: " << cin;
return 0;
}
a) Segmentation fault
b) Nothing is printed
c) Error
d) cin: garbage value
View Answer
Answer: d
Explanation: cin is a variable hence overrides the cin object. cin >> cin has no
meaning so no error.
#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
b) World
c) Error
d) Hello World
View Answer
Answer: a
Explanation: char* are terminated by a ‘\0’ character so the string “Hello\0World” will
be cut down to “Hello”.
28. Which of the following is used to terminate the function declaration in C++?
a) ;
b) ]
c) )
d) :
View Answer
Answer: a
Explanation: ; semicolon is used to terminate a function declaration statement in
C++.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char c = 74;
6. cout << c;
7. return 0;
8. }
a) I
b) J
c) A
d) N
View Answer
Answer: b
Explanation: The literal value for 74 is J. So it will be printing J.
1. #include <iomanip>
2. #include <iostream>
3. using namespace std;
4. int main()
5. {
6. cout << setprecision(17);
7. double d = 0.1;
8. cout << d << endl;
9. return 0;
10. }
32. What is the correct syntax of accessing a static member of a class in C++?
---------------------------
Example class:
class A
{
public:
static int value;
}
---------------------------
a) A->value
b) A^value
c) A.value
d) A::value
View Answer
Answer: d
Explanation: Scope resolution operator(::) is used to access a static member of a
class.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5;
6. float b;
7. cout << sizeof(++a + b);
8. cout << a;
9. return 0;
10. }
a) 2 5
b) 4 5
c) 4 6
d) 2 6
View Answer
Answer: b
Explanation: The a as a integer will be converted to float while calculating the size.
The value of any variable doesn’t modify inside sizeof operator. Hence value of
variable a will remain 5.
Output:
$ g++ size3.cpp
$ a.out
4 5
36. Which of the following symbol is used to declare the preprocessor directives in
C++?
a) $
b) ^
c) #
d) *
View Answer
Answer: c
Explanation: # symbol is used to declare the preprocessor directives.
#include<iostream>
using namespace std;
int main()
{
int a = 5;
auto check = [=]()
{
a = 10;
};
check();
cout<<"Value of a: "<<a<<endl;
return 0;
}
a) Segmentation fault
b) Value of a: 5
c) Value of a: 10
d) Error
View Answer
Answer: d
Explanation: As this lambda expression is capturing the extrenal variable by value
therefore the value of a cannot be changes inside the lambda expression hence the
program gives error.
#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) 30
b) Error
c) Segmentation fault
d) 870
View Answer
Answer: d
Explanation: As we are passing value by reference therefore the change in the value
is reflected back to the passed variable number hence value of number is changed
to 870.
#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.!
View Answer
Answer: a
Explanation: back() function modifies the last character of the string with the
character provided.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n = 5;
6. void *p = &n;
7. int *pi = static_cast<int*>(p);
8. cout << *pi << endl;
9. return 0;
10. }
a) 5
b) 6
c) compile time error
d) runtime error
View Answer
Answer: a
Explanation: We just casted this from void to int, so it prints 5
Output:
$ g++ poi1.cpp
$ a.out
5
44. Which of the following constructors are provided by the C++ compiler if not
defined in a class?
a) Copy constructor
b) Default constructor
c) Assignment constructor
d) All of the mentioned
View Answer
Answer: d
Explanation: If a programmer does not define the above constructors in a class the
C++ compiler by default provides these constructors to avoid error on basic
operations.
#include <iostream>
using namespace std;
int main()
{
try
{
try
{
throw 20;
}
catch (int n)
{
cout << "Inner Catch\n";
throw;
}
}
catch (int x)
{
cout << "Outer Catch\n";
}
return 0;
}
a) Outer Catch
b)
Inner Catch
Outer Catch
c) Error
d) Inner Catch
View Answer
Answer: b
Explanation: The exception thrown by the inner try catch block is caught by the
inner block hence “Inner Catch” is printed but as inner catch block again throws an
exception further therefore the exception is thrown further which is caught by the
outer catch block hence “Outer Catch” is also printed.
46. Which concept allows you to reuse the written code in C++?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
View Answer
Answer: a
Explanation: Inheritance allows you to reuse your already written code by inheriting
the properties of written code into other parts of the code, hence allowing you to
reuse the already written code.
47. What will be the output of the following C++ code snippet?
1. #include <iostream>
2. using namespace std;
3. int operate (int a, int b)
4. {
5. return (a * b);
6. }
7. float operate (float a, float b)
8. {
9. return (a / b);
10. }
11. int main()
12. {
13. int x = 5, y = 2;
14. float n = 5.0, m = 2.0;
15. cout << operate(x, y) <<"\t";
16. cout << operate (n, m);
17. return 0;
18. }
a) 10.0 5
b) 10 2.5
c) 10.0 5.0
d) 5.0 2.5
View Answer
Answer: b
Explanation: In this program, we are divide and multiply the values.
Output:
$ g++ over3.cpp
$ a.out
10 2.5
1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. int a, b, c;
6. a = 2;
7. b = 7;
8. c = (a > b) ? a : b;
9. cout << c;
10. return 0;
11. }
a) 12
b) 14
c) 6
d) 7
View Answer
Answer: d
Explanation: We are using the ternary operator to evaluate this expression. It will
return first option, if first condition is true otherwise it will return second
Output:
$ g++ ess1.cpp
$ a.out
7
50. What is the benefit of c++ input and output over c input and output?
a) Both Type safety & Exception
b) Sequence container
c) Exception
d) Type safety
View Answer
Answer: d
Explanation: C++ input and output are type safety that means we don’t need to
specify the type of variable we are printing.
eg:
in C we need to specify %d showing that an integer will be printed, whereas in C++
we just cout the variable.
printf(“%d”, a);
cout<<a;
51. What will be the output of the following C++ code snippet?
1. #include <stdio.h>
2. #include<iostream>
3. using namespace std;
4. int main ()
5. {
6. int array[] = {0, 2, 4, 6, 7, 5, 3};
7. int n, result = 0;
8. for (n = 0; n < 8; n++)
9. {
10. result += array[n];
11. }
12. cout << result;
13. return 0;
14. }
a) 21
b) 27
c) 26
d) 25
View Answer
Answer: b
Explanation: We are adding all the elements in the array and printing it. Total
elements in the array is 7, but our for loop will go beyond 7 and add a garbage
value.
1. #include <iostream>
2. #include <string>
3. using namespace std;
4. int main ()
5. {
6. string str ("Sanfoundry");
7. for (size_t i = 0; i < str.length();)
8. {
9. cout << str.at(i-1);
10. }
11. return 0;
12. }
a) runtime error
b) Sanfo
c) S
d) Sanfoundry
View Answer
Answer: a
Explanation: This program will terminate because the cout element is out of range.
#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<"Constructor called\n";
}
~A(){
cout<<"Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete[] a;
return 0;
}
a) Segmentation fault
b) “Constructor called” five times and then “Destructor called” five times
c) “Constructor called” five times and then “Destructor called” once
d) Error
View Answer
Answer: b
Explanation: In the above program we have first initiated five-pointer variables using
new keyword hence fives time constructor will be called after that as we using
delete[] (used for deleting multiple objects) to delete variables hence all the five
objects created will be destroyed and hence five times destructor will be called.
1. Which of the following is the correct syntax of including a user defined header
files in C++?
a) #include <userdefined.h>
b) #include <userdefined>
c) #include “userdefined”
d) #include [userdefined]
View Answer
Answer: c
Explanation: C++ uses double quotes to include a user-defined header file. The
correct syntax of including user-defined is #include “userdefinedname”.
7. Which function is used to read a single character from the console in C++?
a) cin.get(ch)
b) getline(ch)
c) read(ch)
d) scanf(ch)
View Answer
Answer: a
Explanation: C++ provides cin.get() function to read a single character from console
whereas others are used to read either a single or multiple characters.
1. Wrapping data and its related functionality into a single entity is known as
_____________
a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity
View Answer
Answer: b
Explanation: In OOPs, the property of enclosing data and its related functions into a
single entity(in C++ we call them classes) is called encapsulation.
b)
int func(int);
int func(int);
c)
int func(float);
float func(int, int, char);
d)
int func();
int new_func();
View Answer
Answer: c
Explanation: Polymorphism means overriding the same function by changing types
or number of arguments. So we have only two options which has the same function
names, but as one can observe that in one option types, name and number of
parameters all are same which will lead to an error. Hence that is wrong so the
option having same name and different types or number of parameters is correct.
8. C++ is ______________
a) procedural programming language
b) object oriented programming language
c) functional programming language
d) both procedural and object oriented programming language
View Answer
Answer: d
Explanation: C++ supports both procedural(step by step instruction) and object
oriented programming(using concept of classes and objects).
10. Which of the following feature of OOPs is not used in the following C++ code?
class A
{
int i;
public:
void print(){cout<<"hello"<<i;}
}
class B: public A
{
int j;
public:
void assign(int a){j = a;}
}
a) Abstraction
b) Encapsulation
c) Inheritance
d) Polymorphism
View Answer
Answer: d
Explanation: As i and j members are private i.e. they are hidden from outer world
therefore we have used the concept of abstraction. Next data members and there
related functions are put together into single class therefore encapsulation is used.
Also as class B is derived from A therefore Inheritance concept is used. But as no
function is overloaded in any of the classes therefore, the concept of polymorphism
is missing here.
1. Which of the following class allows to declare only one object of it?
a) Abstract class
b) Virtual class
c) Singleton class
d) Friend class
View Answer
Answer: c
Explanation: Singleton class allows the programmer to declare only one object of it,
If one tries to declare more than one object the program results into error.
8. Which of the following provides a programmer with the facility of using object of a
class inside other classes?
a) Inheritance
b) Composition
c) Abstraction
d) Encapsulation
View Answer
Answer: b
Explanation: The concept of using objects of one class into another class is known as
Composition.
13. Which concept means the addition of new components to a program as it runs?
a) Data hiding
b) Dynamic binding
c) Dynamic loading
d) Dynamic typing
View Answer
Answer: c
Explanation: Dynamic loading is the concept of adding new components to a
program as it runs.
5. Which of the following supports the concept that reusability is a desirable feature
of a language?
a) It reduces the testing time
b) It reduces maintenance cost
c) It decreases the compilation time
d) It reduced both testing and maintenance time
View Answer
Answer: d
Explanation: As we will be using the existing code therefore we don’t need to check
the code again and again so testing and maintenance time decreases but the
compiler time may increase or remains same because though we are reusing the
code but every part needs to be compiled and extra include statement needs to be
executed therefore compilation time may remain same or increases.
11. Which members are inherited but are not accessible in any case?
a) Private
b) Public
c) Protected
d) Both private and protected
View Answer
Answer: a
Explanation: Private members of a class are inherited to the child class but are not
accessible from the child class.
#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<"Constructor called\n";
}
~A(){
cout<<"Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete a;
return 0;
}
a) “Constructor called” five times and then “Destructor called” five times
b) “Constructor called” five times and then “Destructor called” once
c) Error
d) Segmentation fault
View Answer
Answer: d
Explanation: The program will result in segmentation fault as we are trying to delete
only one pointer variable and leaving other variables as it is which will result in
segmentation fault i.e. improper handling of memory.
#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<"Constructor called\n";
}
~A(){
cout<<"Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete[] a;
return 0;
}
a) “Constructor called” five times and then “Destructor called” five times
b) “Constructor called” five times and then “Destructor called” once
c) Error
d) Segmentation fault
View Answer
Answer: a
Explanation: In the above program we have first initiated five-pointer variables using
new keyword hence fives time constructor will be called after that as we using
delete[](used for deleting multiple objects) to delete variables hence all the five
objects created will be destroyed and hence five times destructor will be called.
#include<iostream>
using namespace std;
class Base {
public:
Base()
{ cout<<"Constructing Base \n"; }
~Base()
{ cout<<"Destructing Base \n"; }
};
class Derived: public Base {
public:
Derived()
{ cout<<"Constructing Derived \n"; }
~Derived()
{ cout<<"Destructing Derived \n"; }
};
int main(void)
{
Derived *d = new Derived();
Base *b = d;
delete b;
return 0;
}
a)
Constructing Base
Constructing Derived
Destructing Base
b)
Constructing Base
Constructing Derived
Destructing Derived
Destructing Base
c)
Constructing Base
Constructing Derived
Destructing Base
Destructing Derived
d)
Constructing Derived
Constructing Base
Destructing Base
Destructing Derived
View Answer
Answer: a
Explanation: As we are storing a derived class object into base class pointer
therefore when the object is destroyed the program has not called the Derived class
destructor which shows that the object is not destroyed therefore the program may
give unusual behaviour.
#include<iostream>
using namespace std;
class Base {
public:
Base()
{ cout<<"Constructing Base \n"; }
virtual~Base()
{ cout<<"Destructing Base \n"; }
};
class Derived: public Base {
public:
Derived()
{ cout<<"Constructing Derived \n"; }
~Derived()
{ cout<<"Destructing Derived \n"; }
};
int main(void)
{
Derived *d = new Derived();
Base *b = d;
delete b;
return 0;
}
a)
Constructing Base
Constructing Derived
Destructing Base
b)
Constructing Base
Constructing Derived
Destructing Derived
Destructing Base
c)
Constructing Base
Constructing Derived
Destructing Base
Destructing Derived
d)
Constructing Derived
Constructing Base
Destructing Base
Destructing Derived
View Answer
Answer: b
Explanation: In this case, we have made the destructor of base class virtual which
will ensure that any derived class object which is pointed by a base class pointer
object on deletion should call both base and derived class destructor.
#include <iostream>
using namespace std;
class A
{
int a;
A() { a = 5;}
};
int main()
{
A *obj = new A;
cout << obj->a;
}
a) 5
b) Garbage value
c) Compile-time error
d) Run-time error
View Answer
Answer: c
Explanation: As Test() constructor is private member of the class therefore cannot
be accessed from the outside world therefore the program gives error.
11. What happens if the following C++ statement is compiled and executed?
a) Undefined behaviour
b) Syntactically incorrect
c) Semantically incorrect
d) The program runs perfectly
View Answer
Answer: a
Explanation: Deleting a pointer twice in a program may lead to run-time error or
may run perfectly. It depends on the compiler how it handles the situation so the
program may compile and run successfully but actually the program should give a
run-time error(segmentation fault) as you are trying to access the unauthorized
memory of the system.
#include<stdio.h>
int main()
{
foo();
}
int foo()
{
printf("Hello");
return 0;
}
#include <stdio.h>
int main(void)
{
const int j = 20;
int *ptr = &j;
printf("*ptr: %d\n", *ptr);
return 0;
}
int *p = malloc(10);
const int a;
#include <stdio.h>
int main(void)
{
int new = 5;
printf("%d", new);
}
#include <stdio.h>
void main()
{
printf("Hello World");
}
#include <stdio.h>
void func(void)
{
printf("Hello");
}
void main()
{
func();
func(2);
}
#include <stdio.h>
void func()
{
printf("Hello");
}
void main()
{
func();
func(2);
}
1. Which of the following is not a fundamental type is not present in C but present in
C++?
a) int
b) float
c) bool
d) void
View Answer
Answer: c
Explanation: Boolean type is not present as a fundamental type in C. int type is used
as boolean in C whereas in C++ bool is defined as a fundamental type for handling
boolean outputs.
2. What is the size of a boolean variable in C++?
a) 1 bit
b) 1 byte
c) 4 bytes
d) 2 bytes
View Answer
Answer: a
Explanation: Boolean uses only 1 bit as it stores only truth values which can be
true(1) or false(0).
5. Which of the following is the correct difference between cin and scanf()?
a) both are the same
b) cin is a stream object whereas scanf() is a function
c) scanf() is a stream object whereas cin is a function
d) cin is used for printing whereas scanf() is used for reading input
View Answer
Answer: b
Explanation: cin is a stream object available in C++ whereas scanf() is a function
available in both C and C++. both are used for reading input from users.
12. What will be the output of the following program in both C and C++?
#include<stdio.h>
int main(int argc, char const *argv[])
{
printf("%d\n", (int)sizeof('a'));
return 0;
}
#include<stdio.h>
int main(int argc, char const *argv[])
{
char a = 'a';
printf("%d\n", (int)sizeof(a));
return 0;
}
#include<iostream>
using namespace std;
int x = 1;
int main()
{
int x = 2;
{
int x = 3;
cout << ::x << endl;
}
return 0;
}
a) 1
b) 2
c) 3
d) 123
View Answer
Answer: a
Explanation: While printing x we are using :: operator hence the refernce is given to
global variable hence the global variable x = 1 is printed.
advertisement
3. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class A
{
~A(){
cout<<"Destructor called\n";
}
};
int main()
{
A a;
return 0;
}
a) Destructor called
b) Nothing will be printed
c) Error
d) Segmentation fault
View Answer
Answer: c
Explanation: Whenever a destructor is private then one should not define any
normal object as it will be destroyed at the end of the program which will call
destructor and as destructor is private the program gives error during compile while
in case of pointer object the compiler at compile does not know about the object,
therefore, does not gives compile error. Hence when the destructor is private then
the programmer can declare pointer object but cannot declare a normal object.
#include<iostream>
using namespace std;
class A
{
~A(){
cout<<"Destructor called\n";
}
};
int main()
{
A *a1 = new A();
A *a2 = new A();
return 0;
}
a) Destructor called
b)
Destructor called
Destructor called
c) Error
d) Nothing is printed
View Answer
c
Explanation: In this program, the destructor of class A is private therefore the
destructor for object a cannot be called hence the program gives an error.
5. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
int x[100];
int main()
{
cout << x[99] << endl;
}
a) Garbage value
b) 0
c) 99
d) Error
View Answer
Answer: b
Explanation: In C++ all the uninitialized variables are set to 0 therefore the value of
all elements of the array is set to 0.
#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin: " << cin;
return 0;
}
#include<stdio.h>
struct STRUCT
{
int a;
int func()
{
printf("HELLO THIS IS STRUCTURE\n");
}
};
int main()
{
struct STRUCT s;
s.func();
return 0;
}
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
View Answer
Answer: b
Explanation: As C does not allows the structure to have member functions,
therefore, it gives an error in case of C but as C++ does allow structures to have
member functions, therefore, the C++ does not give an error.
14. What happens if we run the following code in both C and C++?
#include<stdio.h>
struct STRUCT
{
int a = 5;
int func()
{
printf("%d\n", a);
}
};
int main()
{
struct STRUCT s;
s.func();
return 0;
}
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
View Answer
Answer: b
Explanation: As C does not allows to initialize any member inside the structure,
therefore, the program gives error whereas in case of C++ this is allowed therefore
the program does not give any error.
15. What happens if the following program is compiled in both C and C++?
#include<stdio.h>
struct STRUCT
{
int static a;
};
int main()
{
struct STRUCT s;
return 0;
}
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
View Answer
Answer: b
Explanation: C does not allow the programmer to declare any static members inside
a class whether in C++ it is allowed to declare static variables.
#include<stdio.h>
struct STRUCT
{
private:
int a;
};
int main()
{
printf("%d\n", (int)sizeof(struct STRUCT));
return 0;
}
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
View Answer
Answer: b
Explanation: Access specifiers like private, protected and the public are used
because the OOPs concept and as C is not an Object Oriented language, therefore,
access specifiers are not defined in C and hence C gives error whereas C++ does
not.
advertisement
3. Which of the following is correct about this pointer in C++?
a) this pointer is passed as a hidden argument in all the functions of a class
b) this pointer is passed as a hidden argument in all non-static functions of a class
c) this pointer is passed as a hidden argument in all static functions of a class
d) this pointer is passed as a hidden argument in all static variables of a class
View Answer
Answer: b
Explanation: As static functions are a type of global function for a class so all the
object shares the common instance of that static function whereas all the objects
have there own instance for non-static functions and hence they are passed as a
hidden argument in all the non-static members but not in static members.
4. Which of the following operator is used with this pointer to access members of a
class?
a) .
b) !
c) ->
d) ~
View Answer
Answer: c
Explanation: this pointer is a type of pointer and as we know pointer object uses the
arrow(->) operator to access the members of the class, therefore, this pointer uses -
> operator.
#include <iostream>
a) Hello World
b) Compile-time error
c) Run-time error
d) Segmentation fault
View Answer
Answer: b
Explanation: cout is defined under the namespace std and without including std
namespace we cannot cout, therefore, the program gives an error.
14. Which of the following syntax can be used to use a member of a namespace
without including that namespace?
a) namespace::member
b) namespace->member
c) namespace.member
d) namespace~member
View Answer
Answer: a
Explanation: To use a member of a namespace without including the namespace is
done by this syntax namespace::member.
15. Which of the following C++ code will give error on compilation?
================code 1=================
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout<<"Hello World";
return 0;
}
========================================
================code 2=================
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout<<"Hello World";
return 0;
}
========================================
#include <iostream>
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();
}
a) 0 0
b) 5 0
c) 0 5
d) 5 5
View Answer
Answer: c
Explanation: Static function can be called without using objects therefore the first
call is fine. Next when we are creating 5 objects of the class then value of x is
updated each time and as static variables are global to class therefore each
updation of x is reflected back to each object hence value of x is 5.
advertisement
2. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Player
{
private:
int id;
static int next_id;
public:
int getID() { return id; }
Player() { id = next_id++; }
};
int Player::next_id = 1;
int main()
{
Player p1;
Player p2;
Player p3;
cout << p1.getID() << " ";
cout << p2.getID() << " ";
cout << p3.getID();
return 0;
}
a) 1 2 3
b) 2 2 2
c) 1 3 1
d) 1 1 1
View Answer
Answer: a
Explanation: In this as next_id is a static variable so and initialized with 1 therefore
the id value for 1st objects is 1 and next_id is updated to 2. In this way next_id is
assigned to id in each object creation and updated by 1 so in this way value of each
Id is updated.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
#include <iostream>
using namespace std;
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;
}
a) Garbage value
b) Compile-time Error
c) Run-time Error
d) Nothing is printed
View Answer
Answer: b
Explanation: Every static member function of a class must be initialized explicitly
before use and a data member, a of class A declared inside class B is used without
initializing ‘a’ therefore the program gives an error.
#include<iostream>
using namespace std;
class Test
{
private:
static int count;
public:
Test& fun();
};
int Test::count = 0;
Test& Test::fun()
{
Test::count++;
cout << Test::count << " ";
return *this;
}
int main()
{
Test t;
t.fun().fun().fun().fun();
return 0;
}
a) 4 4 4 4
b) 1 2 3 4
c) 1 1 1 1
d) 0 1 2 3
View Answer
Answer: b
Explanation: Here we are returning the reference of object by the function call fun()
therefore this type of call is allowed. Also as count is static member of the class
therefore updation is reflected to the whole class and to every object of the class.
Therefore the four function calls to fun() function updates the value of count and
prints.
#include <iostream>
class Test
{
public:
void fun();
};
static void Test::fun()
{
std::cout<<"fun() is static";
}
int main()
{
Test::fun();
return 0;
}
a) fun() is static
b) Compile-time Error
c) Run-time Error
d) Nothing is printed
View Answer
Answer: b
Explanation: The prototype of the functions are not matched. The function declared
inside a class does not have static linkage however the class defined outside the
class has the static linkage, therefore, the program gives an error.
#include <iostream>
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;
}
a) 0 0
b) Garbage values
c) Compile error
d) Segmentation fault
View Answer
Answer: c
Explanation: C++ does not allows a constant object to access any non constant
member functions and as getY() is a non constant function and t is a constant object
therefore the program gives the error.
#include <stdio.h>
int main()
{
const int x;
x = 10;
printf("%d", x);
return 0;
}
a) 10
b) Garbage value
c) Error
d) Segmentation fault
View Answer
Answer: c
Explanation: In C++, all the constant variables must be initialized while declaration
and they cannot be modified later in the program. Now in this program as we have
declared the constant variable x in first line and initializing it in the next line
therefore the program gives the error.
a) 9
b) Garbage value
c) Error
d) Segmentation fault
View Answer
Answer: a
Explanation: The program is syntactically and semantically correct hence the
program is compiled and executed successfully.
int f(float)
9. When a language has the capability to produce new data type mean, it can be
called as
a) overloaded
b) extensible
c) encapsulated
d) reprehensible
View Answer
Answer: b
Explanation: Extensible is used to add new features to C++.
advertisement
bool is_int(789.54)
a) True
b) False
c) 1
d) 2
View Answer
Answer: b
Explanation: The given number is a double not an integer, so the function returns 0
which is boolean false.
7. Which of the two operators ++ and — work for the bool data type in C++?
a) None
b) ++
c) —
d) ++ & —
View Answer
Answer: b
Explanation: Due to the history of using integer values as booleans, if an integer is
used as a boolean, then incrementing will mean that whatever its truth value before
the operation, it will have a truth-value of true after it. However, it is not possible to
predict the result of — given knowledge only of the truth value of x, as it could result
in false.
1. #include <iostream>
2. using namespace std;
3. int f(int p, int q)
4. {
5. if (p > q)
6. return p;
7. else
8. return q;
9. }
10. main()
11. {
12. int a = 5, b = 10;
13. int k;
14. bool x = true;
15. bool y = f(a, b);
16. k =((a * b) + (x + y));
17. cout << k;
18. }
a) 55
b) 62
c) 52
d) 75
View Answer
Answer: c
Explanation: In this question, value of x = true and value of y will be also true as
f(a,b) will return a non-zero value. Now when adding these values with integers, the
implicit type conversion takes place hence converting both x and y to 1(integer
equivalent of bool true value). So expression (a*b) + (x+y) is evaluated to 52.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int p;
6. bool a = true;
7. bool b = false;
8. int x = 10;
9. int y = 5;
10. p = ((x | y) + (a + b));
11. cout << p;
12. return 0;
13. }
a) 0
b) 16
c) 12
d) 2
View Answer
Answer: b
Explanation: | means bitwise OR operation so x | y (0101 | 1010) will be evaluated
to 1111 which is integer 15 and as a is true and b is false so a+b(1 + 0) = 1. So final
value of expression in line #10 will be 15 + 1 = 16.
a) 0
b) 1
c) false
d) 2
View Answer
Answer: b
Explanation: The given expression is equivalent to
[( false AND True) OR false OR true] This is OR or three values so if any of them will
be true then the whole exp will be true and as we have last value as true so the
answer of expression will be TRUE.
1. How many characters are specified in the ASCII scheme?
a) 64
b) 128
c) 256
d) 24
View Answer
Answer: b
Explanation: There are 128 characters defined in the C++ ASCII list.
2. Given the variables p, q are of char type and r, s, t are of int type. Select the right
statement?
1. t = (r * s) / (r + s);
2. t = (p * q) / (r + s);
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char c = 74;
6. cout << c;
7. return 0;
8. }
a) A
b) N
c) J
d) I
View Answer
Answer: c
Explanation: The literal value for 74 is J. So it will be printing J.
1. #include <stdio.h>
2. int main()
3. {
4. char a = '\012';
5.
6. printf("%d", a);
7. return 0;
8. }
a) Compiler error
b) 12
c) 10
d) Empty
View Answer
Answer: c
Explanation: The value ‘\012’ means the character with value 12 in octal, which is
decimal 10.
10. What constant defined in <climits> header returns the number of bits in a char?
a) CHAR_SIZE
b) SIZE_CHAR
c) BIT_CHAR
d) CHAR_BIT
View Answer
Answer: d
Explanation: CHAR_BIT is a macro constant defined in <climits> header file which
expresses the number of bits in a character object in bytes.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int x = -1;
6. unsigned int y = 2;
7.
8. if(x > y)
9. {
10. cout << "x is greater";
11. }
12. else
13. {
14. cout << "y is greater";
15. }
16. }
a) x is greater
b) y is greater
c) implementation defined
d) arbitrary
View Answer
Answer: a
Explanation: x is promoted to unsigned int on comparison. On conversion x has all
bits set, making it the bigger one.
advertisement
3. Which of these expressions will return true if the input integer v is a power of
two?
a) (v | (v + 1)) == 0;
b) (~v & (v – 1)) == 0;
c) (v | (v – 1)) == 0;
d) (v & (v – 1)) == 0;
View Answer
Answer: d
Explanation: Power of two integers have a single set bit followed by unset bits.
4. What is the value of the following 8-bit integer after all statements are executed?
1. int x = 1;
2. x = x << 7;
3. x = x >> 7;
a) 1
b) -1
c) 127
d) Implementation defined
View Answer
Answer: d
Explanation: Right shift of signed integers is undefined, and has implementation-
defined behaviour.
5. Which of these expressions will make the rightmost set bit zero in an input
integer x?
a) x = x | (x-1)
b) x = x & (x-1)
c) x = x | (x+1)
d) x = x & (x+2)
View Answer
Answer: b
Explanation: If x is odd the last bit will be 1 and last bit of x-1 will become 0. If x is
even then last bit of x will be 0 and last bit of x-1 will become 1. In both case AND
operation of 1 and 0 will be 0. Hence last bit of final x will be 0.
7. 0946, 786427373824, ‘x’ and 0X2f are _____ _____ ____ and _____ literals
respectively.
a) decimal, character, octal, hexadecimal
b) octal, hexadecimal, character, decimal
c) hexadecimal, octal, decimal, character
d) octal, decimal, character, hexadecimal
View Answer
Answer: d
Explanation: Literal integer constants that begin with 0x or 0X are interpreted as
hexadecimal and the ones that begin with 0 as octal. The character literal are
written within ”.
8. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 8;
6. cout << "ANDing integer 'a' with 'true' :" << a && true;
7. return 0;
8. }
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i = 3;
6. int l = i / -2;
7. int k = i % -2;
8. cout << l << k;
9. return 0;
10. }
a) 0
b) 1
c) Compiler error may be possible
d) Runtime error may be possible
View Answer
Answer: c
Explanation: Using & on a register variable may be invalid, since the compiler may
store the variable in a register, and finding the address of it is illegal.
1. Which of the following is not one of the sizes of the floating point types?
a) short float
b) float
c) long double
d) double
View Answer
Answer: a
Explanation: Floating point types occur in only three sizes-float, long double and
double.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. float num1 = 1.1;
6. double num2 = 1.1;
7. if (num1 == num2)
8. cout << "stanford";
9. else
10. cout << "harvard";
11. return 0;
12. }
a) harvard
b) stanford
c) compile time error
d) runtime error
View Answer
Answer: a
Explanation: Float store floating point numbers with 8 place accuracy and requires 4
bytes of Memory. Double has 16 place accuracy having the size of 8 bytes.
Output:
$ g++ float3.cpp
$ a.out
harvard
1. #include <iomanip>
2. #include <iostream>
3. using namespace std;
4. int main()
5. {
6. cout << setprecision(17);
7. double d = 0.1;
8. cout << d << endl;
9. return 0;
10. }
a) 0.11
b) 0.10000000000000001
c) 0.100001
d) compile time error
View Answer
Answer: b
Explanation: The double had to truncate the approximation due to its limited
memory, which resulted in a number that is not exactly 0.1.
Output:
$ g++ float2.out
$ a.out
0.10000000000000001
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. float i = 123.0f;
6. cout << i << endl;
7. return 0;
8. }
a) 123.00
b) 1.23
c) 123
d) compile time error
View Answer
Answer: c
Explanation: The value 123 is printed because of its precision.
$ g++ float.cpp
$ a.out
123
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. float f1 = 0.5;
6. double f2 = 0.5;
7. if (f1 == 0.5f)
8. cout << "equal";
9. else
10. cout << "not equal";
11. return 0;
12. }
a) equal
b) not equal
c) compile time error
d) runtime error
View Answer
Answer: a
Explanation: 0.5f results in 0.5 to be stored in floating point representations.
Output:
$ g++ float.cpp
$ a.out
equal
10. Which is correct with respect to the size of the data types?
a) char > int < float
b) int < char > float
c) char < int < float
d) char < int < double
View Answer
Answer: d
Explanation: The char has less bytes than int and int has less bytes than double
whereas int and float can potentially have same sizes.
2. It is guaranteed that a ____ has at least 8 bits and a ____ has at least 16 bits.
a) int, float
b) char, int
c) bool, char
d) char, short
View Answer
Answer: d
Explanation: char types in C++ require atleast 8 bits and short requires atleast 16
bits, whereas for bool only 1 bit suffices and both int and float requires atleast 32
bits.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int num = 0x20 + 020 + 20;
6. cout << sizeof(num)<<'\n';
7. return 0;
8. }
a) 2
b) 4
c) Depends on compiler
d) Garbage
View Answer
Answer: c
Explanation: The sum of three numbers are belongs to different number systems,
so the result is type casted into integer.
Output:
$ g++ size.cpp
$ a.out
4
1. #include <iostream>
2. using namespace std;
3. int main ( )
4. {
5. static double i;
6. i = 20;
7. cout << sizeof(i);
8. return 0;
9. }
a) 4
b) 2
c) 8
d) garbage
View Answer
Answer: c
Explanation: The size of the double data type is 8.
$ g++ size1.cpp
$ a.out
8
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int num1 = 10;
6. float num2 = 20;
7. cout << sizeof(num1 + num2);
8. return 0;
9. }
a) 2
b) 4
c) 8
d) garbage
View Answer
Answer: b
Explanation: In this program, integer is converted into float. Therefore the result of
num1 and num2 is float. And it is returning the size of the float.
Output:
$ g++ size2.cpp
$ a.out
4
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5;
6. float b;
7. cout << sizeof(++a + b);
8. cout << a;
9. return 0;
10. }
a) 2 6
b) 4 6
c) 2 5
d) 4 5
View Answer
Answer: d
Explanation: The a as a integer will be converted to float while calculating the size.
The value of any variable doesn’t modify inside sizeof operator. Hence value of
variable a will remain 5.
Output:
$ g++ size3.cpp
$ a.out
4 5
10. What will be the output of the following C++ code (in 32-bit systems)?
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout << sizeof(char);
6. cout << sizeof(int);
7. cout << sizeof(float);
8. return 0;
9. }
a) 1 4 4
b) 1 4 8
c) 1 8 8
d) 1 8 2
View Answer
Answer: a
Explanation: Character is 1 byte, integer 4 bytes and float 4 bytes.
advertisement
ADVERTISEMENT
ADVERTISEMENT
void a;
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. void a = 10, b = 10;
6. int c;
7. c = a + b;
8. cout << c;
9. return 0;
10. }
a) 20
b) compile time error
c) runtime error
d) 40
View Answer
Answer: b
Explanation: void will not accept any values to its type.
1. #include <iostream>
2. using namespace std;
3. enum cat
4. {
5. temp = 7
6. };
7. int main()
8. {
9. int age = 14;
10. age /= temp;
11. cout << "If you were cat, you would be " << age << endl;
12. return 0;
13. }
1. #include <iostream>
2. using namespace std;
3. enum test
4. {
5. A = 32, B, C
6. };
7. int main()
8. {
9. cout << A << B<< C;
10. return 0;
11. }
a) 323334
b) 323232
c) 323130
d) 323134
View Answer
Answer: a
Explanation: If we not assigned any value to enum variable means, then the next
number to initialized number will be allocated to the variable.
Output:
$ g++ enum2.cpp
$ a.out
323334
1. #include <iostream>
2. using namespace std;
3. enum colour
4. {
5. green, red, blue, white, yellow, pink
6. };
7. int main()
8. {
9. cout << green<< red<< blue<< white<< yellow<< pink;
10. return 0;
11. }
a) 012345
b) 123456
c) compile time error
d) runtime error
View Answer
Answer: a
Explanation: The enumerator values start from zero if it is unassigned.
Output:
$ g++ enum3.cpp
$ a.out
012345
9. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. enum channel {star, sony, zee};
6. enum symbol {hash, star};
7. int i = 0;
8. for (i = star; i <= zee; i++)
9. {
10. printf("%d ", i);
11. }
12. return 0;
13. }
a) 012
b) 123
c) compile time error
d) runtime error
View Answer
Answer: c
Explanation: Enumeration variable ‘star’ appears two times in main() which causes
the error. An enumaration constant must be unique within the scope.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i;
6. enum month
7. {
8. JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
9. };
10. for (i = MAR; i <= NOV; i++)
11. cout << i;
12. return 0;
13. }
a) 01234567891011
b) 123456789101112
c) 34567891011
d) 123456789
View Answer
Answer: c
Explanation: We are getting the values from march to november and printing its
concern number.
extern int i;
int i;
1. #include <iostream>
2. using namespace std;
3. int g = 100;
4. int main()
5. {
6. int a;
7. {
8. int b;
9. b = 20;
10. a = 35;
11. g = 65;
12. cout << b << a << g;
13. }
14. a = 50;
15. cout << a << g;
16. return 0;
17. }
a) 2035655065
b) 2035655035
c) 2035635065
d) 2035645065
View Answer
Answer: a
Explanation: The local values of a and g within the block are more dominant than
the global values.
Output:
$ g++ dec1.cpp
$ a.out
2035655065
6. Can two functions declare variables(non static) with the same name?
a) No
b) Yes
c) Yes, but not a very efficient way to write programs
d) No, it gives a runtime error
View Answer
Answer: c
Explanation: We can declare variables with the same name in two functions because
their scope lies within the function.
1. #include <iostream>
2. using namespace std;
3. void addprint()
4. {
5. static int s = 1;
6. s++;
7. cout << s;
8. }
9. int main()
10. {
11. addprint();
12. addprint();
13. addprint();
14. return 0;
15. }
a) 234
b) 111
c) 123
d) 235
View Answer
Answer: a
Explanation: The variable that is declared as static has a file scope.
Output:
$ g++ dec2.cpp
$ a.out
234
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 10;
6. if (a < 10)
7. {
8. for (i = 0; i < 10; i++)
9. cout << i;
10. }
11. else
12. {
13. cout << i;
14. }
15. return 0;
16. }
a) 0123456789
b) 123456789
c) 0
d) error
View Answer
Answer: d
Explanation: We will get compilation error because ‘i’ is an undeclared identifier.
a) char*
b) char
c) CHAR
d) unknown
View Answer
Answer: a
Explanation: The statement makes CHAR a synonym for char*.
int (*fp)(char*)
a) pointer to a pointer
b) pointer to an array of chars
c) pointer to function taking a char* argument and returns an int
d) function taking a char* argument and returning a pointer to int
View Answer
Answer: c
Explanation: The (*fn) represents a pointer to a function and char* as arguments
and returning int from the function. So according to that, the above syntax
represents a pointer to a function taking a char* as an argument and returning int.
advertisement
2. The operator used for dereferencing or indirection is ____
a) *
b) &
c) ->
d) –>>
View Answer
Answer: a
Explanation: * is used as dereferencing operator, used to read value stored at the
pointed address.
string* x, y;
a) b is assigned to a
b) p now points to b
c) a is assigned to b
d) q now points to a
View Answer
Answer: b
Explanation: Assigning to reference changes the object to which the reference is
bound.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5, b = 10, c = 15;
6. int *arr[ ] = {&a, &b, &c};
7. cout << arr[1];
8. return 0;
9. }
a) 5
b) 10
c) 15
d) it will return some random number
View Answer
Answer: d
Explanation: Array element cannot be address of auto variable. It can be address of
static or extern variables.
8. The correct statement for a function that takes pointer to a float, a pointer to a
pointer to a char and returns a pointer to a pointer to a integer is ____________
a) int **fun(float**, char**)
b) int *fun(float*, char*)
c) int **fun(float*, char**)
d) int ***fun(*float, **char)
View Answer
Answer: c
Explanation: Function that takes pointer to a float, a pointer to a pointer to a char
and returns a pointer to a pointer to a integer is int **fun(float*, char**).
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char arr[20];
6. int i;
7. for(i = 0; i < 10; i++)
8. *(arr + i) = 65 + i;
9. *(arr + i) = '\0';
10. cout << arr;
11. return(0);
12. }
a) ABCDEFGHIJ
b) AAAAAAAAAA
c) JJJJJJJJ
d) AAAAAAJJJJ
View Answer
Answer: a
Explanation: Each time we are assigning 65 + i. In first iteration i = 0 and 65 is
assigned. So it will print from A to J.
$ g++ point1.cpp
$ a.out
ABCDEFGHIJ
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char *ptr;
6. char Str[] = "abcdefg";
7. ptr = Str;
8. ptr += 5;
9. cout << ptr;
10. return 0;
11. }
a) fg
b) cdef
c) defg
d) abcd
View Answer
Answer: a
Explanation: Pointer ptr points to string ‘fg’. So it prints fg.
Output:
$ g++ point.cpp
$ a.out
fg
2. What is the index number of the last element of an array with 9 elements?
a) 9
b) 8
c) 0
d) Programmer-defined
View Answer
Answer: b
Explanation: Because the first element always starts at 0. So it is on 8 position.
5. Which of the following gives the memory address of the first element in array?
a) array[0];
b) array[1];
c) array(2);
d) array;
View Answer
Answer: d
Explanation: To get the address of ith index of an array, we use following syntax (arr
+ i). So as we need address of first index we will use (arr + 0) equivalent to arr.
1. #include <stdio.h>
2. #include<iostream>
3. using namespace std;
4. int array1[] = {1200, 200, 2300, 1230, 1543};
5. int array2[] = {12, 14, 16, 18, 20};
6. int temp, result = 0;
7. int main()
8. {
9. for (temp = 0; temp < 5; temp++)
10. {
11. result += array1[temp];
12. }
13. for (temp = 0; temp < 4; temp++)
14. {
15. result += array2[temp];
16. }
17. cout << result;
18. return 0;
19. }
a) 6553
b) 6533
c) 6522
d) 12200
View Answer
Answer: b
Explanation: In this program we are adding the every element of two arrays. Finally
we got output as 6533.
Output:
$ g++ array.cpp
$ a.out
6533
1. #include <stdio.h>
2. #include<iostream>
3. using namespace std;
4. int main ()
5. {
6. int array[] = {0, 2, 4, 6, 7, 5, 3};
7. int n, result = 0;
8. for (n = 0; n < 8; n++)
9. {
10. result += array[n];
11. }
12. cout << result;
13. return 0;
14. }
a) 25
b) 26
c) 27
d) 21
View Answer
Answer: c
Explanation: We are adding all the elements in the array and printing it. Total
elements in the array is 7, but our for loop will go beyond 7 and add a garbage
value.
1. #include <stdio.h>
2. #include<iostream>
3. using namespace std;
4. int main()
5. {
6. int a = 5, b = 10, c = 15;
7. int arr[3] = {&a, &b, &c};
8. cout << *arr[*arr[1] - 8];
9. return 0;
10. }
a) 15
b) 18
c) garbage value
d) compile time error
View Answer
Answer: d
Explanation: The conversion is invalid in this array. So it will arise error. The
following compilation error will be raised:
cannot convert from ‘int *’ to ‘int’
This is because &a, &b and &c represent int* whereas the array defined is of int
type.
1. #include <stdio.h>
2. #include <iostream>
3. using namespace std;
4. int main()
5. {
6. char str[5] = "ABC";
7. cout << str[3];
8. cout << str;
9. return 0;
10. }
a) ABC
b) ABCD
c) AB
d) AC
View Answer
Answer: a
Explanation: We are just printing the values of first 3 values.
$ g++ array.cpp
$ a.out
ABC
1. #include <stdio.h>
2. #include <iostream>
3. using namespace std;
4. int main()
5. {
6. int array[] = {10, 20, 30};
7. cout << -2[array];
8. return 0;
9. }
a) -15
b) -30
c) compile time error
d) garbage value
View Answer
Answer: b
Explanation: It’s just printing the negative value of the concern element.
$ g++ array.cpp
$ a.out
-30
int(*p[5])();
a) p is pointer to function
b) p is array of pointer to function
c) p is pointer to such function which return type is the array
d) p is pointer to array of function
View Answer
Answer: b
Explanation: In the above declaration the variable p is the array, not the pointer.
2. What is size of generic pointer in C++ (in 32-bit platform)?
a) 2
b) 4
c) 8
d) 0
View Answer
Answer: b
Explanation: Size of any type of pointer is 4 bytes in 32-bit platforms.
advertisement
3. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24};
6. cout << *(a[1] + 2) << *(*(a + 1) + 2) << 2[1[a]];
7. return 0;
8. }
a) 15 18 21
b) 21 21 21
c) 24 24 24
d) Compile time error
View Answer
Answer: b
Explanation: a[1][2] means 1 * (4)+2 = 6th element of an array starting from zero.
Output:
Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ point.cpp
$ a.out
21 21 21
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i;
6. const char *arr[] = {"C", "C++", "Java", "VBA"};
7. const char *(*ptr)[4] = &arr;
8. cout << ++(*ptr)[2];
9. return 0;
10. }
a) ava
b) java
c) c++
d) compile time error
View Answer
Answer: a
Explanation: In this program we are moving the pointer from first position to second
position and printing the remaining value.
Output:
$ g++ point1.cpp
$ a.out
ava
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int arr[] = {4, 5, 6, 7};
6. int *p = (arr + 1);
7. cout << *p;
8. return 0;
9. }
a) 4
b) 5
c) 6
d) 7
View Answer
Answer: b
Explanation: In this program, we are making the pointer point to next value and
printing it.
$ g++ point3.cpp
$ a.out
5
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int arr[] = {4, 5, 6, 7};
6. int *p = (arr + 1);
7. cout << arr;
8. return 0;
9. }
a) 4
b) 5
c) address of arr
d) 7
View Answer
Answer: c
Explanation: As we counted to print only arr, it will print the address of the array.
Output:
$ g++ point2.cpp
$ a.out
0xbfb1cff
1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. int numbers[5];
6. int * p;
7. p = numbers; *p = 10;
8. p++; *p = 20;
9. p = &numbers[2]; *p = 30;
10. p = numbers + 3; *p = 40;
11. p = numbers; *(p + 4) = 50;
12. for (int n = 0; n < 5; n++)
13. cout << numbers[n] << ",";
14. return 0;
15. }
a) 10,20,30,40,50,
b) 1020304050
c) compile error
d) runtime error
View Answer
Answer: a
Explanation: In this program, we are just assigning a value to the array and printing
it and immediately dereferencing it.
Output:
$ g++ point4.cpp
$ a.out
10,20,30,40,50,
a) 12
b) 5
c) 13
d) error
View Answer
Answer: c
Explanation: In this program, we are adding the value 9 to the initial value of the
array, So it’s printing as 13.
Output:
$ g++ point5.cpp
$ a.out
13
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int const p = 5;
6. cout << ++p;
7. return 0;
8. }
a) 5
b) 6
c) Error
d) 8
View Answer
Answer: c
Explanation: We cannot modify a constant integer value.
1. #include <iostream>
2. using namespace std;
3. #define PI 3.14159
4. int main ()
5. {
6. float r = 2;
7. float circle;
8. circle = 2 * PI * r;
9. cout << circle;
10. return 0;
11. }
a) 12.5664
b) 13.5664
c) 10
d) 15
View Answer
Answer: a
Explanation: In this program, we are finding the area of the circle by using concern
formula.
Output:
$ g++ cons.cpp
$ a.out
12.5664
#include <iostream>
#include <string>
#include <cstdlib>
a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error
View Answer
Answer: d
Explanation: References require are other names for variables not for a constant
literal. No such assignment are allowed in C++.
advertisement
3. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
a) 5
b) 55
c) Error
d) Segmentation fault
View Answer
Answer: c
Explanation: Every reference should be initialized during its declaration but as p is
not initialized here therfore the program gives error.
#include <iostream>
#include <string>
#include <cstdlib>
a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error
View Answer
Answer: a
Explanation: In this program, every thing is correct so the program runs perfectly
and prints the 5 as output.
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error
View Answer
Answer: d
Explanation: A pointer cannot be directly assigned to references, because types of
pointer(int*) and reference(int) are different here. You need to think before
assigning two variable of different types otherwise the program throws error.
#include <iostream>
#include <string>
#include <cstdlib>
a) 5
b) Address of pointer a
c) Address of pointer p
d) Error
View Answer
Answer: b
Explanation: The program is correct so the the program runs perfectly. It is way to
assign pointers to references. The program prints the address of a because it is an
alias for pointer p and pointer p stores the address of a therefore answer is address
of a.
#include <iostream>
#include <string>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstdlib>
a)
#include <iostream>
#include <string>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstdlib>
a) 5
b) 10
c) Error
d) Segmentation fault
View Answer
Answer: c
Explanation: As we are passing a as const reference to function therefore its value
cannot be changes inside the function. So the program gives error.
$ g++ ref.cpp
$ a.out
In swap 105 In main 105
a) 9
b) 10
c) error
d) 11
View Answer
Answer: b
Explanation: The value is declared and it isincrementedrement, so it’s value is 10.
$ g++ ref1.cpp
$ a.out
10
1. #include <iostream>
2. using namespace std;
3. void print (char * a)
4. {
5. cout << a << endl;
6. }
7. int main ()
8. {
9. const char * a = "Hello world";
10. print(const_cast<char *> (a) );
11. return 0;
12. }
a) Hello world
b) Hello
c) world
d) compile time error
View Answer
Answer: a
Explanation: In this program we used the concept of constant casting to cast the
variable and printing it.
Output:
$ g++ ref2.cpp
$ a.out
Hello world
8. Identify the correct sentence regarding inequality between reference and pointer.
a) we can not create the array of reference
b) we can create the Array of reference
c) we can use reference to reference
d) we can use variable
View Answer
Answer: a
Explanation: It is not allowed in C++ to make an array of references. To test check
following array:
int &arr[] = {&a, &b, &c};
This will give an error.
#include <iostream>
using namespace std;
int f(int &x, int c)
{
c = c - 1;
if (c == 0) return 1;
x = x + 1;
return f(x, c) * x;
}
int main(int argc, char const *argv[])
{
int a = 4;
cout<<f(a,a);
return 0;
}
a) 343
b) 336
c) 120
d) 840
View Answer
Answer: a
Explanation: In this program as one parametere is passed by value and other is
passed by reference so after 4 calls when c == 0, then the value of x = 7 and as x is
passed by reference so all the changes will be reflected back in all the previous calls
hence the answer 1*7*7*7 = 343.
advertisement
ADVERTISEMENT
ADVERTISEMENT
#include<iostream>
using namespace std;
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
a) 30
b) 10
c) Error
d) Segmentation fault
View Answer
Answer: a
Explanation: A function returning value by reference can be used as lvalue i.e. it can
be used on the left side of an expression. Here when we are doing fun() = 30 then
we are changing the value of x(i.e. value returning) to and as x is static therefore it
will not be initialized again so the value of x becomes 30 hence the output is 30.
#include<iostream>
using namespace std;
int &fun()
{
int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
a) 30
b) 10
c) Error
d) Segmentation fault
View Answer
Answer: d
Explanation: In this case we are trying to assign 30 to a local variable which is
returned form the function func() which will be destroyed after the function call
hence next time this assignmnet is not correct hence segmentation fault.
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << x << endl ;
x = 30;
cout << ref << endl;
return 0;
}
a)
20
30
b)
10
10
c)
10
20
d)
10
30
View Answer
Answer: a
Explanation: As we know references are alias for a variable so the value of a variable
can be changed using alias hence both ref and x are same therefore changing the
value of one effects the value of other.
7. How a reference is different from a pointer?
a) A reference cannot be null
b) A reference once established cannot be changed
c) The reference doesn’t need an explicit dereferencing mechanism
d) All of the mentioned
View Answer
Answer: d
Explanation: References can never be NULL. It is not allowed to change a reference
once allocated. Referencing does not need an explicit referencing operator.
#include <iostream>
using namespace std;
int f(int &x, int c)
{
c = c - 1;
if (c == 0) return 1;
x = x + 1;
return f(x, c) * x;
}
int main(int argc, char const *argv[])
{
int a = 4;
cout<<f(a,a);
return 0;
}
a) 343
b) 336
c) 120
d) 840
View Answer
Answer: a
Explanation: In this program as one parametere is passed by value and other is
passed by reference so after 4 calls when c == 0, then the value of x = 7 and as x is
passed by reference so all the changes will be reflected back in all the previous calls
hence the answer 1*7*7*7 = 343.
advertisement
2. Which of the following is incorrect?
a) References cannot be NULL
b) A reference must be initialized when declared
c) Once a reference is declared, it cannot be modified later to reference another
object i.e. it cannot be reset
d) References cannot refer to a constant value
View Answer
Answer: d
Explanation: C++ allows references to refer to a constant value by making constant
references. For example:
const int a = 5;
const int &ref = a;
is an example of that.
#include<iostream>
using namespace std;
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
a) 30
b) 10
c) Error
d) Segmentation fault
View Answer
Answer: a
Explanation: A function returning value by reference can be used as lvalue i.e. it can
be used on the left side of an expression. Here when we are doing fun() = 30 then
we are changing the value of x(i.e. value returning) to and as x is static therefore it
will not be initialized again so the value of x becomes 30 hence the output is 30.
#include<iostream>
using namespace std;
int &fun()
{
int x = 10;
return x;
}
int main()
{
fun() = 30;
cout << fun();
return 0;
}
a) 30
b) 10
c) Error
d) Segmentation fault
View Answer
Answer: d
Explanation: In this case we are trying to assign 30 to a local variable which is
returned form the function func() which will be destroyed after the function call
hence next time this assignmnet is not correct hence segmentation fault.
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << x << endl ;
x = 30;
cout << ref << endl;
return 0;
}
a)
20
30
b)
10
10
c)
10
20
d)
10
30
View Answer
Answer: a
Explanation: As we know references are alias for a variable so the value of a variable
can be changed using alias hence both ref and x are same therefore changing the
value of one effects the value of other.
7. How a reference is different from a pointer?
a) A reference cannot be null
b) A reference once established cannot be changed
c) The reference doesn’t need an explicit dereferencing mechanism
d) All of the mentioned
View Answer
Answer: d
Explanation: References can never be NULL. It is not allowed to change a reference
once allocated. Referencing does not need an explicit referencing operator.
3. The pointer can point to any variable that is not declared with which of these?
a) const
b) volatile
c) both const & volatile
d) static
View Answer
Answer: c
Explanation: Pointer can point to any variable that is not declared with const &
volatile.
advertisement
ADVERTISEMENT
ADVERTISEMENT
1. #include <iostream>
2. using namespace std;
3. int func(void *Ptr);
4. int main()
5. {
6. char *Str = "abcdefghij";
7. func(Str);
8. return 0;
9. }
10. int func(void *Ptr)
11. {
12. cout << Ptr;
13. return 0;
14. }
a) abcdefghij
b) address of string “abcdefghij”
c) compile time error
d) runtime error
View Answer
Answer: b
Explanation: Even though it is a void pointer, we gets the address.
Output:
$ g++ b.cpp
$ a.out
0x8048714
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int *p;
6. void *vp;
7. if (vp == p);
8. cout << "equal";
9. return 0;
10. }
a) equal
b) no output
c) compile error
d) runtime error
View Answer
Answer: a
Explanation: The void pointer is easily converted to any other type of pointer, so
these are equal.
Output:
$ g++ poi4.cpp
$ a.out
equal
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i;
6. char c;
7. void *data;
8. i = 2;
9. c = 'd';
10. data = &i;
11. cout << "the data points to the integer value" << data;
12. data = &c;
13. cout << "the data now points to the character" << data;
14. return 0;
15. }
a) 2d
b) two memory addresses
c) 3d
d) 4d
View Answer
Answer: b
Explanation: Because the data points to the address value of the variables only, So it
is printing the memory address of these two variable.
Output:
$ g++ poi2.cpp
$ a.out
the data points to the integer value0xbfc81824 the data now points to the
character0xbfc8182f
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n = 5;
6. void *p = &n;
7. int *pi = static_cast<int*>(p);
8. cout << *pi << endl;
9. return 0;
10. }
a) 5
b) 6
c) compile time error
d) runtime error
View Answer
Answer: a
Explanation: We just casted this from void to int, so it prints 5
Output:
$ g++ poi1.cpp
$ a.out
5
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5, c;
6. void *p = &a;
7. double b = 3.14;
8. p = &b;
9. c = a + b;
10. cout << c << '\n' << p;
11. return 0;
12. }
a) 8, memory address
b) 8.14
c) memory address
d) 12
View Answer
Answer: a
Explanation: In this program, we are just adding the two values and printing it.
Output:
$ g++ poi.cpp
$ a.out
8
0xbfef0378
1. #include <iostream>
2. #include <string.h>
3. using namespace std;
4. int main()
5. {
6. struct student
7. {
8. int num;
9. char name[25];
10. };
11. student stu;
12. stu.num = 123;
13. strcpy(stu.name, "John");
14. cout << stu.num << endl;
15. cout << stu.name << endl;
16. return 0;
17. }
a)
123
john
b)
john
john
1. #include <iostream>
2. using namespace std;
3. struct Time
4. {
5. int hours;
6. int minutes;
7. int seconds;
8. };
9. int toSeconds(Time now);
10. int main()
11. {
12. Time t;
13. t.hours = 5;
14. t.minutes = 30;
15. t.seconds = 45;
16. cout << "Total seconds: " << toSeconds(t) << endl;
17. return 0;
18. }
19. int toSeconds(Time now)
20. {
21. return 3600 * now.hours + 60 * now.minutes + now.seconds;
22. }
a) 19845
b) 20000
c) 15000
d) 19844
View Answer
Answer: a
Explanation: In this program, we are just converting the given hours and minutes
into seconds.
Output:
$ g++ stu1.cpp
$ a.out
Total seconds:19845
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. struct ShoeType
6. {
7. string style;
8. double price;
9. };
10. ShoeType shoe1, shoe2;
11. shoe1.style = "Adidas";
12. shoe1.price = 9.99;
13. cout << shoe1.style << " $ "<< shoe1.price;
14. shoe2 = shoe1;
15. shoe2.price = shoe2.price / 9;
16. cout << shoe2.style << " $ "<< shoe2.price;
17. return 0;
18. }
1. #include <iostream>
2. using namespace std;
3. struct sec
4. {
5. int a;
6. char b;
7. };
8. int main()
9. {
10. struct sec s ={25,50};
11. struct sec *ps =(struct sec *)&s;
12. cout << ps->a << ps->b;
13. return 0;
14. }
a) 252
b) 253
c) 254
d) 262
View Answer
Answer: a
Explanation: In this program, We are dividing the values of a and b, printing it.
Output:
$ g++ stu5.cpp
$ a.out
252
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
char arr[12] = "Hello World";
for(int i=0;i<12;i++)
{
cout<<(bool)isalpha(arr[i]);
}
}
a) 111110111110
b) 111111111110
c) 111000111110
d) 111110000000
View Answer
Answer: a
Explanation: In this program we are checking whether a character is an alphabet or
not so in “Hello World” except space everything is alphabet, therefore, we have
11111011111 but it is followed by a 0 because every string is followed by a null
character which is not alphabet, therefore, we have 0 at the of the binary string.
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
char arr[12] = "H3ll0 W0r1d";
for(int i=0;i<12;i++)
{
cout<<(bool)isalpha(arr[i]);
}
cout<<endl;
for(int i=0;i<12;i++)
{
cout<<(bool)isdigit(arr[i]);
}
}
a)
000000000000
010010010100
b)
101100100010
010010010111
c)
111111101010
010010000000
d)
101100101010
010010010100
View Answer
Answer: d
Explanation: In this program, we are first checking the alphabets in the string then
digits in the string so accordingly one can find the answer.
12. What will be the output of the following C++ code?
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
char arr[12] = "H3ll0\tW0r1d";
for(int i=0;i<12;i++)
{
cout<<(bool)isprint(arr[i]);
}
}
a) 111000111110
b) 111111111110
c) 111110111110
d) 111110000000
View Answer
Answer: c
Explanation: In this program we are checking the presence of alphabets and digits in
the string so accordingly one can find the answer.
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
char arr[12] = "H3ll0\tW0r1d";
for(int i=0;i<12;i++)
{
cout<<(bool)iscntrl(arr[i]);
}
}
a) 111111111110
b) 000001000001
c) 111000111110
d) 111110000000
View Answer
Answer: b
Explanation: In this program we are checking the presence of control codes i.e. \n,
\r, \r\n, \t, etc. in the string so accordingly one can find the answer.
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
char arr[20] = "\'H3ll0\'";
for(int i=0;i<8;i++)
{
cout<<(bool)ispunct(arr[i]);
}
}
a) 10000010
b) 111111111110
c) 111000111110
d) 111110000000
View Answer
Answer: a
Explanation: In this program we are checking the presence of punctuation
characters like quotes(‘, “, etc.) in the string, so ispunct() returns 1 for single quote
positions and returns 0 otherwise.
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
char arr[27] = "abcdefghijklmnopqrstuvwxyz";
for(int i=0;i<27;i++)
{
cout<<(bool)isxdigit(arr[i]);
}
}
a) 111001100011110000000111100
b) 101010101010101001010101010
c) 111111000000000000000000000
d) 111111111000001111011110111
View Answer
Answer: c
Explanation: In this program, we are checking the presence of hexadecimal
characters in the string and as only a, b, c, d, e and f are used as hexadecimal
characters therefore only first bits are 1 and others are 0.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a;
6. a = 5 + 3 * 5;
7. cout << a;
8. return 0;
9. }
a) 35
b) 20
c) 25
d) 30
View Answer
Answer: b
Explanation: Because the * operator is having highest precedence, So it is executed
first and then the + operator will be executed.
Output:
Subscribe Now: C++ Newsletter | Important Subjects Newsletters
$ g++ op1.cpp
$ a.out
20
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5, b = 6, c, d;
6. c = a, b;
7. d = (a, b);
8. cout << c << ' ' << d;
9. return 0;
10. }
a) 5 6
b) 6 5
c) 6 7
d) 6 8
View Answer
Answer: a
Explanation: It is a separator here. In C, the value a is stored in c and in d the value b
is stored in d because of the bracket.
Output:
$ g++ op3.cpp
$ a.out
5 6
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i, j;
6. j = 10;
7. i = (j++, j + 100, 999 + j);
8. cout << i;
9. return 0;
10. }
a) 1000
b) 11
c) 1010
d) 1001
View Answer
Answer: c
Explanation: j starts with the value 10. j is then incremented to 11. Next, j is added to
100. Finally, j (still containing 11) is added to 999 which yields the result 1010.
Output:
$ g++ op2.cpp
$ a.out
1010
1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. int x, y;
6. x = 5;
7. y = ++x * ++x;
8. cout << x << y;
9. x = 5;
10. y = x++ * ++x;
11. cout << x << y;
12. return 0;
13. }
a) 749735
b) 736749
c) 367497
d) 367597
View Answer
Answer: a
Explanation: Because of the precedence the pre-increment and post increment
operator, we got the output as 749736.
Output:
$ g++ op.cpp
$ a.out
749735
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 5, b = 6, c;
6. c = (a > b) ? a : b;
7. cout << c;
8. return 0;
9. }
a) 6
b) 5
c) 4
d) 7
View Answer
Answer: a
Explanation: Here the condition is false on conditional operator, so the b value is
assigned to c.
Output:
$ g++ op1.cpp
$ a.out
6
1. #include <iostream>
2. using namespace std;
3. main()
4. {
5. double a = 21.09399;
6. float b = 10.20;
7. int c ,d;
8. c = (int) a;
9. d = (int) b;
10. cout << c <<' '<< d;
11. return 0;
12. }
a) 20 10
b) 10 21
c) 21 10
d) 10 20
View Answer
Answer: c
Explanation: In this program, we are casting the operator to integer, So it is printing
as 21 and 10.
Output:
$ g++ op5.cpp
$ a.out
21 10
1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. int n;
6. for (n = 5; n > 0; n--)
7. {
8. cout << n;
9. if (n == 3)
10. break;
11. }
12. return 0;
13. }
a) 543
b) 54
c) 5432
d) 53
View Answer
Answer: a
Explanation: In this program, We are printing the numbers in reverse order but by
using break statement we stopped printing on 3.
Output:
$ g++ stat.cpp
$ a.out
543
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 10;
6. if (a < 15)
7. {
8. time:
9. cout << a;
10. goto time;
11. }
12. break;
13. return 0;
14. }
a) 1010
b) 10
c) infinitely print 10
d) compile time error
View Answer
Answer: d
Explanation: Because the break statement need to be presented inside a loop or a
switch statement.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n = 15;
6. for ( ; ;)
7. cout << n;
8. return 0;
9. }
a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
View Answer
Answer: c
Explanation: There is not a condition in the for loop, So it will loop continuously.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i;
6. for (i = 0; i < 10; i++);
7. {
8. cout << i;
9. }
10. return 0;
11. }
a) 0123456789
b) 10
c) 012345678910
d) compile time error
View Answer
Answer: b
Explanation: for loop with a semicolon is called as body less for loop. It is used only
for incrementing the variable values. So in this program the value is incremented
and printed as 10.
Output:
$ g++ stat2.cpp
$ a.out
10
10. Which looping process is best used when the number of iterations is known?
a) for
b) while
c) do-while
d) all looping processes require that the iterations be known
View Answer
Answer: a
Explanation: Because in for loop we are allowed to provide starting and ending
conditions of loops, hence fixing the number of iterations of loops, whereas no such
things are provided by other loops.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. /* this is comment*
6. cout << "hello world";
7. return 0;
8. }
a) hello world
b) hello
c) compile time error
d) hellohello
View Answer
Answer: c
Explanation: Because the slash should need to be forward not backward.
1. #include <iostream>
2. using namespace std;
3. long factorial (long a)
4. {
5. if (a > 1)
6. return (a * factorial (a + 1));
7. else
8. return (1);
9. }
10. int main ()
11. {
12. long num = 3;
13. cout << num << "! = " << factorial ( num );
14. return 0;
15. }
a) 6
b) 24
c) segmentation fault
d) compile time error
View Answer
Answer: c
Explanation: As we have given in the function as a+1, it will exceed the size and so it
arises the segmentation fault.
Output:
$ g++ arg3.cpp
$ a.out
segmentation fault
1. #include <iostream>
2. using namespace std;
3. void square (int *x)
4. {
5. *x = (*x + 1) * (*x);
6. }
7. int main ( )
8. {
9. int num = 10;
10. square(&num);
11. cout << num;
12. return 0;
13. }
a) 100
b) compile time error
c) 144
d) 110
View Answer
Answer: d
Explanation: We have increased the x value in operand as x + 1, so it will return as
110.
Output:
$ g++ arg2.cpp
$ a.out
110
a) 11
b) 12
c) 13
d) compile time error
View Answer
Answer: c
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:
$ g++ arg1.cpp
$ a.out
13
1. #include <iostream>
2. using namespace std;
3. void Sum(int a, int b, int & c)
4. {
5. a = b + c;
6. b = a + c;
7. c = a + b;
8. }
9. int main()
10. {
11. int x = 2, y =3;
12. Sum(x, y, y);
13. cout << x << " " << y;
14. return 0;
15. }
a) 2 3
b) 6 9
c) 2 15
d) compile time error
View Answer
Answer: c
Explanation: We have passed three values and it will manipulate according to the
given condition and yield the result as 2 15.
Output:
$ g++ arg.cpp
$ a.out
2 15
1. #include <iostream>
2. using namespace std;
3. void mani()
4. void mani()
5. {
6. cout<<"hai";
7. }
8. int main()
9. {
10. mani();
11. return 0;
12. }
a) hai
b) haihai
c) compile time error
d) runtime error
View Answer
Answer: c
Explanation: We have to use the semicolon to declare the function in line 3. This is
called a function declaration and a function declaration ends with a semicolon.
1. #include <iostream>
2. using namespace std;
3. void fun(int x, int y)
4. {
5. x = 20;
6. y = 10;
7. }
8. int main()
9. {
10. int x = 10;
11. fun(x, x);
12. cout << x;
13. return 0;
14. }
a) 10
b) 20
c) compile time error
d) 30
View Answer
Answer: a
Explanation: In this program, we called by value so the value will not be changed, So
the output is 10
Output:
$ g++ fun.cpp
$ a.out
10
8. What is the scope of the variable declared in the user defined function?
a) whole program
b) only inside the {} block
c) the main function
d) header section
View Answer
Answer: b
Explanation: The variable is valid only in the function block as in other.
9. Which of the following feature is used in function overloading and function with
default argument?
a) Encapsulation
b) Polymorphism
c) Abstraction
d) Modularity
View Answer
Answer: b
Explanation: Both of the above types allows a function overloading which is the
basic concept of Polymorphism.
#include<iostream>
using namespace std;
int main()
{
cout << fun(10);
return 0;
}
a) 10
b) 0
c) Error
d) Segmentation fault
View Answer
Answer: c
Explanation: Default arguments should always be declared at the rightmost side of
the parameter list but the above function has a normal variable at the rightmost
side which is a syntax error, therefore the function gives an error.
#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;
}
a)
fun()
fun() const
b)
fun() const
fun()
c)
fun()
fun()
d)
fun() const
fun() const
View Answer
Answer: a
Explanation: As the object declared are of two types one is normal object and other
is constant object So normal objects calls normal fun() whereas constant objects
calls constant fun().
#include <iostream>
using namespace std;
int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y) { return (x+y); }
a) -5
b) 0
c) 10
d) 5
View Answer
Answer: d
Explanation: C++ allows to define such prototype of the function in which you are
not required to give variable names only the default values. While in function
definition you can provide the variable names corresponding to each parameter.
#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
View Answer
Answer: a
Explanation: As we are passing value by reference therefore the change in the value
is reflected back to the passed variable number hence value of number is changed
to 870.
1. #include <iostream>
2. using namespace std;
3. void copy (int& a, int& b, int& c)
4. {
5. a *= 2;
6. b *= 2;
7. c *= 2;
8. }
9. int main ()
10. {
11. int x = 1, y = 3, z = 7;
12. copy (x, y, z);
13. cout << "x =" << x << ", y =" << y << ", z =" << z;
14. return 0;
15. }
a) 2 5 10
b) 2 4 5
c) 2 6 14
d) 2 4 9
View Answer
Answer: c
Explanation: Because we multiplied the values by 2 in the copy function.
Output:
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
$ g++ arg6.cpp
$ a.out
x = 2,y = 6,z = 14
1. #include <iostream>
2. using namespace std;
3. void fun(int &x)
4. {
5. x = 20;
6. }
7. int main()
8. {
9. int x = 10;
10. fun(x);
11. cout << "New value of x is " << x;
12. return 0;
13. }
a) 10
b) 20
c) 15
d) 36
View Answer
Answer: b
Explanation: As the parameter is passed by reference, the value in the original
memory of x is changed hence the output is printed as 20.
Output:
$ g++ arg5.cpp
$ a.out
20
1. #include <iostream>
2. using namespace std;
3. long factorial (long a)
4. {
5. if (a > 1)
6. return (a * factorial (a + 1));
7. else
8. return (1);
9. }
10. int main ()
11. {
12. long num = 3;
13. cout << num << "! = " << factorial ( num );
14. return 0;
15. }
a) 6
b) 24
c) segmentation fault
d) compile time error
View Answer
Answer: c
Explanation: As we have given in the function as a+1, it will exceed the size and so it
arises the segmentation fault.
Output:
$ g++ arg3.cpp
$ a.out
segmentation fault
1. #include <iostream>
2. using namespace std;
3. void square (int *x)
4. {
5. *x = (*x + 1) * (*x);
6. }
7. int main ( )
8. {
9. int num = 10;
10. square(&num);
11. cout << num;
12. return 0;
13. }
a) 100
b) compile time error
c) 144
d) 110
View Answer
Answer: d
Explanation: We have increased the x value in operand as x+1, so it will return as
110.
Output:
$ g++ arg2.cpp
$ a.out
110
1. #include <iostream>
2. using namespace std;
3. int add(int a, int b);
4. int main()
5. {
6. int i = 5, j = 6;
7. cout << add(i, j) << endl;
8. return 0;
9. }
10. int add(int a, int b )
11. {
12. int sum = a + b;
13. a = 7;
14. return a + b;
15. }
a) 11
b) 12
c) 13
d) compile time error
View Answer
Answer: c
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:
$ g++ arg1.cpp
$ a.out
13
1. #include <iostream>
2. using namespace std;
3. void Sum(int a, int b, int & c)
4. {
5. a = b + c;
6. b = a + c;
7. c = a + b;
8. }
9. int main()
10. {
11. int x = 2, y =3;
12. Sum(x, y, y);
13. cout << x << " " << y;
14. return 0;
15. }
a) 2 3
b) 6 9
c) 2 15
d) compile time error
View Answer
Answer: c
Explanation: We have passed three values and it will manipulate according to the
given condition and yield the result as 2 15
Output:
$ g++ arg.cpp
$ a.out
2 15
3. Where does the return statement returns the execution of the program?
a) main function
b) caller function
c) same function
d) block function
View Answer
Answer: b
Explanation: The execution of the program is returned to the point from where the
function was called and the function from which this function was called is known as
caller function.
advertisement
4. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. int max(int a, int b )
4. {
5. return ( a > b ? a : b );
6. }
7. int main()
8. {
9. int i = 5;
10. int j = 7;
11. cout << max(i, j );
12. return 0;
13. }
a) 5
b) 7
c) either 5 or 7
d) 13
View Answer
Answer: b
Explanation: In this program, we are returning the maximum value by using
conditional operator.
Output:
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
$ g++ ret.cpp
$ a.out
7
1. #include <iostream>
2. using namespace std;
3. double & WeeklyHours()
4. {
5. double h = 46.50;
6. double &hours = h;
7. return hours;
8. }
9. int main()
10. {
11. double hours = WeeklyHours();
12. cout << "Weekly Hours: " << hours;
13. return 0;
14. }
a) 46.5
b) 6.50
c) compile time error
d) 26.5
View Answer
Answer: a
Explanation: We are returning the value what we get as input.
Output:
$ g++ ret1.cpp
$ a.out
46.5
1. #include <iostream>
2. using namespace std;
3. int mult (int x, int y)
4. {
5. int result;
6. result = 0;
7. while (y != 0)
8. {
9. result = result + x;
10. y = y - 1;
11. }
12. return(result);
13. }
14. int main ()
15. {
16. int x = 5, y = 5;
17. cout << mult(x, y) ;
18. return(0);
19. }
a) 20
b) 25
c) 30
d) 35
View Answer
Answer: b
Explanation: We are multiplying these values by adding every values.
Output:
$ g++ ret.cpp
$ a.out
25
1. #include <iostream>
2. using namespace std;
3. int gcd (int a, int b)
4. {
5. int temp;
6. while (b != 0)
7. {
8. temp = a % b;
9. a = b;
10. b = temp;
11. }
12. return(a);
13. }
14. int main ()
15. {
16. int x = 15, y = 25;
17. cout << gcd(x, y);
18. return(0);
19. }
a) 15
b) 25
c) 375
d) 5
View Answer
Answer: d
Explanation: In this program, we are finding the gcd of the number.
Output:
$ g++ ret5.cpp
$ a.out
5
1. #include <iostream>
2. using namespace std;
3. void print(int i)
4. {
5. cout << i;
6. }
7. void print(double f)
8. {
9. cout << f;
10. }
11. int main(void)
12. {
13. print(5);
14. print(500.263);
15. return 0;
16. }
a) 5500.263
b) 500.2635
c) 500.263
d) 500.266
View Answer
Answer: a
Explanation: In this program, we are printing the values and the values will be
print(5) will be printed first because of the order of the execution.
Output:
Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ over.cpp
$ a.out
5500.263
a) 11 12.1
b) 12.1 11
c) 11 12
d) compile time error
View Answer
Answer: d
Explanation: As one can observe that no function has declaration similar to that of
called Add(int, int) and Add(double, double) functions. Therefore, error occurs.
1. #include <iostream>
2. using namespace std;
3. int operate (int a, int b)
4. {
5. return (a * b);
6. }
7. float operate (float a, float b)
8. {
9. return (a / b);
10. }
11. int main()
12. {
13. int x = 5, y = 2;
14. float n = 5.0, m = 2.0;
15. cout << operate(x, y) <<"\t";
16. cout << operate (n, m);
17. return 0;
18. }
a) 10.0 5.0
b) 5.0 2.5
c) 10.0 5
d) 10 2.5
View Answer
Answer: d
Explanation: In this program, we are divide and multiply the values.
Output:
$ g++ over3.cpp
$ a.out
10 2.5
9. What should be passed in parameters when function does not require any
parameters?
a) void
b) blank space
c) both void & blank space
d) tab space
View Answer
Answer: b
Explanation: When we does not want to pass any argument to a function then we
leave the parameters blank i.e. func() – function without any parameter.
10. What are the advantages of passing arguments by reference?
a) Changes to parameter values within the function also affect the original
arguments
b) There is need to copy parameter values (i.e. less memory used)
c) There is no need to call constructors for parameters (i.e. faster)
d) All of the mentioned
View Answer
Answer: d
Explanation: All the above mentioned are advantages and properties of call by
reference.
1. If the user did not supply the value, what value will it take?
a) default value
b) rise an error
c) both default value & rise an error
d) error
View Answer
Answer: a
Explanation: If the user did not supply the value means, the compiler will take the
given value in the argument list.
3. Which value will it take when both user and default values are given?
a) user value
b) default value
c) custom value
d) defined value
View Answer
Answer: a
Explanation: The default value will be used when the user value is not given, So in
this case, the user value will be taken.
advertisement
4. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. void func(int a, bool flag = true)
4. {
5. if (flag == true )
6. {
7. cout << "Flag is true. a = " << a;
8. }
9. else
10. {
11. cout << "Flag is false. a = " << a;
12. }
13. }
14. int main()
15. {
16. func(200, false);
17. return 0;
18. }
$ g++ def.cpp
$ a.out
Flag is false. a = 200
1. #include <iostream>
2. #include <string>
3. using namespace std;
4. string askNumber(string prompt = "Please enter a number: ");
5. int main()
6. {
7. string number = askNumber();
8. cout << "Here is your number: " << number;
9. return 0;
10. }
11. string askNumber(string prompt)
12. {
13. string number;
14. cout << prompt;
15. cin >> number;
16. return number;
17. }
a) 5
b) 6
c) the number you entered
d) compile time error
View Answer
Answer: c
Explanation: In this program, we are getting a number and printing it.
Output:
$ g++ def1.cpp
$ a.out
Please enter a number:
5
Here is your number:5
1. #include <iostream>
2. using namespace std;
3. void Values(int n1, int n2 = 10)
4. {
5. using namespace std;
6. cout << "1st value: " << n1;
7. cout << "2nd value: " << n2;
8. }
9. int main()
10. {
11. Values(1);
12. Values(3, 4);
13. return 0;
14. }
a)
1st value: 1
10
4
b)
1st value: 1
10
10
8. If we start our function call with default arguments means, what will be
proceeding arguments?
a) user argument
b) empty arguments
c) default arguments
d) user & empty arguments
View Answer
Answer: c
Explanation: As a rule, the default argument must be followed by default arguments
only.
1. #include <iostream>
2. using namespace std;
3. int func(int m = 10, int n)
4. {
5. int c;
6. c = m + n;
7. return c;
8. }
9. int main()
10. {
11. cout << func(5);
12. return 0;
13. }
a) 15
b) 10
c) compile time error
d) 30
View Answer
Answer: c
Explanation: In function parameters the default arguments should always be the
rightmost parameters.
2. How can you access the arguments that are manipulated in the function?
a) va_list
b) arg_list
c) both va_list & arg_list
d) vg_list
View Answer
Answer: a
Explanation: va_list is provided by C++ to access manipulated arguments in function.
advertisement
1. #include <iostream>
2. #include <stdarg.h>
3. using namespace std;
4. float avg( int Count, ... )
5. {
6. va_list Numbers;
7. va_start(Numbers, Count);
8. int Sum = 0;
9. for (int i = 0; i < Count; ++i )
10. Sum += va_arg(Numbers, int);
11. va_end(Numbers);
12. return (Sum/Count);
13. }
14. int main()
15. {
16. float Average = avg(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
17. cout << "Average of first 10 whole numbers : " << Average;
18. return 0;
19. }
a) 4
b) 5
c) 6
d) 7
View Answer
Answer: a
Explanation: We are just calculating the average of these numbers using cstdarg.
Output:
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
$ g++ uka.cpp
$ a.out
Average of first 10 whole numbers 4
1. #include <iostream>
2. #include <stdarg.h>
3. using namespace std;
4. int add (int num, ...)
5. {
6. int sum = 0;
7. va_list args;
8. va_start (args,num);
9. for (int i = 0; i < num; i++)
10. {
11. int num = va_arg (args,int);
12. sum += num;
13. }
14. va_end (args);
15. return sum;
16. }
17. int main (void)
18. {
19. int total = add(8, 1, 2, -1, 4, 12, -2, 9, 7);
20. cout << "The result is " << total;
21. return 0;
22. }
a) 32
b) 23
c) 48
d) compile time error
View Answer
Answer: a
Explanation: We are adding these numbers by using for statement and stdarg.
Output:
$ g++ uka.cpp
$ a.out
The result is 32
1. #include <iostream>
2. #include <stdarg.h>
3. using namespace std;
4. void dumplist(int, ...);
5. int main()
6. {
7. dumplist(2, 4, 8);
8. dumplist(3, 6, 9, 7);
9. return 0;
10. }
11. void dumplist(int n, ...)
12. {
13. va_list p;
14. int i;
15. va_start(p, n);
16. while (n-->0)
17. {
18. i = va_arg(p, int);
19. cout << i;
20. }
21. va_end(p);
22. }
a) 2436
b) 48697
c) 1111111
d) compile time error
View Answer
Answer: b
Explanation: In this program, we are eradicating the first value
by comparing using while operator.
Output:
$ g++ rka3.cpp
$ a.out
48697
a) 6549
b) 4965
c) 6646
d) compile time error
View Answer
Answer: a
Explanation: In this program, we are returning the ascii value of the character and
printing it.
Output:
$ g++ rka4.cpp
$ a.out
6549
8. Which of the header file should be added in the following C++ code to properly
run the program?
1. #include <iostream>
2. using namespace std;
3. int print_all (int num, ...)
4. {
5. int sum = 0;
6. va_list args;
7. va_start (args,num);
8. for (int i = 0; i < num; i++)
9. {
10. int num = va_arg (args,int);
11. cout<<num<<" ";
12. }
13. va_end (args);
14. return sum;
15. }
16. int main (void)
17. {
18. print_all(8, 1, 2, -1, 4, 12, -2, 9, 7);
19. return 0;
20. }
a) stdlib.h
b) stdarg.h
c) string.h
d) stdpar.h
View Answer
Answer: b
Explanation: <stdarg.h> header provided to perform variable number of argument
passing.
1. #include <iostream>
2. #include <stdarg.h>
3. using namespace std;
4. void fun(std::string msg, ...);
5. int main()
6. {
7. fun("IndiaBIX", 1, 4, 7, 11, 0);
8. return 0;
9. }
10. void fun(std::string msg, ...)
11. {
12. va_list ptr;
13. int num;
14. va_start(ptr, msg);
15. num = va_arg(ptr, int);
16. num = va_arg(ptr, int);
17. cout << num;
18. }
a) 6
b) 5
c) 8
d) 4
View Answer
Answer: d
Explanation: In this program, we are moving the pointer to the second value and
printing it.
Output:
$ g++ uka6.cpp
$ a.out
4
10. What will initialize the list of arguments in stdarg.h header file?
a) va_list
b) va_start
c) va_arg
d) vg_arg
View Answer
Answer: b
Explanation: va_start initialises the the list of arguments in <stdarg.h> header file.
1. #include <iostream>
2. using namespace std;
3. int add(int first, int second)
4. {
5. return first + second + 15;
6. }
7. int operation(int first, int second, int (*functocall)(int, int))
8. {
9. return (*functocall)(first, second);
10. }
11. int main()
12. {
13. int a;
14. int (*plus)(int, int) = add;
15. a = operation(15, 10, plus);
16. cout << a;
17. return 0;
18. }
a) 25
b) 35
c) 40
d) 45
View Answer
Answer: c
Explanation: In this program, we are adding two numbers with 15, So we got the
output as 40.
Output:
Subscribe Now: C++ Newsletter | Important Subjects Newsletters
$ g++ pfu2.cpp
$ a.out
40
1. #include <iostream>
2. using namespace std;
3. void func(int x)
4. {
5. cout << x ;
6. }
7. int main()
8. {
9. void (*n)(int);
10. n = &func;
11. (*n)( 2 );
12. n( 2 );
13. return 0;
14. }
a) 2
b) 20
c) 21
d) 22
View Answer
Answer: d
Explanation: As we are calling the function two times with the same value, So it is
printing as 22.
Output:
$ g++ pfu.cpp
$ a.out
22
1. #include <iostream>
2. using namespace std;
3. int n(char, int);
4. int (*p) (char, int) = n;
5. int main()
6. {
7. (*p)('d', 9);
8. p(10, 9);
9. return 0;
10. }
11. int n(char c, int i)
12. {
13. cout << c << i;
14. return 0;
15. }
a)
d9
b) d9d9
c) d9
d) compile time error
View Answer
Answer: a
Explanation: As function pointer p is pointing to n(char, int), so for first call d9 will be
printed for second call 10, which corresponds to ‘\n’ character, and then 9 is printed.
Output:
$ g++ pfu1.cpp
$ a.out
d9
9
1. #include <iostream>
2. using namespace std;
3. int func (int a, int b)
4. {
5. cout << a;
6. cout << b;
7. return 0;
8. }
9. int main(void)
10. {
11. int(*ptr)(char, int);
12. ptr = func;
13. func(2, 3);
14. ptr(2, 3);
15. return 0;
16. }
a) 2323
b) 232
c) 23
d) compile time error
View Answer
Answer: d
Explanation: In this program, we can’t do the casting from char to int, So it is raising
an error.
int(*ptr[5])();
1. #include <iostream>
2. using namespace std;
3. #define MIN(a,b) (((a)<(b)) ? a : b)
4. int main ()
5. {
6. float i, j;
7. i = 100.1;
8. j = 100.01;
9. cout <<"The minimum is " << MIN(i, j) << endl;
10. return 0;
11. }
a) 100.01
b) 100.1
c) compile time error
d) 100
View Answer
Answer: a
Explanation: In this program, we are getting the minimum number using conditional
operator.
Output:
$ g++ mac3.cpp
$ a.out
The minimum value is 100.01
1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. cout << "Value of __LINE__ : " << __LINE__ << endl;
6. cout << "Value of __FILE__ : " << __FILE__ << endl;
7. cout << "Value of __DATE__ : " << __DATE__ << endl;
8. cout << "Value of __TIME__ : " << __TIME__ << endl;
9. return 0;
10. }
a) 5
b) details about your file
c) compile time error
d) runtime error
View Answer
Answer: b
Explanation: In this program, we are using the macros to print the information
about the file.
Output:
$ g++ mac2.cpp
$ a.out
Value of __LINE__ : 5
Value of __FILE__ : mac1.cpp
Value of __DATE__ : Oct 10 2012
Value of __TIME__ : 22:24:37
1. #include <iostream>
2. using namespace std;
3. #define SquareOf(x) x * x
4. int main()
5. {
6. int x;
7. cout << SquareOf(x + 4);
8. return 0;
9. }
a) 16
b) 64
c) compile time error
d) 75386824
View Answer
Answer: d
Explanation: In this program, as we have not initialize the variable x, we will get a
output of ending digit of 4.
Output:
$ g++ mac1.cpp
$ a.out
75386824
1. #include <iostream>
2. using namespace std;
3. #define PR(id) cout << "The value of " #id " is "<<id
4. int main()
5. {
6. int i = 10;
7. PR(i);
8. return 0;
9. }
a) 10
b) 15
c) 20
d) 12
View Answer
Answer: a
Explanation: In this program, we are just printing the declared values.
Output:
$ g++ mac.cpp
$ a.out
10
1. #include <iostream>
2. using namespace std;
3. #define MAX 10
4. int main()
5. {
6. int num;
7. num = ++MAX;
8. cout << num;
9. return 0;
10. }
a) 11
b) 10
c) compile time error
d) 13
View Answer
Answer: c
Explanation: Macro Preprocessor only replaces occurance of macro symbol with
macro symbol value, So we can’t increment the value.
2. What is the ability to group some lines of code that can be included?
in the program?
a) specific task
b) program control
c) modularization
d) macros
View Answer
Answer: c
Explanation: Modularization is also similar to macros but it is used to build large
projects.
1. #include <iostream>
2. using namespace std;
3. namespace first
4. {
5. int var = 5;
6. }
7. namespace second
8. {
9. double var = 3.1416;
10. }
11. int main ()
12. {
13. int a;
14. a = first::var + second::var;
15. cout << a;
16. return 0;
17. }
a) 8.31416
b) 8
c) 9
d) compile time error
View Answer
Answer: b
Explanation: As we are getting two variables from namespace variable and we are
adding that.
Output:
$ g++ name.cpp
$ a.out
8
a) 11
b) 01
c) 00
d) 10
View Answer
Answer: d
Explanation: We are inter mixing the variable and comparing it which is bigger and
smaller and according to that we are printing the output.
Output:
$ g++ name1.cpp
$ a.out
10
1. #include <iostream>
2. using namespace std;
3. namespace Box1
4. {
5. int a = 4;
6. }
7. namespace Box2
8. {
9. int a = 13;
10. }
11. int main ()
12. {
13. int a = 16;
14. Box1::a;
15. Box2::a;
16. cout << a;
17. return 0;
18. }
a) 4
b) 13
c) 16
d) compile time error
View Answer
Answer: c
Explanation: In this program, as there is lot of variable a and it is printing the value
inside the block because it got the highest priority.
Output:
$ g++ name2.cpp
$ a.out
16
1. #include <iostream>
2. using namespace std;
3. namespace space
4. {
5. int x = 10;
6. }
7. namespace space
8. {
9. int y = 15;
10. }
11. int main(int argc, char * argv[])
12. {
13. space::x = space::y =5;
14. cout << space::x << space::y;
15. }
a) 1015
b) 1510
c) 55
d) compile time error
View Answer
Answer: c
Explanation: We are overriding the value at the main function and so we are getting
the output as 55.
Output:
$ g++ name4.cpp
$ a.out
55
1. #include <iostream>
2. using namespace std;
3. namespace extra
4. {
5. int i;
6. }
7. void i()
8. {
9. using namespace extra;
10. int i;
11. i = 9;
12. cout << i;
13. }
14. int main()
15. {
16. enum letter { i, j};
17. class i { letter j; };
18. ::i();
19. return 0;
20. }
a) 9
b) 10
c) compile time error
d) 11
View Answer
Answer: a
Explanation: A scope resolution operator without a scope qualifier refers to the
global namespace.
namespace A{
int i
}
advertisement
b)
namespace B{
int i;
};
c)
namespace C{
int i;
d)
Namespace D{
int i
View Answer
Answer: c
Explanation: A namespace definition always starts with the namespace keyword so
definition with Namespace(capital N) is wrong. namespace does is not terminated
by a semicolon hence the definition with a semicolon is wrong. every variable
declaration in C++ should end with semicolon therefore namespace containing ‘int i’
without semicolon is wrong.
#include <iostream>
#include <string>
using namespace std;
namespace A{
a) 10
b) 5
c) Error
d) 105
View Answer
Answer: c
Explanation: Variable cout is defined in above defined namespace B and also in the
inbuilt namespace std. So the compiler confuses and throws an error saying that
cout is ambiguous i.e. which cout to use as it i available in both std and B
namespace.
#include <iostream>
#include <string>
using namespace std;
namespace A{
a) 5
b) 10
c) Error
d) Wrong use of namespace
View Answer
Answer: a
Explanation: As we have mentioned that ‘using namespace B’ so now whereever var
will be used it will be from namespace B. hence the output was the value of var
from namespace B.
#include <iostream>
#include <string>
using namespace std;
namespace A{
a) 5
b) 10
c) 20
d) Error
View Answer
Answer: c
Explanation: As var is already declared in this scope so that gets preference over
others. Therefore 20 is printed which is the value assigned to var declared in this
scope.
#include <iostream>
#include <string>
using namespace std;
namespace
{
int var = 10;
}
int main()
{
cout<<var;
}
a) 10
b) Error
c) Some garbage value
d) Nothing but program runs perfectly
View Answer
Answer: a
Explanation: A namespace without name is called unnamed namespace and is valid
in that scope only. So its like global scope of variable. One can access that var from
main() function.
namespace A{
int var = 10;
namespace B{
int i = 15;
}
}
a) cout<<A::i;
b) cout<<B::i;
c) cout<<A::B::i;
d) cout<<i;
View Answer
Answer: c
Explanation: Here namespace B is nested inside the namespace A. Hence to access
the variable i we need to mention through B and A. So it should A::B::i, which means
i belongs to namespace B which is defined inside the namespace A.
#include <iostream>
#include <string>
using namespace std;
namespace My_old_school_and_college_friends_number
{
long int f1 = 9999999999;
long int f2 = 1111111111;
}
namespace contacts = My_old_school_and_college_friends_number;
int main(){
cout<<contacts::f1;
}
a) 9999999999
b) 1111111111
c) error
d) segmentation fault
View Answer
Answer: a
Explanation: C++ allows to use namespaces aliases i.e. if a namespace having a large
name we can assign it to a new namespace having a small name for the
convenience in coding. This assignment of namespaces is called namespace
aliasing.
h2.h
#include <iostream>
using namespace std;
namespace B{
float func(float a){
cout<<"using namespace B";
return 2*a;
}
}
------------------------------------------------
Content of program.cpp
------------------------------------------------
#include <iostream>
#include <string>
#include "h1.h"
#include "h2.h"
using namespace std;
using namespace A;
using namespace B;
int main(int argc, char const *argv[])
{
/* code */
int a = 10;
float b = 10.0;
cout<<func(a)<<endl;
cout<<func(b);
return 0;
}
-----------------------------------------------
a)
b)
h2.h
#include <iostream>
using namespace std;
namespace B{
float func(float a){
cout<<"using namespace B";
return 2*a;
}
}
------------------------------------------------
Content of program.cpp
------------------------------------------------
#include <iostream>
#include <string>
#include "h1.h"
#include "h2.h"
using namespace std;
using namespace A;
using namespace B;
int main(int argc, char const *argv[])
{
/* code */
int a = 10;
float b = 10.0;
cout<<A::func(a)<<endl;
cout<<A::func(b);
return 0;
}
------------------------------------------------
a)
b)
c)
d)
View Answer
Answer: b
Explanation: Here we have specified that func() should be called from the
namespace A, hence both the calls will use the same function from namespace A.
13. What changes you can do in the header files to avoid the redefinition that
compiler will give when both the header files are included in the same program
keeping the declaration of both the functions same?
Content of h1.h
------------------------------------------------
h1.h
#include <iostream>
using namespace std;
int func(int a){
cout<<"Multiplied by 2";
return 2*a;
}
------------------------------------------------
Content of h2.h
------------------------------------------------
h2.h
#include <iostream>
using namespace std;
int func(int a){
cout<<"divided by 2";
return a/2;
}
------------------------------------------------
Content of h2.h
------------------------------------------------
#include <iostream>
using namespace std;
namespace B{
float func(float a){
cout<<"divided by 2";
return a/2;
}
}
------------------------------------------------
Now one can use multiplication func as A::func(int); and dividion function as
B::func(int).
1. To where does the program control transfers when the exception is arisen?
a) catch
b) handlers
c) throw
d) try
View Answer
Answer: b
Explanation: When an exception is arisen mean, the exception is caught by handlers
and then it decides the type of exception.
3. What will happen when the exception is not caught in the program?
a) error
b) program will execute
c) block of that code will not execute
d) program will execute & displays wrong output
View Answer
Answer: a
Explanation: When exceptions are not caught in any program then program throws
error.
advertisement
4. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int age = 0;
6. try
7. {
8. if (age < 0)
9. {
10. throw "Positive Number Required";
11. }
12. cout << age;
13. }
14. catch(const char *Message)
15. {
16. cout << "Error: " << Message;
17. }
18. return 0;
19. }
a) 0
b) error:Positive Number Required
c) compile time error
d) runtime error
View Answer
Answer: a
Explanation: As the zero marks the beginning of the positive number, it is printed as
output
Output:
Subscribe Now: C++ Newsletter | Important Subjects Newsletters
$ g++ excep.cpp
$ a.out
0
1. #include <iostream>
2. using namespace std;
3. void PrintSequence(int StopNum)
4. {
5. int Num;
6. Num = 1;
7. while (true)
8. {
9. if (Num >= StopNum)
10. throw Num;
11. cout << Num;
12. Num++;
13. }
14. }
15. int main(void)
16. {
17. try
18. {
19. PrintSequence(20);
20. }
21. catch(int ExNum)
22. {
23. cout << "Caught an exception with value: " << ExNum;
24. }
25. return 0;
26. }
1. #include <iostream>
2. using namespace std;
3. double division(int a, int b)
4. {
5. if (b == 0)
6. {
7. throw "Division by zero condition!";
8. }
9. return (a / b);
10. }
11. int main ()
12. {
13. int x = 50;
14. int y = 2;
15. double z = 0;
16. try
17. {
18. z = division(x, y);
19. cout << z;
20. }
21. catch(const char *msg)
22. {
23. cerr << msg;
24. }
25. return 0;
26. }
a) 25
b) 20
c) Division by zero condition!
d) 35
View Answer
Answer: a
Explanation: In this program, we resembling the division by using the exception
handling.
Output:
$ g++ excep2.cpp
$ a.out
25
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char* buff;
6. try
7. {
8. buff = new char[1024];
9. if (buff == 0)
10. throw "Memory allocation failure!";
11. else
12. cout << sizeof(buff) << "Byte successfully allocated!"<<endl;
13. }
14. catch(char *strg)
15. {
16. cout<<"Exception raised: "<<strg<<endl;
17. }
18. return 0;
19. }
1. #include <iostream>
2. using namespace std;
3. void Funct();
4. int main()
5. {
6. try
7. {
8. Funct();
9. }
10. catch(double)
11. {
12. cerr << "caught a double type..." << endl;
13. }
14. return 0;
15. }
16. void Funct()
17. {
18. throw 3;
19. }
1. #include <iostream>
2. #include <exception>
3. using namespace std;
4. int main()
5. {
6. try
7. {
8. int * array1 = new int[100000000];
9. int * array2 = new int[100000000];
10. int * array3 = new int[100000000];
11. int * array4 = new int[100000000];
12. cout << "Allocated successfully";
13. }
14. catch(bad_alloc&)
15. {
16. cout << "Error allocating the requested memory." << endl;
17. }
18. return 0;
19. }
a) Allocated successfully
b) Error allocating the requested memory
c) Depends on the memory of the computer
d) Error
View Answer
Answer: c
Explanation: In this program, we allocating the memory to the arrays by using
exception handling and we handled the exception by standard exception.
Output:
$ g++ excep5.cpp
$ a.out
Allocated successfully
10. What will happen when the handler is not found for an exception?
a) calls the standard library function terminate()
b) raise an error
c) executes the remaining block
d) raise an error and executes the remaining block
View Answer
Answer: a
Explanation: None.
1. #ifndef Exercise_H
2. #define Exercise_H
3. int number = 842;
4. #endif
ii.
1. #include <iostream>
2. #include "exe.h"
3. using namespace std;
4. int main(int argc, char * argv[] )
5. {
6. cout << number++;
7. return 0;
8. }
a) 842
b) 843
c) compile time error
d) 845
View Answer
Answer: a
Explanation: In this program, we have created a header file and linked that into the
source program and we post incrementing that because of that it is printed as 842.
Output:
$ g++ link.cpp
$ a.out
842
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char name[30];
6. cout << "Enter name: ";
7. gets(name);
8. cout << "Name: ";
9. puts(name);
10. return 0;
11. }
a) jobsjobs
b) jobs
c) compile time error
d) program will not run
View Answer
Answer: c
Explanation: This program will run on older version of C++ with the inclusion of
#include header file, but for on new compiler C++14 and above the gets is removed
from the header file so it will not run on them even after inclusion of cstdio header
file.
9. Which of the following header files is required for creating and reading data files?
a) ofstream.h
b) fstream.h
c) ifstream.h
d) console.h
View Answer
Answer: b
Explanation: In this fstream.h header file is used for accessing the files only.
1. #include <iostream>
2. #include <stdarg.h>
3. using namespace std;
4. float avg( int Count, ... )
5. {
6. va_list Numbers;
7. va_start(Numbers, Count);
8. int Sum = 0;
9. for (int i = 0; i < Count; ++i)
10. Sum += va_arg(Numbers, int);
11. va_end(Numbers);
12. return (Sum/Count);
13. }
14. int main()
15. {
16. float Average = avg(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
17. cout << Average;
18. return 0;
19. }
a) 4
b) 5
c) 6
d) compile time error
View Answer
Answer: a
Explanation: In this program, we are finding the average of first 10 numbers using
stdarg header file
Output:
$ g++ std.cpp
$ a.out
4
1. #include <iostream>
2. using namespace std;
3. class rect
4. {
5. int x, y;
6. public:
7. void val (int, int);
8. int area ()
9. {
10. return (x * y);
11. }
12. };
13. void rect::val (int a, int b)
14. {
15. x = a;
16. y = b;
17. }
18. int main ()
19. {
20. rect rect;
21. rect.val (3, 4);
22. cout << "rect area: " << rect.area();
23. return 0;
24. }
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
View Answer
Answer: b
Explanation: In this program, we are calculating the area of rectangle based on
given values.
Output:
$ g++ class.cpp
$ a.out
rect area: 12
1. #include <iostream>
2. using namespace std;
3. class CDummy
4. {
5. public:
6. int isitme (CDummy& param);
7. };
8. int CDummy::isitme (CDummy& param)
9. {
10. if (¶m == this)
11. return true;
12. else
13. return false;
14. }
15. int main ()
16. {
17. CDummy a;
18. CDummy *b = &a;
19. if (b->isitme(a))
20. {
21. cout << "execute";
22. }
23. else
24. {
25. cout<<"not execute";
26. }
27. return 0;
28. }
a) execute
b) not execute
c) error
d) both execute & not execute
View Answer
Answer: a
Explanation: In this program, we are just pointing the pointer to a object and
printing execute if it is correctly pointed.
Output:
$ g++ class1.cpp
$ a.out
execute
8. The data members and functions of a class in C++ are by default ____________
a) protected
b) private
c) public
d) public & protected
View Answer
Answer: b
Explanation: By default all the data members and member functions of class are
private.
10. 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
View Answer
Answer: a
Explanation: For structures, by default all the data members and member functions
are public.
2. Which operator a pointer object of a class uses to access its data members and
member functions?
a) .
b) ->
c) :
d) ::
View Answer
Answer: b
Explanation: ->(arrow operator) is used by a pointer object to access members of its
class.
advertisement
#include <iostream>
#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();
}
a) 5
b) 10
c) Error
d) Segmentation fault
View Answer
Answer: c
Explanation: As the assign() is a constant function and a constant function cannot
change the state of an object and as in the assign function we are trying to modify
the member a of the object therefore the program gives error.
---------------------------
Example class:
class A
{
public:
static int value;
}
---------------------------
a) A.value
b) A::value
c) A->value
d) A^value
View Answer
Answer: b
Explanation: Scope resolution operator(::) is used to access a static member of a
class.
#include <iostream>
#include <string>
using namespace std;
class A
{
mutable int a;
public:
int assign(int i) const {
a = i;
}
int return_value() const {
return a;
}
};
a) 5
b) Error
c) Segmentation fault
d) Undefined value
View Answer
Answer: a
Explanation: As a is mutable member of the class it’s value can be modified whether
it is a part of constant object or not. It can be modified even inside a constant
member function. Hence, the program tuns fine and does not gives any error.
#include <iostream>
using namespace std;
class S
{
int m;
public:
#define MAC(S::m)
};
a) Hello World
b) Error
c) Segmentation Fault
d) Blank Space
View Answer
Answer: b
Explanation: Macros cannot access the private member of a class therefore #define
MAC(S::m) will give an error.
#include <iostream>
#include <string>
using namespace std;
class A
{
static int a;
public:
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
a) 5
b) Garbage value
c) Error
d) Segmentation fault
View Answer
Answer: c
Explanation: Every static member of a class is initialised before its use. As ‘a’ is a
static member of the class and is not initialised so the program will give error.
#include <iostream>
#include <string>
using namespace std;
class A
{
static int a;
public:
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
int A::a = 5;
a) 1055
b) 555
c) 101010
d) 51010
View Answer
Answer: c
Explanation: As ‘a’ is a static member of the class so it is a type of global variable to
the class i.e. any change made by one object is reflected back to all the other
objects. Hence when a is changed to 10 by object a1, so value of ‘a’ becomes 10 for
each object and 3 times 10 is printed.
#include <iostream>
#include <string>
using namespace std;
class A
{
int a = 5;
public:
void change(int i){
a = i;
}
static void value_of_a(){
cout<<a;
}
};
a) 10
b) Error
c) Segmentation Fault
d) 5
View Answer
Answer: b
Explanation: As value_of_a() is a static function and static member can access only
static members therefore the program will give error.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. typedef int num;
6. num a = 10, b = 15;
7. num c = a + b + a - b;
8. cout << c;
9. return 0;
10. }
a) 20
b) 15
c) 30
d) 25
View Answer
Answer: a
Explanation: In this program, we are manipulating the numbers and printing the
result using user-defined data types.
Output:
Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ user.cpp
$ a.out
20
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i;
6. enum month
7. {
8. JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC
9. };
10. for (i = JAN; i <= DEC; i++)
11. cout << i;
12. return 0;
13. }
a) 012345678910
b) 0123456789
c) 01234567891011
d) 01234567891011122
View Answer
Answer: a
Explanation: In this program, we are defined the data types as enumerator and
printing its value in a order.
Output:
$ g++ user1.cpp
$ a.out
012345678910
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. typedef int num;
6. typedef char let;
7. let w = "steve";
8. num a = 10, b = 15;
9. num c = a + w;
10. cout << c;
11. return 0;
12. }
a) 10steve
b) steve10
c) compile time error
d) compile but not run
View Answer
Answer: c
Explanation: Error: invalid conversion from ‘const char*’ to ‘let {aka char}’.
10. How many types of models are available to create the user-defined data type?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: There are two types of models. They are references to built-in types
and multipart types.
3. Which of these following members are not accessed by using direct member
access operator?
a) public
b) private
c) protected
d) both private & protected
View Answer
Answer: d
Explanation: Because of the access is given to the private and protected, We can’t
access them by using direct member access operator.
advertisement
4. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. public :
6. double length;
7. double breadth;
8. double height;
9. };
10. int main( )
11. {
12. Box Box1;
13. double volume;
14. Box1.height = 5;
15. Box1.length = 6;
16. Box1.breadth = 7.1;
17. volume = Box1.height * Box1.length * Box1.breadth;
18. cout << "Volume of Box1 : " << volume <<endl;
19. return 0;
20. }
a) 210
b) 213
c) 215
d) 217
View Answer
Answer: b
Explanation: In the above program, we are calculating the area of the cube by using
the cube formula
Output:
Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ obj1.cpp
$ a.out
213
1. #include <iostream>
2. using namespace std;
3. class Rect
4. {
5. int x, y;
6. public:
7. void set_values (int,int);
8. int area ()
9. {
10. return (x * y);
11. }
12. };
13. void Rect::set_values (int a, int b)
14. {
15. x = a;
16. y = b;
17. }
18. int main ()
19. {
20. Rect recta, rectb;
21. recta.set_values (5, 6);
22. rectb.set_values (7, 6);
23. cout << "recta area: " << recta.area();
24. cout << "rectb area: " << rectb.area();
25. return 0;
26. }
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. private:
6. int var;
7. public:
8. void input()
9. {
10. cout << var;
11. }
12. void output()
13. {
14. cout << "Variable entered is ";
15. cout << var << "\n";
16. }
17. };
18. int main()
19. {
20. sample object;
21. object.input();
22. object.output();
23. object.var();
24. return 0;
25. }
a)
Enter an integer 5
Variable entered is 5
b) Runtime error
c) Error
d)
Enter an integer 7
Variable entered is 7
View Answer
9. Which special character is used to mark the end of class?
a) ;
b) :
c) #
d) $
View Answer
Answer: a
Explanation: Similar to ending any statement, a class is also terminated with
semicolon(;).
1. #include <iostream>
2. using namespace std;
3. class number
4. {
5. int i;
6. public:
7. int geti();
8. void puti(int j);
9. };
10. int number::geti()
11. {
12. return i;
13. }
14. void number::puti(int j)
15. {
16. i = j;
17. }
18. int main()
19. {
20. number s;
21. s.puti(10);
22. cout << s.geti( );
23. return 0;
24. }
a) 10
b) 11
c) 20
d) 22
View Answer
Answer: a
Explanation: We are getting the number and copying it to j and printing it.
Output:
$ g++ obj2.cpp
$ a.out
10
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. public:
6. int x, y;
7. sample() {};
8. sample(int, int);
9. sample operator + (sample);
10. };
11. sample::sample (int a, int b)
12. {
13. x = a;
14. y = b;
15. }
16. sample sample::operator+ (sample param)
17. {
18. sample temp;
19. temp.x = x + param.x;
20. temp.y = y + param.y;
21. return (temp);
22. }
23. int main ()
24. {
25. sample a (4,1);
26. sample b (3,2);
27. sample c;
28. c = a + b;
29. cout << c.x << "," << c.y;
30. return 0;
31. }
a) 5, 5
b) 7, 3
c) 3, 7
d) 3, 5
View Answer
Answer: b
Explanation: In this program, we are adding the first number of a with first number
of b by using operator function and also we are adding second number by this
method also.
Output:
Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ oper.cpp
$ a.out
7, 3
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. double length;
6. double breadth;
7. double height;
8. public:
9. double getVolume(void)
10. {
11. return length * breadth * height;
12. }
13. void setLength( double len )
14. {
15. length = len;
16. }
17. void setBreadth( double bre )
18. {
19. breadth = bre;
20. }
21. void setHeight( double hei )
22. {
23. height = hei;
24. }
25. Box operator+(const Box& b)
26. {
27. Box box;
28. box.length = this->length + b.length;
29. box.breadth = this->breadth + b.breadth;
30. box.height = this->height + b.height;
31. return box;
32. }
33. };
34. int main( )
35. {
36. Box Box1;
37. Box Box2;
38. Box Box3;
39. double volume = 0.0;
40. Box1.setLength(6.0);
41. Box1.setBreadth(7.0);
42. Box1.setHeight(5.0);
43. Box2.setLength(12.0);
44. Box2.setBreadth(13.0);
45. Box2.setHeight(10.0);
46. volume = Box1.getVolume();
47. cout << "Volume of Box1 : " << volume <<endl;
48. volume = Box2.getVolume();
49. cout << "Volume of Box2 : " << volume <<endl;
50. Box3 = Box1 + Box2;
51. volume = Box3.getVolume();
52. cout << "Volume of Box3 : " << volume <<endl;
53. return 0;
54. }
a)
b)
c)
d)
View Answer
Answer: a
Explanation: In this program, we finding the box3 area by adding box1 and box2.
Output:
$ g++ oper1.cpp
$ a.out
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
a)
operator+
operator+=
b)
operator+=
operator+
c)
operator+
operator+
d)
operator+
operator=
View Answer
Answer: a
Explanation: We are using two operator functions and executing them and the
result is printed according to the order.
Output:
$ g++ oper2.cpp
$ a.out
operator+
operator+=
1. #include <iostream>
2. using namespace std;
3. class myclass
4. {
5. public:
6. int i;
7. myclass *operator->()
8. {return this;}
9. };
10. int main()
11. {
12. myclass ob;
13. ob->i = 10;
14. cout << ob.i << " " << ob->i;
15. return 0;
16. }
a) 10 10
b) 11 11
c) error
d) runtime error
View Answer
Answer: a
Explanation: In this program, -> operator is used to describe the member of the
class and so we are getting this output.
Output:
$ g++ char4.cpp
$ a.out
10 10
1. #include <iostream>
2. using namespace std;
3. ostream & operator<<(ostream & i, int n)
4. {
5. return i;
6. }
7. int main()
8. {
9. cout << 5 << endl;
10. cin.get();
11. return 0;
12. }
a) 5
b) 6
c) error
d) runtime error
View Answer
Answer: c
Explanation: In this program, there will arise an ambiguous overload for 5.
OOP MCQ
3. Which was the first purely object oriented programming language developed?
a) Kotlin
b) SmallTalk
c) Java
d) C++
View Answer
Answer: b
Explanation: SmallTalk was the first programming language developed which was
purely object oriented. It was developed by Alan Kay. OOP concept came into the
picture in 1970’s.
11. The feature by which one object can interact with another object is _____________
a) Message reading
b) Message Passing
c) Data transfer
d) Data Binding
View Answer
Answer: b
Explanation: The interaction between two object is called the message passing
feature. Data transfer is not a feature of OOP. Also, message reading is not a feature
of OOP.
12. Which among the following, for a pure OOP language, is true?
a) The language should follow at least 1 feature of OOP
b) The language must follow only 3 features of OOP
c) The language must follow all the rules of OOP
d) The language should follow 3 or more features of OOP
View Answer
Answer: c
Explanation: The language must follow all the rules of OOP to be called a purely
OOP language. Even if a single OOP feature is not followed, then it’s known to be a
partially OOP language.
13. How many types of access specifiers are provided in OOP (C++)?
a) 4
b) 3
c) 2
d) 1
View Answer
Answer: b
Explanation: Only 3 types of access specifiers are available. Namely, private,
protected and public. All these three can be used according to the need of security
of members.
14. In multilevel inheritance, which is the most significant feature of OOP used?
a) Code efficiency
b) Code readability
c) Flexibility
d) Code reusability
View Answer
Answer: d
Explanation: The classes using multilevel inheritance will use the code in all the
subsequent subclasses if available. Hence the most significant feature among the
options given is code reusability. This feature is generally intended to use the data
values and reuse the redundant functions.
17. Which constructor will be called from the object created in the below C++ code?
class A
{
int i;
A()
{
i=0; cout<<i;
}
A(int x=0)
{
i=x; cout<<I;
}
};
A obj1;
a) Parameterized constructor
b) Default constructor
c) Run time error
d) Compile time error
View Answer
Answer: d
Explanation: When a default constructor is defined and another constructor with 1
default value argument is defined, creating object without parameter will create
ambiguity for the compiler. The compiler won’t be able to decide which constructor
should be called, hence compile time error.
20. In which access should a constructor be defined, so that object of the class can
be created in any function?
a) Any access specifier will work
b) Private
c) Public
d) Protected
View Answer
Answer: c
Explanation: Constructor function should be available to all the parts of program
where the object is to be created. Hence it is advised to define it in public access, so
that any other function is able to create objects.
21. Which among the following is correct for the class defined below?
class student
{
int marks;
public: student(){}
student(int x)
{
marks=x;
}
};
main()
{
student s1(100);
student s2();
student s3=100;
return 0;
}
23. Which constructor will be called from the object obj2 in the following C++
program?
class A
{
int i;
A()
{
i=0;
}
A(int x)
{
i=x+1;
}
A(int y, int x)
{
i=x+y;
}
};
A obj1(10);
A obj2(10,20);
A obj3;
a) A(int y, int x)
b) A(int y; int x)
c) A(int y)
d) A(int x)
View Answer
Answer: a
Explanation: The two argument constructor will be called as we are passing 2
arguments to the object while creation. The arguments will be passed together and
hence compiler resolves that two argument constructor have to be called.
29. Which keyword among the following can be used to declare an array of objects
in java?
a) allocate
b) arr
c) new
d) create
View Answer
Answer: c
Explanation: The keyword new can be used to declare an array of objects in java.
The syntax must be specified with an object pointer which is assigned with a
memory space containing the required number of object space. Even initialization
can be done directly.
30. Which operator can be used to free the memory allocated for an object in C++?
a) Unallocate
b) Free()
c) Collect
d) delete
View Answer
Answer: d
Explanation: The delete operator in C++ can be used to free the memory and
resources held by an object. The function can be called explicitly whenever required.
In C++ memory management must be done by the programmer. There is no
automatic memory management in C++.
32. Which type of members can’t be accessed in derived classes of a base class?
a) All can be accessed
b) Protected
c) Private
d) Public
View Answer
Answer: c
Explanation: The private members can be accessed only inside the base class. If the
class is derived by other classes. Those members will not be accessible. This concept
of OOP is made to make the members more secure.
37. What happens if non static members are used in static member function?
a) Executes fine
b) Compile time error
c) Executes if that member function is not used
d) Runtime error
View Answer
Answer: b
Explanation: There must be specific memory space allocated for the data members
before the static member functions uses them. But the space is not reserved if
object is not declared. Hence only if static members are not used, it leads to compile
time error.
46. Which class/set of classes can illustrate polymorphism in the following C++
code?
47. If data members are private, what can we do to access them from the class
object?
a) Private data members can never be accessed from outside the class
b) Create public member functions to access those data members
c) Create private member functions to access those data members
d) Create protected member functions to access those data members
View Answer
Answer: b
Explanation: We can define public member functions to access those private data
members and get their value for use or alteration. They can’t be accessed directly
but is possible to be access using member functions. This is done to ensure that the
private data doesn’t get modified accidentally.
48. Which among the following is not a necessary condition for constructors?
a) Its name must be same as that of class
b) It must not have any return type
c) It must contain a definition body
d) It can contains arguments
View Answer
Answer: c
Explanation: Constructors are predefined implicitly, even if the programmer doesn’t
define any of them. Even if the programmer declares a constructor, it’s not
necessary that it must contain some definition.
50. If in multiple inheritance, class C inherits class B, and Class B inherits class A. In
which sequence are their destructors called if an object of class C was declared?
a) ~A() then ~B() then ~C()
b) ~C() then ~A() then ~B()
c) ~C() then ~B() then ~A()
d) ~B() then ~C() then ~A()
View Answer
Answer: c
Explanation: The destructors are always called in the reverse order of how the
constructors were called. Here class A constructor would have been created first if
Class C object is declared. Hence class A destructor is called at last.