0% found this document useful (0 votes)
59 views263 pages

C++ MCQ

The document contains a series of multiple-choice questions (MCQs) related to the C++ programming language, covering topics such as syntax, data types, object-oriented programming concepts, and error handling. Each question is followed by the correct answer and an explanation. The content is designed to test knowledge and understanding of C++ for learners and developers.

Uploaded by

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

C++ MCQ

The document contains a series of multiple-choice questions (MCQs) related to the C++ programming language, covering topics such as syntax, data types, object-oriented programming concepts, and error handling. Each question is followed by the correct answer and an explanation. The content is designed to test knowledge and understanding of C++ for learners and developers.

Uploaded by

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

C++ MCQs

1. Who invented C++?


a) Dennis Ritchie
b) Ken Thompson
c) Brian Kernighan
d) Bjarne Stroustrup
View Answer
Answer: d
Explanation: Bjarne Stroustrup is the original creator of C++ in 1979 at AT&T Bell
Labs.

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”.

4. Which of the following is used for comments in C++?


a) /* comment */
b) // comment */
c) // comment
d) both // comment or /* comment */
View Answer
Answer: d
Explanation: Both the ways are used for commenting in C++ programming. // is
used for single line comments and /* … */ is used for multiple line comments.
5. Which of the following user-defined header file extension used in c++?
a) hg
b) cpp
c) h
d) hf
View Answer
Answer: c
Explanation: .h extensions are used for user defined header files. To include a user
defined header file one should use #include”name.h” i.e. enclosed within double
quotes.
advertisement
6. Which of the following is a correct identifier in C++?
a) VAR_1234
b) $var_name
c) 7VARNAME
d) 7var_name
View Answer
Answer: a
Explanation: The rules for writing an identifier is as follows:
i) may contain lowercase/uppercase letters, digits or underscore(_) only
ii) should start with a non-digit character
iii) should not contain any special characters like @, $, etc.

7. Which of the following is not a type of Constructor in C++?


a) Default constructor
b) Parameterized constructor
c) Copy constructor
d) Friend constructor
View Answer
Answer: d
Explanation: Friend function is not a constructor whereas others are a type of
constructor used for object initialization.

8. Which of the following approach is used by C++?


a) Left-right
b) Right-left
c) Bottom-up
d) Top-down
View Answer
Answer: c
Explanation: C++ is an object-oriented language and OOL uses a bottom-up
approach to solve/view a problem.
9. What is virtual inheritance in C++?
a) C++ technique to enhance multiple inheritance
b) C++ technique to ensure that a private member of the base class can be accessed
somehow
c) C++ technique to avoid multiple inheritances of classes
d) C++ technique to avoid multiple copies of the base class into children/derived
class
View Answer
Answer: d
Explanation: Virtual inheritance is a C++ technique with which it ensures that a
derived class contains only one copy of the base class’s variables. Refer Wikipedia
for more info.

10. What happens if the following C++ statement is compiled and executed?

int *ptr = NULL;


delete ptr;

a) The program is not semantically correct


b) The program is compiled and executed successfully
c) The program gives a compile-time error
d) The program compiled successfully but throws an error during run-time
View Answer
Answer: b
Explanation: The above statement is syntactically and semantically correct as C++
allows the programmer to delete a NULL pointer, therefore, the program is
compiled and executed successfully.

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

#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.

12. What is the difference between delete and delete[] in C++?


a) delete is syntactically correct but delete[] is wrong and hence will give an error if
used in any case
b) delete is used to delete normal objects whereas delete[] is used to pointer objects
c) delete is a keyword whereas delete[] is an identifier
d) delete is used to delete single object whereas delete[] is used to
multiple(array/pointer of) objects
View Answer
Answer: d
Explanation: delete is used to delete a single object initiated using new keyword
whereas delete[] is used to delete a group of objects initiated with the new
operator.

13. What happens if the following program is executed in C and C++?

#include <stdio.h>
int main(void)
{
int new = 5;
printf("%d", new);
}

a) Error in C and successful execution in C++


b) Error in both C and C++
c) Error in C++ and successful execution in C
d) A successful run in both C and C++
View Answer
Answer: c
Explanation: new is a keyword in C++, therefore, we cannot declare a variable with
name new but as there is no such keyword new in C, therefore, the program is
compiled and executed successfully in C.

14. What happens if the following program is executed in C and C++?

#include <stdio.h>
void func(void)
{
printf("Hello");
}
void main()
{
func();
func(2);
}

a) Outputs Hello twice in both C and C++


b) Error in C and successful execution in C++
c) Error in C++ and successful execution in C
d) Error in both C and C++
View Answer
Answer: d
Explanation: As the func(void) needs no argument during its call, hence when we are
calling func(2) with 2 as passed as a parameter then this statement gives the error in
both C++ and C compiler.

15. Which of the following is correct about this pointer in C++?


a) this pointer is passed as a hidden argument in all static variables of a class
b) this pointer is passed as a hidden argument in all the functions of a class
c) this pointer is passed as a hidden argument in all non-static functions of a class
d) this pointer is passed as a hidden argument in all static functions of a class
View Answer
Answer: c
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.

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

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.

18. Which of the following type is provided by C++ but not C?


a) double
b) float
c) int
d) bool
View Answer
Answer: d
Explanation: C++ provides the boolean type to handle true and false values whereas
no such type is provided in C.
19. What is the value of p in the following C++ code snippet?

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.

21. What will be the output of the following C++ function?

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.

22. Which of the following correctly declares an array in C++?


a) array{10};
b) array array[10];
c) int array;
d) int array[10];
View Answer
Answer: d
Explanation: Because array variable and values need to be declared after the
datatype only.

23. What is the size of wchar_t in C++?


a) Based on the number of bits in the system
b) 2 or 4
c) 4
d) 2
View Answer
Answer: a
Explanation: Compiler wants to make CPU as more efficient in accessing the next
value.

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

#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.

25. What is the use of the indentation in c++?


a) r distinguishes between comments and inner data
b) distinguishes between comments and outer data
c) distinguishes between comments and code
d) r distinguishes between comments and outer data
View Answer
Answer: c
Explanation: To distinguish between different parts of the program like comments,
codes, etc.

26. Which is more effective while calling the C++ functions?


a) call by object
b) call by pointer
c) call by value
d) call by reference
View Answer
Answer: d
Explanation: In the call by reference, it will just passes the reference of the memory
addresses of passed values rather than copying the value to new memories which
reduces the overall time and memory use.

27. What will be the output of the following C++ program?

#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++.

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

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.

30. What will be the output of the following C++ program?

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) compile time error


b) 0.100001
c) 0.11
d) 0.10000000000000001
View Answer
Answer: d
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

31. Which keyword is used to define the macros in c++?


a) #macro
b) #define
c) macro
d) define
View Answer
Answer: b
Explanation: #define is the keyword that is used to define the macros in c++.

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.

33. The C++ code which causes abnormal termination/behaviour of a program


should be written under _________ block.
a) catch
b) throw
c) try
d) finally
View Answer
Answer: c
Explanation: Code that leads to the abnormal termination of the program should be
written under the try block.

34. What is Inheritance in C++?


a) Deriving new classes from existing classes
b) Overloading of classes
c) Classes with same names
d) Wrapping of data into a single class
View Answer
Answer: a
Explanation: Inheritance is the concept of OOPs in which new classes are derived
from existing classes in order to reuse the properties of classes defined earlier.

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

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.

37. What will be the output of the following C++ program?

#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.

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

#include <iostream>
using namespace std;
void square (int *x, int *y)
{
*x = (*x) * --(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}
a) 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.

39. What is meant by a polymorphism in C++?


a) class having only single form
b) class having four forms
c) class having many forms
d) class having two forms
View Answer
Answer: c
Explanation: Polymorphism is literally meant class having many forms.

40. What will be the output of the following C++ program?

#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.

41. Pick the incorrect statement about inline functions in C++?


a) Saves overhead of a return call from a function
b) They are generally very large and complicated function
c) These functions are inserted/substituted at the point of call
d) They reduce function call overheads
View Answer
Answer: b
Explanation: Inline are functions that are expanded when it is called. The whole
code of the inline function gets inserted/substituted at the point of call. In this, they
help in reducing the function call overheads. Also they save overhead of a return call
from a function. Inline functions are generally kept small.

42. What will be the output of the following C++ program?

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

43. What is abstract class in C++?


a) Any Class in C++ is an abstract class
b) Class from which any class is derived
c) Class specifically used as a base class with atleast one virtual functions
d) Class specifically used as a base class with atleast one pure virtual functions
View Answer
Answer: d
Explanation: An abstract class is defined as a class which is specifically used as a
base class. An abstract class should have atleast one pure virtual function.

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.

45. What will be the output of the following C++ program?

#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

48. How structures and classes in C++ differ?


a) Structures by default hide every member whereas classes do not
b) In Structures, members are public by default whereas, in Classes, they are private
by default
c) Structures cannot have private members whereas classes can have
d) In Structures, members are private by default whereas, in Classes, they are public
by default
View Answer
Answer: b
Explanation: Structure members are public by default whereas, class members are
private by default. Both of them can have private and public members.

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

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.

52. What will be the output of the following C++ program?

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.

53. What will be the output of the following C++ program?

#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”.

2. Which of the following is a correct identifier in C++?


a) 7var_name
b) 7VARNAME
c) VAR_1234
d) $var_name
View Answer
Answer: c
Explanation: The rules for writing an identifier is as follows:
i) may contain lowercase/uppercase letters, digits or underscore(_) only
ii) should start with a non-digit character
iii) should not contain any special characters like @, $, etc.

3. Which of the following is called address operator?


a) *
b) &
c) _
d) %
View Answer
Answer: b
Explanation: & operator is called address operator and is used to access the address
of a variable.
advertisement
4. Which of the following is used for comments in C++?
a) // comment
b) /* comment */
c) both // comment or /* comment */
d) // comment */
View Answer
Answer: c
Explanation: Both the ways are used for commenting in C++ programming. // is
used for single line comments and /* … */ is used for multiple line comments.
5. What are the actual parameters in C++?
a) Parameters with which functions are called
b) Parameters which are used in the definition of a function
c) Variables other than passed parameters in a function
d) Variables that are never used in the function
View Answer
Answer: a
Explanation: Actual parameters are those using which a function call is made i.e.
which are actually passed in a function when that function is called.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

6. What are the formal parameters in C++?


a) Parameters with which functions are called
b) Parameters which are used in the definition of the function
c) Variables other than passed parameters in a function
d) Variables that are never used in the function
View Answer
Answer: b
Explanation: Formal parameters are those which are used in the definition of a
function. They are the parameters that represent the actual parameters passed and
they are the one which is used inside the function.

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.

8. Which function is used to write a single character to console in C++?


a) cout.put(ch)
b) cout.putline(ch)
c) write(ch)
d) printf(ch)
View Answer
Answer: a
Explanation: C++ provides cout.put() function to write a single character to console
whereas others are used to write either a single or multiple characters.

9. What are the escape sequences?


a) Set of characters that convey special meaning in a program
b) Set of characters that whose use are avoided in C++ programs
c) Set of characters that are used in the name of the main function of the program
d) Set of characters that are avoided in cout statements
View Answer
Answer: a
Explanation: Escape sequence is a set of characters that convey a special meaning
to the program. They are used to convey a meaning which cannot be conveyed
directly.

10. Which of the following escape sequence represents carriage return?


a) \r
b) \n
c) \n\r
d) \c
View Answer
Answer: a
Explanation: \r is used to represent carriage return which means move the cursor to
the beginning of the next line.

11. Which of the following escape sequence represents tab?


a) \t
b) \t\r
c) \b
d) \a
View Answer
Answer: a
Explanation: \t is used to represent tab which means a set of blank spaces in a line.

12. Who created C++?


a) Bjarne Stroustrup
b) Dennis Ritchie
c) Ken Thompson
d) Brian Kernighan
View Answer
Answer: a
Explanation: Bjarne Stroustrup is the original creator of C++ during 1979 at AT&T
Bell Labs.

13. Which of the following is called insertion/put to operator?


a) <<
b) >>
c) >
d) <
View Answer
Answer: a
Explanation: << operator is called insertion or put to operator i.e. insert/put things
to console/files.

14. Which of the following is called extraction/get from operator?


a) <<
b) >>
c) >
d) <
View Answer
15. A language which has the capability to generate new data types are called
________________
a) Extensible
b) Overloaded
c) Encapsulated
d) Reprehensible
View Answer
Answer: a
Explanation: Languages that can produce/generate new data types are called
extensible languages as they have the ability to handle new data types.

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.

2. How structures and classes in C++ differ?


a) In Structures, members are public by default whereas, in Classes, they are private
by default
b) In Structures, members are private by default whereas, in Classes, they are public
by default
c) Structures by default hide every member whereas classes do not
d) Structures cannot have private members whereas classes can have
View Answer
Answer: a
Explanation: Structure members are public by default whereas, class members are
private by default. Both of them can have private and public members.

3. What does polymorphism in OOPs mean?


a) Concept of allowing overiding of functions
b) Concept of hiding data
c) Concept of keeping things in differnt modules/files
d) Concept of wrapping things into a single unit
View Answer
Answer: a
Explanation: In OOPs, Polymorphism is the concept of allowing a user to override
functions either by changing the types or number of parameters passed.
advertisement
4. Which concept allows you to reuse the written code?
a) Encapsulation
b) Abstraction
c) Inheritance
d) Polymorphism
View Answer
Answer: c
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.

5. Which of the following explains Polymorphism?


a)

Note: Join free Sanfoundry classes at Telegram or Youtube

int func(int, int);


float func1(float, float);

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.

6. Which of the following shows multiple inheritances?


a) A->B->C
b) A->B; A->C
c) A,B->C
d) B->A
View Answer
Answer: c
Explanation: In multiple inheritance, a single class is inherited from two classes. So
in A,B->C, Class C is inherited from A and B, whereas in A->B->C, C from B and B
from A called simple inheritance, in A->B; A->C, B and C are inherited from A which
is called hierarchical inheritance.

7. How access specifiers in Class helps in Abstraction?


a) They does not helps in any way
b) They allows us to show only required things to outer world
c) They help in keeping things together
d) Abstraction concept is not used in classes
View Answer
Answer: b
Explanation: Abstraction is the concept of hiding things from the outer world and
showing only the required things to the world, which is where access specifiers
private, protected and public helps in keeping our knowledge hidden from the
world.

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).

9. What does modularity mean?


a) Hiding part of program
b) Subdividing program into small independent parts
c) Overriding parts of program
d) Wrapping things into single unit
View Answer
Answer: b
Explanation: Modularity means dividing a program into independent sub programs
so that it can be invoked from other parts of the same program or any other
program.

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.

2. Which of the following is not a type of Constructor?


a) Friend constructor
b) Copy constructor
c) Default constructor
d) Parameterized constructor
View Answer
Answer: a
Explanation: Friend function is not a constructor whereas others are a type of
constructor used for object initialization.

3. Which of the following is correct?


a) Base class pointer object cannot point to a derived class object
b) Derived class pointer object cannot point to a base class object
c) A derived class cannot have pointer objects
d) A base class cannot have pointer objects
View Answer
Answer: b
Explanation: C++ does not allow a derived class pointer to point a base class pointer
whereas Base class can point to a derived class object. Both base class and derived
class can have pointer objects.
advertisement
4. Out of the following, which is not a member of the class?
a) Static function
b) Friend function
c) Constant function
d) Virtual function
View Answer
Answer: b
Explanation: Friend function is not a member of the class. They are given the same
access rights as the class member function have but they are not actual members of
the class.

5. What is the other name used for functions inside a class?


a) Member variables
b) Member functions
c) Class functions
d) Class variables
View Answer
Answer: b
Explanation: Functions of a class are also known as member functions of a class.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

6. Which of the following cannot be a friend?


a) Function
b) Class
c) Object
d) Operator function
View Answer
Answer: c
Explanation: Objects of any class cannot be made a friend of any other or same
class whereas functions, classes and operator functions can be made a friend.

7. Why references are different from pointers?


a) A reference cannot be made null
b) A reference cannot be changed once initialized
c) No extra operator is needed for dereferencing of a reference
d) All of the mentioned
View Answer
Answer: d
Explanation: References cannot be made null whereas a pointer can be. References
cannot be changed whereas pointers can be modified.
Pointers need * operator to dereference the value present inside it whereas
reference does not need an operator for dereferencing.

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.

9. How many types of polymorphism are there in C++?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: There are two types of polymorphism in C++ namely run-time and
compile-time polymorphisms.

10. How run-time polymorphisms are implemented in C++?


a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions
View Answer
Answer: d
Explanation: Run-time polymorphism is implemented using Inheritance and virtual
in which object decides which function to call.

11. How compile-time polymorphisms are implemented in C++?


a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions
View Answer
Answer: c
Explanation: Compile-time polymorphism is implemented using templates in which
the types(which can be checked during compile-time) are used decides which
function to be called.

12. Which of the following is an abstract data type?


a) int
b) float
c) class
d) string
View Answer
Answer: c
Explanation: Class is used as an abstract data type as it can be used to give
implementation independent view whereas no other data type can be used to
provide this.

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.

14. Which of the following explains the overloading of functions?


a) Virtual polymorphism
b) Transient polymorphism
c) Ad-hoc polymorphism
d) Pseudo polymorphism
View Answer
Answer: c
Explanation: Ad-hoc polymorphism is a type of polymorphism in which a function
denotes heterogeneous implementation depending upon the types of argument.
15. Which of the following approach is used by C++?
a) Top-down
b) Bottom-up
c) Left-right
d) Right-left
View Answer
Answer: b
Explanation: C++ is an object-oriented language and OOL uses a bottom-up
approach to solve/view a problem.

1. Which operator is overloaded for a cout object?


a) >>
b) <<
c) <
d) >
View Answer
Answer: b
Explanation: cout in C++ uses << operator to print anything so << operator is
overloaded for a cout object.

2. Which of the following cannot be used with the virtual keyword?


a) Class
b) Member functions
c) Constructors
d) Destructors
View Answer
Answer: c
Explanation: Virtual keyword cannot be used with constructors as constructors are
defined to initialized an object of particular class hence no other class needs
constructor of other class.

3. Which concept is used to implement late binding?


a) Virtual functions
b) Operator functions
c) Constant functions
d) Static functions
View Answer
Answer: a
Explanation: Virtual functions are used to implement the concept of late binding i.e.
binding actual functions to their calls.
advertisement
4. Which of the following is correct?
a) C++ allows static type checking
b) C++ allows dynamic type checking.
c) C++ allows static member function to be of type const.
d) C++ allows both static and dynamic type checking
View Answer
Answer: d
Explanation: C++ allows both static and dynamic type checking i.e. types are
checked by the compiler.

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.

Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of the following is a static polymorphism mechanism?


a) Function overloading
b) Operator overloading
c) Templates
d) All of the mentioned
View Answer
Answer: d
Explanation: All the options mentioned above uses static polymorphism
mechanism. As the conflicts in all these types of functions are resolved during
compile-time.

7. Which of the following is true?


I) All operators in C++ can be overloaded.
II) The basic meaning of an operator can be changed.
a) I only
b) II only
c) Both I and II
d) Neither I nor II
View Answer
Answer: d
Explanation: Both statements are false because all the operators of C++ cannot be
overloaded and the basic meaning of an operator cannot be changed, we can only
give new meaning to an operator.
8. Which of the following is not a type of inheritance?
a) Multiple
b) Multilevel
c) Distributive
d) Hierarchical
View Answer
Answer: c
Explanation: Distributive is not a type of inheritance whereas others are a type of
inheritance having their own meaning.

9. What happens if a class does not have a name?


a) It will not have a constructor
b) It will not have a destructor
c) It is not allowed
d) It will neither have a constructor or destructor
View Answer
Answer: b
Explanation: A class without a name will not have a destructor. The object is made
so constructor is required but the destructor is not. Check the code below:
#include <iostream>
using namespace std;
class
{
public:
void func()
{
cout<<"Hello world";
}
}a;
int main(int argc, char const *argv[])
{
a.func();
return 0;
}

10. Which of the following statement is true?


I) In Procedural programming languages, all function calls are resolved at compile-
time
II) In Object Oriented programming languages, all function calls are resolved at
compile-time
a) I only
b) II only
c) Both I and II
d) Neither I nor II
View Answer
Answer: a
Explanation: In Procedural programming like C we don’t have the concept of
polymorphism, therefore, all the function calls are resolved at the compile-time but
in case of OOP languages sue to polymorphism concept all function calls are not
resolved at compile-time.

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.

12. Which of the following is correct?


a) Friend functions can access public members of a class
b) Friend functions can access protected members of a class
c) Friend functions can access private members of a class
d) All of the mentioned
View Answer
Answer: d
Explanation: Friend functions can access any member of a class without caring
about the type of member i.e. without caring whether it is private, protected or
public.

13. Which of the following is correct in C++?


a) Classes cannot have protected data members
b) Structures can have member functions
c) Class members are public by default
d) Structure members are private by default
View Answer
Answer: b
Explanation: Though C does not allows member functions in structures but C++
allows structures to have member functions. Members of structures are public by
default and those of classes are private by default. Classes can have protected data
members.

14. Which of the following is used to make an abstract class?


a) By using virtual keyword in front of a class declaration
b) By using an abstract keyword in front of a class declaration
c) By declaring a virtual function in a class
d) By declaring a pure virtual function in a class
View Answer
Answer: d
Explanation: Abstract class should have at least one pure virtual function. Therefore
to declare an abstract class one should declare a pure virtual function in a class.

15. Which of the following is correct?


a) A class is an instance of its objects
b) An object is an instance of its class
c) A class is an instance of the data type that the class have
d) An object is an instance of the data type of the class
View Answer
Answer: b
Explanation: An object is an instance of a class i.e. an object represents a class i.e.
what class has(data members) and what it can do(member functions).

1. Which of the following is correct about new and malloc?


a) Both are available in C
b) Pointer object initialization of a class with both new and malloc calls the
constructor of that class
c) Pointer object initialization of a class using new involves constructor call whereas
using malloc does not involve constructor call
d) Pointer object initialization of a class using malloc involves constructor call
whereas using new does not involve constructor call
View Answer
Answer: c
Explanation: Object initialization using new keyword involves constructor call
whereas malloc does not involve constructor call. That’s why new is explicitly added
in C++. Also, malloc is used to assign memory to any pointer hence it assigns
memory equals to the size of the class however new keyword involves initialization
also hence calls the constructor of that class.

2. What is virtual inheritance?


a) C++ technique to avoid multiple copies of the base class into children/derived
class
b) C++ technique to avoid multiple inheritances of classes
c) C++ technique to enhance multiple inheritance
d) C++ technique to ensure that a private member of the base class can be accessed
somehow
View Answer
Answer: a
Explanation: Virtual inheritance is a C++ technique with which it ensures that a
derived class contains only one copy of the base class’s variables. Refer Wikipedia
for more info.

3. What is the difference between delete and delete[] in C++?


a) delete is used to delete normal objects whereas delete[] is used to pointer objects
b) delete is a keyword whereas delete[] is an identifier
c) delete is used to delete single object whereas delete[] is used to
multiple(array/pointer of) objects
d) delete is syntactically correct but delete[] is wrong and hence will give an error if
used in any case
View Answer
Answer: c
Explanation: delete is used to delete a single object initiated using new keyword
whereas delete[] is used to delete a group of objects initiated with the new
operator.
advertisement
4. What will be the output of the following C++ code?

#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.

Note: Join free Sanfoundry classes at Telegram or Youtube

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

#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.

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

#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.

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

#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.

8. What is the correct syntax of declaring array of pointers of integers of size 10 in


C++?
a) int arr = new int[10];
b) int **arr = new int*[10];
c) int *arr = new int[10];
d) int *arr = new int*[10];
View Answer
Answer: b
Explanation: As we have to declare an array of pointers of integers therefore we
need double pointer array in which each element is collection pointers to integers.
Therefore the correct syntax is int **arr = new int*[10];

9. Which of the following is correct about new and malloc?


i) new is an operator whereas malloc is a function
ii) new calls constructor malloc does not
iii) new returns required pointer whereas malloc returns void pointer and needs to
be typecast
a) i and ii
b) ii and iii
c) i and iii
d) i, ii and iii
View Answer
Answer: d
Explanation: All the statements about the new and malloc are correct. new is an
operator whereas malloc() is a function. The constructor is called when new is used
and new returns required type memory pointer.

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

#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?

int *ptr = NULL;


delete ptr;

a) The program compiled successfully but throws an error during run-time


b) The program gives a compile-time error
c) The program is not semantically correct
d) The program is compiled and executed successfully
View Answer
Answer: d
Explanation: The above statement is syntactically and semantically correct as C++
allows the programmer to delete a NULL pointer, therefore, the program is
compiled and executed successfully.

12. What happens if a pointer is deleted twice in a program as shown in the


following C++ statements?

int *ptr = new int;


delete ptr;
delete ptr;

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.

1. What happens if the following program is executed in C and C++?

#include<stdio.h>
int main()
{
foo();
}
int foo()
{
printf("Hello");
return 0;
}

a) Error in both C and C++


b) Warning in both C and C++
c) Error in C++ but Warning in C
d) Error in C but Warning in C++
View Answer
Answer: c
Explanation: In C++ all the functions should be declared before it is called otherwise
the C++ compiler will give an error but in case of C the compiler just gives a warning
and the program can be executed.
advertisement
2. What happens if the following program is executed in C and C++?

#include <stdio.h>
int main(void)
{
const int j = 20;
int *ptr = &j;
printf("*ptr: %d\n", *ptr);
return 0;
}

a) Error in both C and C++


b) Warning in both C and C++
c) Error in C but Warning in C++
d) Error in C++ but Warning in C
View Answer
Answer: d
Explanation: C++ is strict on the use of types of variables hence when the
programmer tries to assign const int to a normal pointer the program gives error
whereas C is not strict on types therefore it gives warning only.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

3. What happens if the following line is executed in C and C++?

int *p = malloc(10);

a) Error in both C and C++


b) Warning in both C and C++
c) Error in C++ and successful execution in C
d) Error in C and successful execution in C++
View Answer
Answer: c
Explanation: C++ is strict in type check but C is not and as malloc returns a void*
which we are trying to assign to an int*, therefore, the C++ compiler gives error
whereas C compiler executes the program successfully.

4. What happens if the following line is executed in C and C++?

const int a;

a) Error in both C and C++


b) Warning in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
View Answer
Answer: d
Explanation: C++ compiler does not allow the programmer to declare a constant
variable without initializing it hence the C++ compiler gives an error whereas C
allows such declaration, therefore, the program compiles and runs successfully.

5. What happens if the following program is executed in C and C++?

#include <stdio.h>
int main(void)
{
int new = 5;
printf("%d", new);
}

a) Error in both C and C++


b) A successful run in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
View Answer
Answer: d
Explanation: new is a keyword in C++, therefore, we cannot declare a variable with
name new but as there is no such keyword new in C, therefore, the program is
compiled and executed successfully in C.

6. What happens if the following program is executed in C and C++?

#include <stdio.h>
void main()
{
printf("Hello World");
}

a) Error in both C and C++


b) Successful run in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
View Answer
Answer: d
Explanation: main() function in C++ must return int otherwise the C++ compiler gives
the error whereas C does not forces such things on main() function. Thereas when
we aremaking void main(){} function in this program the C++ compiler gives error
whereas C compiler runs successfully.

7. What happens if the following program is executed in C and C++?

#include <stdio.h>
void func(void)
{
printf("Hello");
}
void main()
{
func();
func(2);
}

a) Error in both C and C++


b) Outputs Hello twice in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
View Answer
Answer: a
Explanation: As the func(void) needs no argument during its call, hence when we are
calling func(2) with 2 as passed as a parameter then this statement gives the error in
both C++ and C compiler.

8. What happens if the following program is executed in C and C++?

#include <stdio.h>
void func()
{
printf("Hello");
}
void main()
{
func();
func(2);
}

a) Error in both C and C++


b) Outputs Hello twice in both C and C++
c) Error in C and Outputs Hello twice in C++
d) Error in C++ and Outputs Hello twice in C
View Answer
Answer: d
Explanation: In C++ whenever a function without argument is declared it is
equivalent to function with void arguments i.e. func() == func(void) whereas in C a
function without argument is equivalent to func(…) i.e. it can take any number of
arguments so func(2) call is also valid in C but not valid in C++. Hence it gives error in
C++ whereas no error in C.

9. Which of the following type is provided by C++ but not C?


a) int
b) bool
c) float
d) double
View Answer
Answer: b
Explanation: C++ provides the boolean type to handle true and false values whereas
no such type is provided in C.

10. Which of the following feature is not provided by C?


a) Pointers
b) Structures
c) References
d) Functions
View Answer
Answer: c
Explanation: References are introduced in C++. They are not present in C.

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).

3. Which of the following is C++ equivalent for scanf()?


a) cin
b) cout
c) print
d) input
View Answer
Answer: a
Explanation: C++ uses cin to read input form uses. However C++ also uses scanf().
advertisement
4. Which of the following is C++ equivalent for printf()?
a) cin
b) cout
c) print
d) input
View Answer
Answer: b
Explanation: C++ uses cout to print output to console. However C++ also uses
printf().

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.

Subscribe Now: C++ Newsletter | Important Subjects Newsletters

6. Which of the following is an exit-controlled loop?


a) for
b) while
c) do-while
d) all of the mentioned
View Answer
Answer: c
Explanation: do-while is called exit controlled loop because in do-while termination
condition is checked when we have executed the body of the loop i.e. we are exiting
the body and then checking the condition, therefore, it is called exit controlled loop.

7. Which of the following is an entry-controlled loop?


a) for
b) while
c) do-while
d) both while and for
View Answer
Answer: d
Explanation: Both while and for loops are called entry controlled loop because in
both of them the termination condition is checked before we enter the body of the
loop hence they are called entry controlled loop.

8. In which part of the for loop termination condition is checked?


for(I;II;III)
{IV}
a) I
b) II
c) III
d) IV
View Answer
Answer: b
Explanation: In II part the termination condition of the for loop is checked.

9. What is dynamic binding?


a) The process of linking the actual code with a procedural call during run-time
b) The process of linking the actual code with a procedural call during compile-time
c) The process of linking the actual code with a procedural call at any-time
d) All of the mentioned
View Answer
Answer: a
Explanation: Binding of calls and variables with actual code at run-time is called
dynamic binding. For example in the concept of polymorphism types are decided
are defined during the execution of code which leads to the different function calls
depending upon the types used this is called dynamic binding. As the function call is
decided during the run-time therefore dynamic binding happens at run-time.

10. What is static binding?


a) The process of linking the actual code with a procedural call during run-time
b) The process of linking the actual code with a procedural call during compile-time
c) The process of linking the actual code with a procedural call at any-time
d) All of the mentioned
View Answer
Answer: b
Explanation: Binding of calls and variables with actual code at compile-time is called
static binding. For example normally whenever we declare a variable we define its
type hence compiler knows what type should be binded to that variable i.e.
compiler can decide about that variable this is called static binding.

11. What is name mangling in C++?


a) The process of adding more information to a function name so that it can be
distinguished from other functions by the compiler
b) The process of making common names for all the function of C++ program for
better use
c) The process of changing the names of variable
d) The process of declaring variables of different types
View Answer
Answer: a
Explanation: Name mangling is the process of adding some more information to a
function name so that it can be distinguished from other functions by the compiler.
This is used when a programmer uses the concept of function overloading in his/her
program.

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

a) Output in C is 1 and in C++ is 4


b) Output in C is 4 and in C++ is 1
c) Output in C is 1 and in C++ is 1
d) Output in C is 4 and in C++ is 4
View Answer
Answer: b
Explanation: In C a character is stored as int therefore the size of ‘a’ is printed as 4
whereas in C++ it is stored as char only therefore in C++ it prints 1.

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

#include<stdio.h>
int main(int argc, char const *argv[])
{
char a = 'a';
printf("%d\n", (int)sizeof(a));
return 0;
}

a) Output in C is 1 and in C++ is 4


b) Output in C is 4 and in C++ is 1
c) Output in C is 1 and in C++ is 1
d) Output in C is 4 and in C++ is 4
View Answer
14. Which of the following syntax for declaring a variable of struct STRUCT can be
used in both C and C++?
a) struct STRUCT S;
b) STRUCT S;
c) Both struct STRUCT S; and STRUCT S;
d) Both C and C++ have different syntax
View Answer
Answer: a
Explanation: C program requires struct keyword while defining a variable of any
structure, therefore, we cannot use the second STRUCT S; definition to declare a
variable.

15. What if we define the below structure in C and C++?


a) Error in C but not in C++
b) Error in C++ but not in C
c) No error in both C and C++
d) Error in both C and C++
View Answer
Answer: a
Explanation: The above definition will give an error in C but not in C++ as C does not
allows the programmer to give any default values to any member of structure but
C++ does allow.

1. Which of the following is the scope resolution operator?


a) .
b) *
c) ::
d) ~
View Answer
Answer: c
Explanation: :: operator is called scope resolution operator used for accessing a
global variable from a function which is having the same name as the variable
declared in the function.

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

#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?

Note: Join free Sanfoundry classes at Telegram or Youtube

#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.

4. 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 *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.

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

#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin: " << cin;
return 0;
}

a) cin: garbage value


b) Error
c) Segmentation fault
d) Nothing is printed
View Answer
Answer: a
Explanation: cin is a variable hence overrides the cin object. cin >> cin has no
meaning so no error.

7. Which of the following operator has left to right associativity?


a) Unary operator
b) Logical not
c) Array element access
d) addressof
View Answer
Answer: c
Explanation: Array element has left to right associativity i.e. expressions are
evaluated from left to right in case of array element access.

8. Which of the following is accessed by a member function of a class?


a) The object of that class
b) All members of a class
c) The public part of a class
d) The private part of a class
View Answer
Answer: b
Explanation: A member function of a class can access all the members of its class
whether they are private, protected or public.

9. What is the size of a character literal in C and C++?


a) 4 and 1
b) 1 and 4
c) 1 and 1
d) 4 and 4
View Answer
Answer: a
Explanation: The size of a character literal is 4 in case of C but it is one in case of
C++. You can do printf(“%d”, (int)sizeof(‘a’)); in both C and C++ to check this.

10. What is the size of a character type in C and C++?


a) 4 and 1
b) 1 and 4
c) 1 and 1
d) 4 and 4
View Answer
Answer: c
Explanation: The size of a character type in both C and C++ is 1. You can do
printf(“%d”, (int)sizeof(char)); in both C and C++ to check this.

11. Which of the following is correct?


a) struct tag is required in both C and C++ while declaring an object of the structure
b) struct is not required in C but required in C++ while declaring an object of the
structure
c) struct is not required in C++ but required in C while declaring an object of the
structure
d) struct tag is not required in both C and C++ while declaring an object of the
structure
View Answer
Answer: c
Explanation: C++ does not require struct keyword while declaring an object of the
structure whereas in C we require struct tag for declaring an object.

12. Which of the following is correct?


a) struct cannot have member function in C but it can in C++
b) struct cannot have member function in C++ but it can in C
c) struct cannot have member function in both C and C++
d) struct can have member function in both C and C++
View Answer
Answer: a
Explanation: struct can have member function in C++ whereas member functions
are not allowed in case of C.
13. What happens if we run the following code in both C and C++?

#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.

1. Which of the following statement is correct?


a) Structure in C allows Constructor definition
b) Structure in C++ allows Constructor definition
c) Both allow Constructor definition
d) C allows constructor definition while C++ does not
View Answer
Answer: b
Explanation: As C does not allow the programmer to define a function inside a
structure and constructor itself is a function, therefore, the constructor definition is
not allowed in C whereas such definitions are allowed in C++.

2. What happens if the following code is compiled on both C and C++?

#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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

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.

5. Why this pointer is used?


a) To access the members of a class which have the same name as local variables in
that scope
b) To access all the data stored under that class
c) To access objects of other class
d) To access objects of other variables
View Answer
Answer: a
Explanation: this pointer is used to access the members of a class which have the
same name as local variables in that part of the code.

6. How many types of polymorphism are there?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: There are two types of polymorphism in C++ namely compile-time
polymorphism and run-time polymorphism.

7. What is the other name of compile-time polymorphism?


a) Static polymorphism
b) Dynamic polymorphism
c) Executing polymorphism
d) Non-executing polymorphism
View Answer
Answer: a
Explanation: Compile-time polymorphism is also known as static polymorphism as it
is implemented during the compile-time.

8. What is the other name of run-time polymorphism?


a) Static polymorphism
b) Dynamic polymorphism
c) Executing polymorphism
d) Non-executing polymorphism
View Answer
Answer: b
Explanation: Run-time polymorphism is also known as dynamic polymorphism as it
is implemented during the run-time of the program.
9. Which of the following is correct about static polymorphism?
a) In static polymorphism, the conflict between the function call is resolved during
the compile time
b) In static polymorphism, the conflict between the function call is resolved during
the run time
c) In static polymorphism, the conflict between the function call is never resolved
during the execution of a program
d) In static polymorphism, the conflict between the function call is resolved only if it
required
View Answer
Answer: a
Explanation: The conflict between which function to call is resolved during the
compile time in static polymorphism i.e. before the execution of the program starts.

10. Which of the following is correct about dynamic polymorphism?


a) In dynamic polymorphism, the conflict between the function call is resolved
during the compile time
b) In dynamic polymorphism, the conflict between the function call is resolved
during the run time
c) In dynamic polymorphism, the conflict between the function call is never resolved
during the execution of the program
d) In dynamic polymorphism, the conflict between the function call is resolved at the
beginning of the program
View Answer
Answer: b
Explanation: The conflict between which function to call is resolved during the run
time in dynamic polymorphism i.e. the conflict is resolved when the execution
reaches the function call statement.

11. Which of the following operator(s) can be used with pointers?


i) – only
ii) +, *
iii) +, –
iv) +, -, *
v) /
vi) + only
a) i only
b) vi only
c) ii and v
d) iv
View Answer
Answer: a
Explanation: The only arithmetic operator that can be used with a pointer is –
subtraction operator. No arithmetic operator can be used with pointers.
12. What is std in C++?
a) std is a standard class in C++
b) std is a standard namespace in C++
c) std is a standard header file in C++
d) std is a standard file reading header in C++
View Answer
Answer: b
Explanation: std is a standard namespace present in C++ which contains different
stream classes and objects like cin, cout, etc. and other standard functions.

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

#include <iostream>

int main(int argc, char const *argv[])


{
cout<<"Hello World";
return 0;
}

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

a) Both code 1 and code 2


b) Code 1 only
c) Code 2 only
d) Neither code 1 nor code 2
View Answer
Answer: d
Explanation: Neither code 1 nor code2 will give 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. What will be the output of the following C++ code?

#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!

3. Which of the following is correct about static variables?


a) Static functions do not support polymorphism
b) Static data members cannot be accessed by non-static member functions
c) Static data members functions can access only static data members
d) Static data members functions can access both static and non-static data
members
View Answer
Answer: c
Explanation: Static member functions can access static data members only. Static
member functions can be overloaded. Static data members can be accessed by non-
static member functions.

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

#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.

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

#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.

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

#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.

7. Const qualifier can be applied to which of the following?


i) Functions inside a class
ii) Arguments of a function
iii) Static data members
iv) Reference variables
a) i, ii and iii
b) i, ii, iii, and iv
c) ii, iii and iv
d) i only
View Answer
Answer: b
Explanation: const keyword can be applied to all of the following mentioned above.

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

#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.

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

#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.

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


#include <iostream>
int const s=9;
int main()
{
std::cout << s;
return 0;
}

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.

1. What is the size of wchar_t in C++?


a) 2
b) 4
c) 2 or 4
d) Based on the number of bits in the system
View Answer
Answer: d
Explanation: Compiler wants to make CPU as more efficient in accessing the next
value.

2. Pick the odd one out.


a) array type
b) character type
c) boolean type
d) integer type
View Answer
Answer: a
Explanation: Array type is not the basic type and it is constructed using the basic
type.

3. Which data type is used to represent the absence of parameters?


a) int
b) short
c) void
d) float
View Answer
Answer: c
Explanation: Because void specifies an empty set of values/parameters.
advertisement
4. What does ‘\a’ escape code represent?
a) alert
b) backslash
c) tab
d) form feed
View Answer
Answer: a
Explanation: Because \a is used to produce a beep sound.

5. Which type is best suited to represent the logical values?


a) integer
b) boolean
c) character
d) float
View Answer
Answer: b
Explanation: Logical values can be either true or false, so the boolean type is suited
for it.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

6. Identify the user-defined types from the following?


a) enumeration
b) classes
c) both enumeration and classes
d) int
View Answer
Answer: c
Explanation: They must be defined by the users before use, unlike the other types
which are readily available.

7. Which of the following statements are true?

int f(float)

a) f is a function taking an argument of type int and returning a floating point


number
b) f is a function taking an argument of type float and returning an integer
c) f is a function of type float
d) f is a function of type int
View Answer
Answer: b
Explanation: The argument that is passed to a function f is of float type and the
function finally returns a value that id is of integer type.
8. The value 132.54 can be represented using which data type?
a) double
b) void
c) int
d) bool
View Answer
Answer: a
Explanation: The given value is with decimal points, so float or double can be used.

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++.

10. Pick the odd one out.


a) integer, character, boolean, floating
b) enumeration, classes
c) integer, enum, void
d) arrays, pointer, classes
View Answer
Answer: c
Explanation: integer, character, boolean & floating consists of all fundamental types,
enumeration & classes consists of user-defined types and arrays, pointer & classes
consists of derived types but integer, enum & void is a mixture.

1. Is bool a fundamental data type in C++?


a) Yes
b) No, it is a typedef of unsigned char
c) No, it is an enum of {false, true}
d) No, it is expanded from macros
View Answer
Answer: a
Explanation: C++ has bool as a fundamental data type.

2. Find the odd one out.


a) std::vector<int>
b) std::vector<short>
c) std::vector<long>
d) std::vector<bool>
View Answer
Answer: d
Explanation: std::vector<bool> is a specialized version of vector, which is used for
elements of type bool and optimizes for space. It behaves like the unspecialized
version of vector and the storage is not necessarily an array of bool values, but the
library implementation may optimize storage so that each value is stored in a single
bit.

3. What is the value of the bool?

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.

Note: Join free Sanfoundry classes at Telegram or Youtube

4. What happens when a null pointer is converted into bool?


a) an error is flagged
b) bool value evaluates to true
c) bool value evaluates to false
d) the statement is ignored
View Answer
Answer: c
Explanation: A pointer can be implicitly converted to a bool. A nonzero pointer
converts to true and zero valued pointer converts to false.

5. Which of the following statements are false?


a) bool can have two values and can be used to express logical expressions
b) bool cannot be used as the type of the result of the function
c) bool can be converted into integers implicitly
d) a bool value can be used in arithmetic expressions
View Answer
Answer: b
Explanation: Boolean can be used as a return value of a function.

6. For what values of the expression is an if-statement block not executed?


a) 0 and all negative values
b) 0 and -1
c) 0
d) 0, all negative values, all positive values except 1
View Answer
Answer: c
Explanation: The if-statement block is only not executed when the expression
evaluates to 0. its just syntactic sugar for a branch-if-zero instruction.

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.

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

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.

9. What is the value of p in the following C++ code?

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.

10. Evaluate the following.

(false && true) || false || true

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

a) 1 is true but 2 is false


b) 1 is false and 2 is true
c) both 1 and 2 are true
d) both 1 and 2 are false
View Answer
Answer: c
Explanation: Every character constant has an integer value. Also char belongs to the
integral type hence arithmetic and logical operations can be performed on them.
advertisement
3. Which of the following belongs to the set of character types?
a) char
b) wchar_t
c) only a
d) both wchar_t and char
View Answer
Answer: d
Explanation: wchar_t and char are used to represent wide character and character.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

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.

5. How do we represent a wide character of the form wchar_t?


a) L’a’
b) l’a’
c) L[a]
d) la
View Answer
Answer: a
Explanation: A wide character is always indicated by immediately preceding the
character literal by an L.

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

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.

7. In C++, what is the sign of character data type by default?


a) Signed
b) Unsigned
c) Implementation dependent
d) Unsigned Implementation
View Answer
Answer: c
Explanation: The standard does not specify if plain char is signed or unsigned. There
are three distinct character types according to the standard: char, signed char and
unsigned char.

8. Is the size of character literals different in C and C++?


a) Implementation defined
b) Can’t say
c) Yes, they are different
d) No, they are not different
View Answer
Answer: c
Explanation: In C++, sizeof(‘a’) == sizeof(char) == 1. In C however, sizeof(‘a’) ==
sizeof(int).

9. Suppose in a hypothetical machine, the size of char is 32 bits. What would


sizeof(char) return?
a) 4
b) 1
c) Implementation dependent
d) Machine dependent
View Answer
Answer: b
Explanation: The standard does NOT require a char to be 8-bits, but does require
that sizeof(char) return 1.

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. The size_t integer type in C++ is?


a) Unsigned integer of at least 64 bits
b) Signed integer of at least 16 bits
c) Unsigned integer of at least 16 bits
d) Signed integer of at least 64 bits
View Answer
Answer: c
Explanation: The size_t type is used to represent the size of an object. Hence, it’s
always unsigned. According to the language specification, it is at least 16 bits.

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

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

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.

6. Which of these expressions will isolate the rightmost set bit?


a) x = x & (~x)
b) x = x ^ (~x)
c) x = x & (-x)
d) x = x ^ (-x)
View Answer
Answer: c
Explanation: Negative of a number is stores as 2;s complement in C++, so when you
will take AND of x and (-x) the rightmost digit will be preserved.

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

a) ANDing integer ‘a’ with ‘true’ :8


b) ANDing integer ‘a’ with ‘true’ :0
c) ANDing integer ‘a’ with ‘true’ :1
d) ANDing integer ‘a’ with ‘true’ :9
View Answer
Answer: a
Explanation: The && operator in C++ uses short-circuit evaluation so that if bool1
evaluates to false it doesn’t bother evaluating bool2. So as here bool1 is 8 which is
true as it is non-zero so C++ does not cares about the expression further and prints
the answer of expression which is 8. If you write true && 8 then the output will be 1
because true is true and its integer equivalent is 1 so 1 will be printed.

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

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) compile time error


b) -1 1
c) 1 -1
d) implementation defined
View Answer
Answer: b
Explanation: Sign of result of mod operation on negative numbers is sign of the
dividend.

10. What will be the output of the following C++ function?


1. int main()
2. {
3. register int i = 1;
4. int *ptr = &i;
5. cout << *ptr;
6. return 0;
7. }

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.

2. Which of the following is a valid floating-point literal?


a) f287.333
b) F287.333
c) 287.e2
d) 287.3.e2
View Answer
Answer: c
Explanation: To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and
there should not be any blank space.

3. What is the range of the floating point numbers?


a) -3.4E+38 to +3.4E+38
b) -3.4E+38 to +3.4E+34
c) -3.4E+38 to +3.4E+36
d) -3.4E+38 to +3.4E+32
View Answer
Answer: a
Explanation: This is the defined range of floating type number sin C++. Also range
for +ve and -ve side should be same so the answer is -3.4E+38 to +3.4E+38.
advertisement
4. Which of three sizes of floating point types should be used when extended
precision is required?
a) float
b) double
c) long double
d) extended float
View Answer
Answer: c
Explanation: Float for single precision, double for double precision and long double
for extended precision.

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

Note: Join free Sanfoundry classes at Telegram or Youtube

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

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

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

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

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

8. Which is used to indicate single precision value?


a) F or f
b) L or l
c) Either F or for L or l
d) Neither F or for L or l
View Answer
Answer: a
Explanation: Either F or f can be used to indicate single precision values.

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

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.

1. The size of an object or a type can be determined using which operator?


a) malloc
b) sizeof
c) malloc
d) calloc
View Answer
Answer: b
Explanation: The sizeof operator gives the size of the object or type.

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.

3. Implementation dependent aspects about an implementation can be found in


____
a) <implementation>
b) <limits>
c) <limit>
d) <numeric>
View Answer
Answer: b
Explanation: The limit header holds the details of the machine dependent details.
advertisement
4. Size of C++ objects are expressed in terms of multiples of the size of a ____ and
the size of a char is _______
a) char, 1
b) int, 1
c) float, 8
d) char, 4
View Answer
Answer: a
Explanation: Each object in C++ is expressed in terms of char type and size of char
type is one byte.

5. Identify the incorrect option.


a) 1 <= sizeof(bool) <= sizeof(long)
b) sizeof(float) <= sizeof(double) <= sizeof(long double)
c) sizeof(char) <= sizeof(long) <=sizeof(wchar_t)
d) sizeof(N) = sizeof(signed N) = sizeof(unsigned N)
View Answer
Answer: c
Explanation: sizeof(char) <= sizeof(wchar_t) <= sizeof(long).

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

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

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

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

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

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

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

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

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.

1. Which of the following will not return a value?


a) null
b) void
c) empty
d) free
View Answer
Answer: b
Explanation: Because void represents an empty set of values so nothing will be
return.

2. ______________ have the return type void.


a) all functions
b) constructors
c) destructors
d) none of the mentioned
View Answer
Answer: d
Explanation: Constructor creates an Object and Destructor destroys the object. They
are not supposed to return anything, not even void.

3. What does the following statement mean?

advertisement
ADVERTISEMENT
ADVERTISEMENT

void a;

a) variable a is of type void


b) a is an object of type void
c) declares a variable with value a
d) flags an error
View Answer
Answer: d
Explanation: There are no void objects.

Note: Join free Sanfoundry classes at Telegram or Youtube

4. Choose the incorrect option.


a) void is used when the function does not return a value
b) void is also used when the value of a pointer is null
c) void is used as the base type for pointers to objects of unknown type
d) void is a special fundamental type
View Answer
Answer: b
Explanation: void fundamental type is used in the cases of a and c.

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

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. Identify the incorrect option.


a) enumerators are constants
b) enumerators are user-defined types
c) enumerators are same as macros
d) enumerator values start from 0 by default
View Answer
Answer: c
Explanation: Enumerators are used in order to create our own types whereas
macros are textual substitutions.

2. In which type do the enumerators are stored by the compiler?


a) string
b) integer
c) float
d) string & float
View Answer
Answer: b
Explanation: In C++, enumerations are stored as integers by the compiler starting
with 0.

3. To which of these enumerators can be assigned?


a) integer
b) negative
c) enumerator
d) all of the mentioned
View Answer
Answer: d
Explanation: Since enumerators evaluate to integers, and integers can be assigned
to enumerators, enumerators can be assigned to other enumerators.
advertisement
4. What will happen when defining the enumerated type?
a) it will not allocate memory
b) it will allocate memory
c) it will not allocate memory to its variables
d) allocate memory to objects
View Answer
Answer: a
Explanation: Enumerator will allocate the memory when its variables are defined.
5. Which variable does equals in size with enum variable?
a) int variable
b) float variable
c) string variable
d) float & string variable
View Answer
Answer: a
Explanation: The enum variable is converted to an integer and stored by the
compiler. So both are equal in size.

Note: Join free Sanfoundry classes at Telegram or Youtube

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

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

a) If you were cat, you would be 5


b) If you were cat, you would be 2
c) If you were cat, you would be 7
d) If you were cat, you would be 9
View Answer
Answer: b
Explanation: The age will be divided by using compound assignment operator and
so it will return the age of the cat according to your age.
$ g++ enum1.cpp
$ a.out
If you were cat, you would be 2

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

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

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

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.

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

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.

1. Choose the correct option.

extern int i;
int i;

a) both 1 and 2 declare i


b) 1 declares the variable i and 2 defines i
c) 1 declares and defines i, 2 declares i
d) 1 declares i,2 declares and defines i
View Answer
Answer: d
Explanation: The keyword extern is not a definition and is not allocated storage until
it is initialized.
advertisement
ADVERTISEMENT
ADVERTISEMENT

2. Pick the right option.

Statement 1: A definition is also a declaration.


Statement 2: An identifier can be declared just once.

a) Statement 1 is true, Statement 2 is false


b) Statement 2 is true, Statement 1 is false
c) Both are false
d) Both are true
View Answer
Answer: b
Explanation: An identifier can be declared many times must be defined just once.

Subscribe Now: C++ Newsletter | Important Subjects Newsletters

3. Which of the given statements are false?

i. extern int func;

ii. extern int func2(int,int);

iii. int func2(int,int);

iv. extern class foo;


a) iii and iv only
b) ii and iii only
c) only iv
d) ii, iii and iv
View Answer
Answer: c
Explanation: No extern are allowed for class declarations.

4. Pick the right option.

Statement 1: Global values are not initialized by the stream.


Statement 2: Local values are implicitly initialised to 0.

a) Statement 1 is true, Statement 2 is false


b) Statement 2 is true, Statement 1 is false
c) Both are false
d) Both are true
View Answer
Answer: c
Explanation: Global values are implicitly initialised to 0, but local values have to be
initialised by the system.

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

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.

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

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

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 = 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.

9. Identify the incorrect statements.

int var = 10;


int *ptr = &(var + 1); //statement 1
int *ptr2 = &var; //statement 2
&&var = 40; //statement 3

a) Statement 1 and 2 are wrong


b) Statement 2 and 3 are wrong
c) Statement 1 and 3 are wrong
d) Statement 1, 2 and 3 are wrong
View Answer
Answer: c
Explanation: In statement 1 lvalue is required as unary ‘&’ operand and in statement
3 lvalue is required as left operand.
10. Identify the type of variables.

typedef char* CHAR;


CHAR p,q;

a) char*
b) char
c) CHAR
d) unknown
View Answer
Answer: a
Explanation: The statement makes CHAR a synonym for char*.

1. What does the following statement mean?

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.

3. Choose the right option.

string* x, y;

a) x is a pointer to a string, y is a string


b) y is a pointer to a string, x is a string
c) both x and y are pointers to string types
d) y is a pointer to a string
View Answer
Answer: a
Explanation: * is to be grouped with the variables, not the data types.

4. Which one of the following is not a possible state for a pointer.


a) hold the address of the specific object
b) point one past the end of an object
c) zero
d) point to a type
View Answer
Answer: d
Explanation: A pointer can be in only 3 states a, b and c.

5. Which of the following is illegal?


a) int *ip;
b) string s, *sp = 0;
c) int i; double* dp = &i;
d) int *pi = 0;
View Answer
Answer: c
Explanation: dp is initialized int value of i.

6. What will happen in the following C++ code snippet?

1. int a = 100, b = 200;


2. int *p = &a, *q = &b;
3. p = q;

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.

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

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**).

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

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

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

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

1. Which of the following correctly declares an array?


a) int array[10];
b) int array;
c) array{10};
d) array array[10];
View Answer
Answer: a
Explanation: Because array variable and values need to be declared after the
datatype only.

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.

3. What is the correct definition of an array?


a) An array is a series of elements of the same type in contiguous memory locations
b) An array is a series of element
c) An array is a series of elements of the same type placed in non-contiguous
memory locations
d) An array is an element of the different type
View Answer
Answer: a
Explanation: Correct definition of an array is An array is a series of elements of the
same type in contiguous memory locations.
advertisement
4. Which of the following accesses the seventh element stored in array?
a) array[6];
b) array[7];
c) array(7);
d) array;
View Answer
Answer: a
Explanation: The array location starts from zero, So it can accessed by array[6].

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.

Subscribe Now: C++ Newsletter | Important Subjects Newsletters

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

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

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

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.

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

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.

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

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

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

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

1. What is the meaning of the following declaration?

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

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

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

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

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

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

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

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

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,

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


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 + 9;
8. return 0;
9. }

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. The constants are also called as _____________


a) const
b) preprocessor
c) literals
d) variables
View Answer
Answer: c
Explanation: Other name for Constants are literals.

2. What are the parts of the literal constants?


a) integer numerals
b) floating-point numerals
c) strings and boolean values
d) all of the mentioned
View Answer
Answer: d
Explanation: Because these are the types used to declare variables and so these can
be declared as constants.

3. How are the constants declared?


a) const keyword
b) #define preprocessor
c) both const keyword and #define preprocessor
d) $define
View Answer
Answer: c
Explanation: The const will declare with a specific type value and #define is used to
declare user-defined constants.
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 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.

Note: Join free Sanfoundry classes at Telegram or Youtube

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

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

6. Which of the following statement is not true about preprocessor directives?


a) These are lines read and processed by the preprocessor
b) They do not produce any code by themselves
c) These must be written on their own line
d) They end with a semicolon
View Answer
Answer: d
Explanation: No terminating character required for preprocessor directives
statements.

7. Regarding the following statement which of the statements is true?

const int a = 100;

a) Declares a variable a with 100 as its initial value


b) Declares a construction a with 100 as its initial value
c) Declares a constant a whose value will be 100
d) Constructs an integer type variable with an as identifier and 100 as the value
View Answer
Answer: c
Explanation: Because the const is used to declare non-changeable values only.

8. The difference between x and ‘x’ is?


a) The first one refers to a variable whose identifier is x and the second one refers to
the character constant x
b) The first one is a character constant x and the second one is the string literal x
c) Both are same
d) Both are string literal
View Answer
Answer: a
Explanation: In a C++ code, names with quotes like ‘x’ represent a character or
string(in case of a collection of characters) whereas without quotes they represent
an identifier.

9. How to declare a wide character in the string literal?


a) L prefix
b) l prefix
c) W prefix
d) Z prefix
View Answer
Answer: a
Explanation: It can turn this as the wide character instead of narrow characters.

1. What are the references in C++?


a) An alternative name for already existing variables
b) A pointer to a variable
c) A new type of variables
d) A new type of constant variable
View Answer
Answer: a
Explanation: References are an alternative name for an already defined variable.
They are different from pointers.

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

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int &q = 5;
cout<<q;
return 0;
}

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?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int &p;
int a = 5;
&p = a;
cout<<p;
return 0;
}

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.

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

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int a = 5;
int &p = a;
cout<<p;
return 0;
}

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>

using namespace std;

int main(int argc, char const *argv[])


{
int a = 5;
int *p = &a;
int &q = p;
cout<<p;
return 0;
}

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.

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

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int a = 5;
int *p = &a;
int *(&q) = p;
cout<<q;
return 0;
}

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.

7. What is the difference between references and pointers?


a) References are an alias for a variable whereas pointer stores the address of a
variable
b) References and pointers are similar
c) References stores address of variables whereas pointer points to variables
d) Pointers are an alias for a variable whereas references stores the address of a
variable
View Answer
Answer: a
Explanation: References are an alias/another name for a variable whereas pointer
stores the address of a variable. Pointers need to be deference before use whereas
references need not.

8. Pick the correct statement about references in C++.


a) References stores the address of variables
b) References and variables both have the same address
c) References use dereferencing operator(*) to access the value of variable its
referencing
d) References were also available in C
View Answer
Answer: b
Explanation: References and variable it is referring to shares the same address.
References do not consume extra address. References do not store the address of
other variables. No dereferencing operator required while using references.
References are not available in C++.

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

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int a = 5;
int *p = &a;
int &q = a;
cout<<p<<endl;
cout<<q<<endl;
return 0;
}

a) Address of a followed by 5 in next line


b) Address of p followed by 5 in next line
c) Address of a followed by Address of a in next line
d) Address of p followed by Address of q in next line
View Answer
Answer: a
Explanation: Pointer p stores the address of variable whereas q is alias for variable a
therefore when p is printed it prints the address of a and when q is printed value of
a is printed.

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

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int a = 5;
int *p = &a;
int &q = a;
cout<<*p<<endl;
cout<<*q<<endl;
return 0;
}

a) Address of a followed by 5 in next line


b) Address of p followed by 5 in next line
c) Run time error
d) Compile time error
View Answer
Answer: d
Explanation: References uses no * operator to access the value of variables it is
refering to therefore no program gives error as we are using * operator.

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


#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int a = 5;
int &q = a;
cout<<&a<<endl;
cout<<&q<<endl;
return 0;
}

a)

b) Address of p followed by 5 in next line


c) 5 followed by Address of a in next line
d) Address of a followed by Address of a in next line
View Answer
Answer: d
Explanation: Both variable and reference shares the same addres so the output will
be two times the address of a, because references are other name for same variable
not a new variable with separate memory.

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

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char const *argv[])


{
int &q = NULL;
cout<<q;
return 0;
}
a) NULL
b) 0
c) Address of NULL
d) Error
View Answer
Answer: d
Explanation: NULL cannot be assigned to references therefore the program gives
error. Here it is an int reference and NULL is not an int therefore cannot be assigned
to this reference.

13. Pick the correct statement about references.


a) References can be assigned value NULL
b) References once assigned cannot be changed to refer another variable
c) Reference should not be initialized when created
d) Reference is the same as pointers
View Answer
Answer: b
Explanation: References are should be initialized during its creation and once
assigned cannot be changed to refer another variable. References cannot be
assigned NULL value. References and pointers are two different concepts.

14. Which of the following operator is used while declaring references?


a) *
b) &
c) ^
d) ->
View Answer
Answer: b
Explanation: & operator is used for assigning references.

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

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

void func(const int &a)


{
int temp = 10;
a = temp;
cout<<a;
}

int main(int argc, char const *argv[])


{
int a = 5;
func(a);
return 0;
}

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.

1. Which value can we not assign to reference?


a) integer
b) floating
c) unsigned
d) null
View Answer
Answer: d
Explanation: If it can be assigned with a null value means, it is a copy of the pointer.

2. Identify the incorrect statement.


a) Reference is the alternate name of the object
b) A reference value once defined can be reassigned
c) A reference value once defined cannot be reassigned
d) Reference is the alternate name of the variable
View Answer
Answer: b
Explanation: Reference is a thing which points to the valid memory address, so it
can’t be redesigned.

3. Which reference modifier is used to define the reference variable?


a) &
b) $
c) #
d) @
View Answer
Answer: a
Explanation: & aka ‘ampersand’ used to define a reference variable.
advertisement
4. What will be the output of the following C++ code?
1. #include <iostream>
2. using namespace std;
3. void swap(int &a, int &b);
4. int main()
5. {
6. int a = 5, b = 10;
7. swap(a, b);
8. cout << "In main " << a << b;
9. return 0;
10. }
11. void swap(int &a, int &b)
12. {
13. int temp;
14. temp = a;
15. a = b;
16. b = temp;
17. cout << "In swap " << a << b;
18. }

a) In swap 105 In main 105


b) In swap 105 In main 510
c) In swap 510 In main 105
d) In swap 510 In main 510
View Answer
Answer: a
Explanation: As the function is called by reference i.e. all the changes are done
directly into the memories of a and b. Therefore changes made to a and b in swap
function is reflected back to main function. Hence the values of a and b in swap as
well as in main function is changed.
Output:
Subscribe Now: C++ Newsletter | Important Subjects Newsletters

$ g++ ref.cpp
$ a.out
In swap 105 In main 105

5. What does a reference provide?


a) Alternate name for the class
b) Alternate name for the variable
c) Alternate name for the pointer
d) Alternate name for the object
View Answer
Answer: b
Explanation: Because we are pointing memory address using the temp variable.

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


1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a = 9;
6. int & aref = a;
7. a++;
8. cout << "The value of a is " << aref;
9. return 0;
10. }

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

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

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.

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

#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

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.

3. Which of the following function must use reference.


a) Assignment operator function
b) Copy Constructor
c) Destructor
d) Parameterized constructor
View Answer
Answer: b
Explanation: We don’t need references in case of assignment, destructor or
constructor. But in case of a copy constructor, we need to call copy constructor
because if we use pass by value then as copy constructor itself is a function. So if we
pass an argument bypass by value method in a copy constructor, a call to copy
constructor would be made to again call copy constructor which becomes an
endless chain of calls. Therefore compiler doesn’t allow parameters to be passed by
value.

Note: Join free Sanfoundry classes at Telegram or Youtube

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

#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.

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

#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.

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

#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.

8. Which of the following statement(s) is/are correct?


a) * operator is used to declare a reference
b) A reference variable defined to refer a particular variable can refer to any other
variable also
c) References must always be initialized inside classes
d) A variable can have more than one references
View Answer
Answer: d
Explanation: A variable can have multiple references as references are nothing just
another name for a variable hence a variable can more than one references.

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

#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.

3. Which of the following function must use reference.


a) Assignment operator function
b) Copy Constructor
c) Destructor
d) Parameterized constructor
View Answer
Answer: b
Explanation: We don’t need references in case of assignment, destructor or
constructor. But in case of a copy constructor, we need to call copy constructor
because if we use pass by value then as copy constructor itself is a function. So if we
pass an argument bypass by value method in a copy constructor, a call to copy
constructor would be made to again call copy constructor which becomes an
endless chain of calls. Therefore compiler doesn’t allow parameters to be passed by
value.

Note: Join free Sanfoundry classes at Telegram or Youtube

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

#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.

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

#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.

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


#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.

8. Which of the following statement(s) is/are correct?


a) * operator is used to declare a reference
b) A reference variable defined to refer a particular variable can refer to any other
variable also
c) References must always be initialized inside classes
d) A variable can have more than one references
View Answer
Answer: d
Explanation: A variable can have multiple references as references are nothing just
another name for a variable hence a variable can more than one references.

1. The void pointer can point to which type of objects?


a) int
b) float
c) double
d) all of the mentioned
View Answer
Answer: d
Explanation: Because it doesn’t know the type of object it is pointing to, So it can
point to all objects.

2. When does the void pointer can be dereferenced?


a) when it doesn’t point to any value
b) when it cast to another type of object
c) using delete keyword
d) using shift keyword
View Answer
Answer: b
Explanation: By casting the pointer to another data type, it can be dereferenced
from the void pointer.

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

4. A void pointer cannot point to which of these?


a) methods in c++
b) class member in c++
c) methods & class member in c++
d) none of the mentioned
View Answer
Answer: d
Explanation: A void pointer can point to methods & class member in c++.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

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

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

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

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

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

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

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

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

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

10. What we can’t do on a void pointer?


a) pointer arithmetic
b) pointer functions
c) pointer objects
d) pointer functions & objects
View Answer
Answer: a
Explanation: Because the void pointer is used to cast the variables only, So pointer
arithmetic can’t be done in a void pointer.

1. The data elements in the structure are also known as what?


a) objects
b) members
c) data
d) objects & data
View Answer
Answer: b
Explanation: Variables declared inside a class are called as data elements or data
members.
2. What will be used when terminating a structure?
a) :
b) }
c) ;
d) ;;
View Answer
Answer: c
Explanation: While terminating a structure, a semicolon is used to end this up.

3. What will happen when the structure is declared?


a) it will not allocate any memory
b) it will allocate the memory
c) it will be declared and initialized
d) it will be declared
View Answer
Answer: a
Explanation: While the structure is declared, it will not be initialized, So it will not
allocate any memory.
advertisement
4. The declaration of the structure is also called as?
a) structure creator
b) structure signifier
c) structure specifier
d) structure creator & signifier
View Answer
Answer: c
Explanation: The structure declaration with open and close braces and with a
semicolon is also called structure specifier.

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

Subscribe Now: C++ Newsletter | Important Subjects Newsletters

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

c) compile time error


d) runtime error
View Answer
Answer: a
Explanation: We are copying the value john to the name and then we are printing
the values that are in the program.
Output:
$ g++ stu.cpp
$ a.out
123
john

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

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

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

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

a) Adidas $ 9.99Adidas $ 1.11


b) Adidas $ 9.99Adidas $ 9.11
c) Adidas $ 9.99Adidas $ 11.11
d) Adidas $ 11.11Adidas $ 11.11
View Answer
Answer: a
Explanation: We copied the value of shoe1 into shoe2 and divide the shoe2 value by
9, So this is the output.
Output:
$ g++ stu2.cpp
$ a.out
Adidas $ 9.99
Adidas $ 1.11

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

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

9. Which of the following is a properly defined structure?


a) struct {int a;}
b) struct a_struct {int a;}
c) struct a_struct int a;
d) struct a_struct {int a;};
View Answer
Answer: d
Explanation: option struct {int a;} is not correct because name of structure and
;(after declaration) are missing. In option struct a_struct {int a;} ; is missing. In option
struct a_struct int a; {} are missing.

10. Which of the following accesses a variable in structure *b?


a) b->var;
b) b.var;
c) b-var;
d) b>var;
View Answer
Answer: a
Explanation: Because arrow operator(->) is used to access members of structure
pointer whereas dot operator(.) is used to access normal structure variables.

1. Which function is used to check whether a character is an alphabet?


a) isalpha()
b) isalnum()
c) isdigit()
d) isblank()
View Answer
Answer: a
Explanation: Character classification provides isalpha() function to check whether a
character in C++ is an alphabet or not.

2. Which function is used to check whether a character is an alphabet or number?


a) isalpha()
b) isalnum()
c) isdigit()
d) isblank()
View Answer
Answer: b
Explanation: Character classification provides isalnum() function to check whether a
character in C++ is alphabet or number.

3. Which function is used to check whether a character is a number?


a) isalpha()
b) isalnum()
c) isdigit()
d) isblank()
View Answer
Answer: c
Explanation: Character classification provides isdigit() function to check whether a
character in C++ is number or not.
advertisement
4. Which function is used to check whether a character is a tab or space?
a) isalpha()
b) isalnum()
c) isdigit()
d) isblank()
View Answer
Answer: d
Explanation: Character classification provides isblank() function to check whether a
character in C++ is space or tab.

5. Which function is used to check whether a character is tab or space or whitespace


control code(\n,\r,etc.)?
a) isspace()
b) isalnum()
c) iscntrl()
d) isblank()
View Answer
Answer: a
Explanation: Character classification provides isspace() function to check whether a
character in C++ is tab or space or whitespace control code(\n, \r, etc.).

Subscribe Now: C++ Newsletter | Important Subjects Newsletters

6. Which function is used to check whether a character is tab or a control code?


a) isspace()
b) isalnum()
c) iscntrl()
d) isblank()
View Answer
Answer: c
Explanation: Character classification provides iscntrl() function to check whether a
character in C++ is tab or a control code.

7. Which function is used to check whether a character is printable on console?


a) isxdigit()
b) isprint()
c) iscntrl()
d) ispunct()
View Answer
Answer: b
Explanation: Character classification provides isprint() function to check whether a
character in C++ is printable on console.

8. Which function is used to check whether a character is hexadecimal?


a) isxdigit()
b) isprint()
c) iscntrl()
d) ispunct()
View Answer
Answer: a
Explanation: Character classification provides isxdigit() function to check whether a
character in C++ is hexadecimal.

9. Which function is used to check whether a character is punctuation mark?


a) isxdigit()
b) isprint()
c) iscntrl()
d) ispunct()
View Answer
Answer: d
Explanation: Character classification provides ispunct() function to check whether a
character in C++ is punctuation mark.

10. 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] = "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.

11. 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 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.

13. 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)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.

14. 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[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.

15. 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[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. Which operator is having the right to left associativity in the following?


a) Array subscripting
b) Function call
c) Addition and subtraction
d) Type cast
View Answer
Answer: d
Explanation: There are many rights to left associativity operators in C++, which
means they are evaluation is done from right to left. Type Cast is one of them. Here
is a link of the associativity of operators: https://github.com/MicrosoftDocs/cpp-
docs/blob/master/docs/cpp/cpp-built-in-operators-precedence-and-associativity.md

2. Which operator is having the highest precedence?


a) postfix
b) unary
c) shift
d) equality
View Answer
Answer: a
Explanation: The operator which is having the highest precedence is postfix and
lowest is equality.

3. What is this operator called ?:?


a) conditional
b) relational
c) casting operator
d) unrelational
View Answer
Answer: a
Explanation: In this operator, if the condition is true means, it will return the first
operator, otherwise second operator.
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 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

5. What is the use of dynamic_cast operator?


a) it converts virtual base class to derived class
b) it converts the virtual base object to derived objects
c) it will convert the operator based on precedence
d) it converts the virtual base object to derived class
View Answer
Answer: a
Explanation: Because the dynamic_cast operator is used to convert from base class
to derived class.

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

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

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

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

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

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

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

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

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

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. How are many sequences of statements present in c++?


a) 4
b) 3
c) 5
d) 6
View Answer
Answer: c
Explanation: There are five sequences of statements. They are Preprocessor
directives, Comments, Declarations, Function Declarations, Executable statements.

2. The if..else statement can be replaced by which operator?


a) Bitwise operator
b) Conditional operator
c) Multiplicative operator
d) Addition operator
View Answer
Answer: b
Explanation: In the conditional operator, it will predicate the output using the given
condition.

3. The switch statement is also called as?


a) choosing structure
b) selective structure
c) certain structure
d) bitwise structure
View Answer
Answer: b
Explanation: The switch statement is used to choose the certain code to execute, So
it is also called as selective structure.
advertisement
4. The destination statement for the goto label is identified by what label?
a) $
b) @
c) *
d) :
View Answer
Answer: d
Explanation: : colon is used at the end of labels of goto statements.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

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

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

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.

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

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.

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

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

9. How many types of loops are there in C++?


a) 4
b) 2
c) 3
d) 1
View Answer
Answer: a
Explanation: There are four types of loop. They are the while, do while, nested, for
the loop.

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. How many types of comments are there in c++?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: There are two types of comments in C++. Single line comments uses
double slash //. Multiple line comments uses /* comment inside */.

2. What is a comment in c++?


a) comments are parts of the source code disregarded by the compiler
b) comments are executed by the compiler to find the meaning of the comment
c) comments are executable
d) comments are executed by the compiler
View Answer
Answer: a
Explanation: Comments are used to add meaning to the program.

3. What type of comments does c++ support?


a) single line
b) multiline
c) single line and multi-line
d) reusable line
View Answer
Answer: c
Explanation: C++ provides two types of comments in programs. They are single
line(using //) or multiple line (using /*…… */) comments.
advertisement
4. What will be the output of the following C++ code?

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

5. What is used to write multi line comment in c++?


a) /* …. */
b) /$ …. $/
c) //
d) /$ …. */
View Answer
Answer: a
Explanation: The /* is used to write the multi line comment.

6. What is the use of the indentation in c++?


a) distinguishes between comments and code
b) r distinguishes between comments and outer data
c) distinguishes between comments and outer data
d) r distinguishes between comments and inner data
View Answer
Answer: a
Explanation: To distinguish between different parts of the program like comments,
codes, etc.

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

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

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

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

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


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

10. What will happen when we use void in argument passing?


a) It will not return value to its caller
b) It will return value to its caller
c) May or may not depend on the declared return type of the function, the passed
arguments are different than the function return type
d) It will return value
View Answer
Answer: a
Explanation: As void is not having any return value, it will not return the value to the
caller.

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

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. Where does the execution of the program starts?


a) user-defined function
b) main function
c) void function
d) else function
View Answer
Answer: b
Explanation: Normally the execution of the program in c++ starts from main only.

2. What are mandatory parts in the function declaration?


a) return type, function name
b) return type, function name, parameters
c) parameters, function name
d) parameters, variables
View Answer
Answer: a
Explanation: In a function, return type and function name are mandatory all else are
just used as a choice.

3. which of the following is used to terminate the function declaration?


a) :
b) )
c) ;
d) ]
View Answer
Answer: c
Explanation: ; semicolon is used to terminate a function declaration statement in
C++.
advertisement
4. How many can max number of arguments present in function in the c99
compiler?
a) 99
b) 90
c) 102
d) 127
View Answer
Answer: d
Explanation: C99 allows to pass a maximum of 127 arguments in a function.

5. Which is more effective while calling the functions?


a) call by value
b) call by reference
c) call by pointer
d) call by object
View Answer
Answer: b
Explanation: In the call by reference, it will just passes the reference of the memory
addresses of passed values rather than copying the value to new memories which
reduces the overall time and memory use.

Subscribe Now: C++ Newsletter | Important Subjects Newsletters

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

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.

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

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. How many minimum number of functions should be present in a C++ program


for its execution?
a) 0
b) 1
c) 2
d) 3
View Answer
Answer: b
Explanation: The execution of a C++ program starts from main function hence we
require atleast 1 function to be present in a C++ program to execute and i.e. the
main function.

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


a) int
b) char
c) float
d) void
View Answer
Answer: a
Explanation: C++ uses int as the default return values for functions. It also restricts
that the return type of the main function must be int.

2. What happens to a function defined inside a class without any complex


operations (like looping, a large number of lines, etc)?
a) It becomes a virtual function of the class
b) It becomes a default calling function of the class
c) It becomes an inline function of the class
d) The program gives an error
View Answer
Answer: c
Explanation: Any function which is defined inside a class and has no complex
operations like loops, a large number of lines then it is made inline.

3. What is an inline function?


a) A function that is expanded at each call during execution
b) A function that is called during compile time
c) A function that is not checked for syntax errors
d) A function that is not checked for semantic analysis
View Answer
Answer: a
Explanation: Inline function is those which are expanded at each call during the
execution of the program to reduce the cost of jumping during execution.
advertisement
4. An inline function is expanded during ______________
a) compile-time
b) run-time
c) never expanded
d) end of the program
View Answer
Answer: a
Explanation: An inline function is expanded during the compile-time of a program.

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


i) If the function has static variables.
ii) If the function has global and register variables.
iii) If the function contains loops
iv) If the function is recursive
a) i, iv
b) iii, iv
c) ii, iii, iv
d) i, iii, iv
View Answer
Answer: d
Explanation: A function is not inline if it has static variables, loops or the function is
having any recursive calls.

Note: Join free Sanfoundry classes at Telegram or Youtube

6. When we define the default values for a function?


a) When a function is defined
b) When a function is declared
c) When the scope of the function is over
d) When a function is called
View Answer
Answer: b
Explanation: Default values for a function is defined when the function is declared
inside a program.

7. Where should default parameters appear in a function prototype?


a) To the rightmost side of the parameter list
b) To the leftmost side of the parameter list
c) Anywhere inside the parameter list
d) Middle of the parameter list
View Answer
Answer: a
Explanation: Default parameters are defined to the rightmost side of parameter list
in a function to differentiate between the normal and default parameters for
example if a function is defined as fun(int x = 5, int y) then if we call fun(10) then 10
should be given to x or y because one can apply both logics like x = 10 already
defined and 10 passed is for y but if compiler reads it from left to right it will think it
is for x and no parameter is given for y, therefore, the compiler will give error.

8. If an argument from the parameter list of a function is defined constant then


_______________
a) It can be modified inside the function
b) It cannot be modified inside the function
c) Error occurs
d) Segmentation fault
View Answer
Answer: b
Explanation: A function is not allowed a constant member of the parameter list.

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.

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

#include<iostream>
using namespace std;

int fun(int x = 0, int y = 0, int z)


{ return (x + y + z); }

int main()
{
cout << fun(10);
return 0;
}

a) 10
b) 0
c) Error
d) Segmentation fault
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.

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

#include<iostream>
using namespace std;

class Test
{
protected:
int x;
public:
Test (int i):x(i) { }
void fun() const { cout << "fun() const " << endl; }
void fun() { cout << "fun() " << endl; }
};

int main()
{
Test t1 (10);
const Test t2 (20);
t1.fun();
t2.fun();
return 0;
}

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().

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

#include <iostream>
using namespace std;

int fun(int=0, int = 0);

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

a) -5
b) 0
c) 10
d) 5
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.

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

#include <iostream>
using namespace std;
void square (int *x, int *y)
{
*x = (*x) * --(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}

a) 870
b) 30
c) Error
d) Segmentation fault
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.

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


a) start() function
b) main() function
c) new() function
d) end() function
View Answer
Answer: b
Explanation: The execution of a C++ program starts from the main() function.

15. Which of the following is important in a function?


a) Return type
b) Function name
c) Both return type and function name
d) The return type, function name and parameter list
View Answer
Answer: c
Explanation: The important things required in a function is its return type and its
name other than that parameter list are optional which a function may or may not
have.

1. How many ways of passing a parameter are there in c++?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: There are three ways of passing a parameter. They are pass by
value,pass by reference and pass by pointer.

2. Which is used to keep the call by reference value as intact?


a) static
b) const
c) absolute
d) virtual
View Answer
Answer: b
Explanation: Because const will not change the value of the variables during the
execution.

3. By default how the value are passed in c++?


a) call by value
b) call by reference
c) call by pointer
d) call by object
View Answer
Answer: a
Explanation: None.
advertisement
4. What will be the output of the following C++ code?

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

5. What will be the new value of x in the following C++ code?

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

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

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

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

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

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

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

9. What will happen when we use void in argument passing?


a) It will not return value to its caller
b) It will return value to its caller
c) Maybe or may not be return any value to its caller
d) It will return value with help of object
View Answer
Answer: a
Explanation: As void is not having any return value, it will not return the value to the
caller.

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

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. How many types of returning values are present in c++?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: The three types of returning values are return by value, return by
reference and return by address.
2. What will you use if you are not intended to get a return value?
a) static
b) const
c) volatile
d) void
View Answer
Answer: d
Explanation: Void is used to not to return anything.

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

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

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

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

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

7. When will we use the function overloading?


a) same function name but different number of arguments
b) different function name but same number of arguments
c) same function name but same number of arguments
d) different function name but different number of arguments
View Answer
Answer: a
Explanation: We use function overloading when we want the same name function to
perform different procedure for different types of parameters or different number
of parameters provided to the function.

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

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. Which of the following permits function overloading on c++?


a) type
b) number of arguments
c) type & number of arguments
d) number of objects
View Answer
Answer: c
Explanation: Both type and number of arguments permits function overloading in
C++, like
int func(int);
float func(float, float)
Here both type and number of arguments are different.

2. In which of the following we cannot overload the function?


a) return function
b) caller
c) called function
d) main function
View Answer
Answer: a
Explanation: While overloading the return function, it will rise a error, So we can’t
overload the return function.
3. Function overloading is also similar to which of the following?
a) operator overloading
b) constructor overloading
c) destructor overloading
d) function overloading
View Answer
Answer: b
Explanation: In constructor overloading, we will be using the same options availed in
function overloading.
advertisement
4. What will be the output of the following C++ code?

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

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


1. #include <iostream>
2. using namespace std;
3. int Add(int X, int Y, int Z)
4. {
5. return X + Y;
6. }
7. double Add(double X, double Y, double Z)
8. {
9. return X + Y;
10. }
11. int main()
12. {
13. cout << Add(5, 6);
14. cout << Add(5.5, 6.6);
15. return 0;
16. }

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.

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

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

7. Overloaded functions are ________________


a) Very long functions that can hardly run
b) One function containing another one or more functions inside it
c) Two or more functions with the same name but different number of parameters
or type
d) Very long functions
View Answer
Answer: c
Explanation: This is the definition of function overloading i.e. function having same
name but different number of parameters and types.

8. What will happen while using pass by reference?


a) The values of those variables are passed to the function so that it can manipulate
them
b) The location of variable in memory is passed to the function so that it can use the
same memory area for its processing
c) The function declaration should contain ampersand (& in its type declaration)
d) The function declaration should contain $
View Answer
Answer: b
Explanation: In pass by reference, we can use the function to access the variable
and it can modify it. Therefore we are using pass by reference.

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.

2. Where can the default parameter be placed by the user?


a) leftmost
b) rightmost
c) both leftmost & rightmost
d) topmost
View Answer
Answer: b
Explanation: To avoid the ambiguity between the non-default parameters and
default parameters.

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

a) Flag is true. a = 200


b) Flag is false. a = 100
c) Flag is false. a = 200
d) Flag is true. a = 100
View Answer
Answer: c
Explanation: In this program, we are passing the value, as it evaluates to false, it
produces the output as following.
Output:
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!

$ g++ def.cpp
$ a.out
Flag is false. a = 200

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

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

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

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

c) compile time error


d) runtime error
View Answer
Answer: a
Explanation: In this program, We are passing the values as by default values rules it
is working.
Output:
$ g++ def2.cpp
$ a.out
1st value: 1
2nd value: 10
1st value: 3
2nd value: 4

7. What we can’t place followed by the non-default arguments?


a) trailing arguments
b) default arguments
c) both trailing & default arguments
d) leading arguments
View Answer
Answer: b
Explanation: To avoid the ambiguity in arguments.
eg. if func(int a=3, int b);
so if we call func(5), here will 5 will be value of a or b, because 5 is first parameter so
a should be 5 but as only one argument is given b should be 5. So to remove such
ambiguity default parameters are kept at the end or rightmost side.

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.

9. What is the default return type of a function?


a) int
b) void
c) float
d) char
View Answer
Answer: b
Explanation: void is the default return value of any function, to handle both empty
and non-empty values.

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

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.

1. Which header file is used to pass unknown number of arguments to function?


a) stdlib.h
b) string.h
c) stdarg.h
d) stdio.h
View Answer
Answer: c
Explanation: Because the cstdarg defines this header file to process the unknown
number of arguments.

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.

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

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

4. What is the maximum number of arguments or parameters that can be present


in one function call?
a) 64
b) 256
c) 255
d) 16
View Answer
Answer: b
Explanation: C++ allows maximum number of 256 arguments in a function call.

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

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

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

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

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


1. #include <iostream>
2. #include <stdarg.h>
3. using namespace std;
4. int flue(char c,...);
5. int main()
6. {
7. int x, y;
8. x = flue('A', 1, 2, 3);
9. y = flue('1', 1.0,1, '1', 1.0f, 1l);
10. cout << x << y;
11. return 0;
12. }
13. int flue(char c,...)
14. {
15. return c;
16. }

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.

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

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. To which does the function pointer point to?


a) variable
b) constants
c) function
d) absolute variables
View Answer
Answer: c
Explanation: A function pointer points to a function.

2. What will we not do with function pointers?


a) allocation of memory
b) deallocation of memory
c) both allocation & deallocation of memory
d) finds memory status
View Answer
Answer: c
Explanation: As it is used to execute a block of code, So we will not allocate or
deallocate memory.

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


a) __cdecl
b) __stdcall
c) __pascal
d) __fastcall
View Answer
Answer: a
Explanation: __cdecl is the default calling convention for a compiler in c++.
advertisement
ADVERTISEMENT
ADVERTISEMENT
4. What will be the output of the following C++ code?

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

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

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

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

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

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

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.

8. What is the mandatory part to present in function pointers?


a) &
b) return values
c) data types
d) $
View Answer
Answer: c
Explanation: The data types are mandatory for declaring the variables in the
function pointers.

9. which of the following can be passed in function pointers?


a) variables
b) data types
c) functions
d) objects
View Answer
Answer: c
Explanation: Only functions are passed in function pointers.

10. What is the meaning of the following declaration?

int(*ptr[5])();

a) ptr is pointer to function


b) ptr is array of pointer to function
c) ptr is pointer to such function which return type is array
d) ptr is pointer to array of function
View Answer
Answer: b
Explanation: In this expression, ptr is array not pointer.

1. which keyword is used to define the macros in c++?


a) macro
b) define
c) #define
d) #macro
View Answer
Answer: c
Explanation: #define is the keyword which is used to define the macros in c++.

2. Which symbol is used to declare the preprocessor directives?


a) #
b) $
c) *
d) ^
View Answer
Answer: a
Explanation: # symbol is used to declare the preprocessor directives.

3. How many types of macros are there in c++?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: There are two types of macros. They are object-like and function-like.
advertisement
4. What is the mandatory preprocessor directive for c++?
a) #define <iostream>
b) #include <iostream>
c) #undef <iostream>
d) #macro <iostream>
View Answer
Answer: b
Explanation: For a c++ program to execute, we need #include<iostream>.

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

Note: Join free Sanfoundry classes at Telegram or Youtube

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

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

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

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

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

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

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

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

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.

10. What is the other name of the macro?


a) scripted directive
b) executed directive
c) link directive
d) executed & link directive
View Answer
Answer: a
Explanation: When the compiler encounters a previously defined macro, it will take
the result from that execution itself.

1. which of the following is used to implement the c++ interfaces?


a) absolute variables
b) abstract classes
c) constant variables
d) default variables
View Answer
Answer: b
Explanation: Abstract classes in C++ are purposely defined for making base classes
containing atleast one virtual function which can be overloaded on inheritance,
which means single function name for different sub-classes, hence acts as an
interface.

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.

3. How many types do functions fall depends on modularization?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: There are two types of functions. They are program control and
specific task.
advertisement
4. How many types of modularization are there in c++?
a) 4
b) 3
c) 1
d) 2
View Answer
Answer: d
Explanation: There are two types of modular programming. They are interface and
implementation.

5. What does the client module import?


a) macro
b) records
c) interface
d) instance
View Answer
Answer: c
Explanation: Because they access the functions in the module user interface.

Note: Join free Sanfoundry classes at Telegram or Youtube

6. Identify the correct statement.


a) c++ does not have built-in interfaces
b) c++ does have built-in interfaces
c) c++ have no concept of interfaces
d) c++ does have built-in interfaces & classes
View Answer
Answer: a
Explanation: Unlike other programming languages like Java and others, C++ has no
inbuilt interfaces.

7. What is similar to the interface in c++?


a) methods
b) instance of a class
c) pure abstract class
d) methods & instance of a class
View Answer
Answer: c
Explanation: Pure abstract classes in C++ are a type of interface because it contains
only abstract member functions and no data or concrete member functions.
8. Which of the following implements the module in the program?
a) macro
b) header files
c) macro & header files
d) interfaces
View Answer
Answer: b
Explanation: We can include the group of code by using the #include header file.

1. Which operator is used to signify the namespace?


a) conditional operator
b) ternary operator
c) scope operator
d) bitwise operator
View Answer
Answer: c
Explanation: Scope operator(::) is used in namespace syntax.
General syntax:
namespace X{ int a;}
cout<<X::a;

2. Identify the correct statement.


a) Namespace is used to group class, objects and functions
b) Namespace is used to mark the beginning of the program
c) A namespace is used to separate the class, objects
d) Namespace is used to mark the beginning & end of the program
View Answer
Answer: a
Explanation: Namespace allows you to group class, objects, and functions. It is used
to divide the global scope into the sub-scopes.

3. What is the use of Namespace?


a) To encapsulate the data
b) To structure a program into logical units
c) Encapsulate the data & structure a program into logical units
d) It is used to mark the beginning of the program
View Answer
Answer: b
Explanation: The main aim of the namespace is to understand the logical units of
the program and to make the program so robust.
advertisement
4. What is the general syntax for accessing the namespace variable?
a) namespace::operator
b) namespace,operator
c) namespace#operator
d) namespace$operator
View Answer
Answer: a
Explanation: To access variables from namespace we use following syntax.
namespace :: variable;
General syntax:
namespace X{ int a;}
cout<<X::a;

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

Note: Join free Sanfoundry classes at Telegram or Youtube

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

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


1. #include <iostream>
2. using namespace std;
3. namespace first
4. {
5. int x = 5;
6. int y = 10;
7. }
8. namespace second
9. {
10. double x = 3.1416;
11. double y = 2.7183;
12. }
13. int main ()
14. {
15. using first::x;
16. using second::y;
17. bool a, b;
18. a = x > y;
19. b = first::y < second::x;
20. cout << a << b;
21. return 0;
22. }

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

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

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

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

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

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

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.

10. Which keyword is used to access the variable in the namespace?


a) using
b) dynamic
c) const
d) static
View Answer
Answer: a
Explanation: using keyword is used to specify the name of the namespace to which
the variable belongs.
eg.
namespace A{ int a = 5;}
namespace B{ int a = 10;}
using namespace A;
cout<<a; // will print value of a from namespace A.
using namespace B;
cout<<a; // will print value of a from namespace B.

1. Pick the incorrect statement for namespaces in C++.


a) Namespace declarations are always global scope
b) Keyword namespace is used at the starting of a namespace definition
c) Namespace has access specifiers like private or public
d) Namespace definitions can be nested
View Answer
Answer: c
Explanation: Namespace does not have any specifiers associated with it like classes
or structures.

2. Which operator is used for accessing a member of namespace?


a) :
b) ::
c) ->
d) .
View Answer
Answer: b
Explanation: Scope resolution operator(::) is used for accessing a member of a
namespace. example:
namespace A{
int var;
}
A::var = 5;

3. Which is the correct syntax of declaring a namespace?


a)

namespace A{

int i

}
advertisement
b)

namespace B{

int i;

};

Note: Join free Sanfoundry classes at Telegram or Youtube

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.

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

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

int var = 10;


}
namespace B{
int cout = 5;
}
int main()
{
using namespace B;
cout<<A::var;
}

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.

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

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

int var = 10;


}
namespace B{
int var = 5;
}
int main()
{
using namespace B;
cout<<var;
}

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.

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

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

int var = 10;


}
namespace B{
int var = 5;
}
int main()
{
int var = 20;
using namespace B;
cout<<var;
}

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.

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

#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.

8. What is the correct syntax of defining a namespace?


a) namespace name{}
b) Namespace name{};
c) namespace name{};
d) typedef namespace name{} NAME
View Answer
Answer: a
Explanation: A namespace:
-Starts with keyword namespace
-Followed by identifier
-All members inside the braces{}
-No semicolon at the end
namespace identifier{}.

9. How to print the value of the i variable inside namespace B?

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.

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

#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.

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

Content of header file h1.h


------------------------------------------------
h1.h
#include <iostream>
using namespace std;
namespace A{
int func(int a){
cout<<"using namespace A";
return 2*a;
}
}
------------------------------------------------

Content of header file h2.h


------------------------------------------------

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)

using namespace A10

using namespace B10

b)

using namespace A20

using namespace B20

c) Error due to clash of func()


d) This is not allwed in C++
View Answer
Answer: b
Explanation: Here both the func() are available in different namespaces having same
name but different types which is equivalent to function overloading. So no error
will be there as function overloading is allowed in C++.

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

Content of header file h1.h


------------------------------------------------
h1.h
#include <iostream>
using namespace std;
namespace A{
int func(int a){
cout<<"using namespace A";
return 2*a;
}
}
------------------------------------------------

Content of header file h2.h


------------------------------------------------

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)

using namespace A20


using namespace B20

b)

using namespace A20


using namespace A20

c)

using namespace A10


using namespace A10

d)

using namespace A10


using namespace B10

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

a) Cannot be handled because C++ does not allow this


b) Declare both the function inside different namespaces
c) Include one header files where they are needed so that no clashes occur
d) Make the header files name same
View Answer
Answer: b
Explanation: Define both the function in different namespaces under their
respective header files. So whenever we need them we can use the name of their
respective namespaces to call them. This will resolve the error keeping function
declarations same.
Modified Header files :
h1.h
Content of h1.h
------------------------------------------------
#include <iostream>
using namespace std;
namespace A{
int func(int a){
cout<<"Multiplied by 2";
return 2*a;
}
}
------------------------------------------------

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.

2. Which keyword is used to check exception in the block of code?


a) catch
b) throw
c) try
d) handlers
View Answer
Answer: c
Explanation: The try() statement is used for exceptions in c++.

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

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

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

a) compile time error


b) prints first 19 numbers
c) prints first 19 numbers and throws exception at 20
d) prints first 17 numbers
View Answer
Answer: c
Explanation: In this program, we are printing upto 19 numbers and when executing
the 20, we are raising a exception.
Output:
$ g++ excep1.cpp
$ a.out
12345678910111213141516171819Caught an exception with value: 20

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

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

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

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

a) 4 Bytes allocated successfully


b) 8 Bytes allocated successfully
c) Memory allocation failure
d) Depends on the size of the data type
View Answer
Answer: d
Explanation: As we are allocating the memory to the variables and if there are not
sufficient size means, it will throw an exception.
Output:
$ g++ excep3.cpp
$ a.out
4 Bytes allocated successfully

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

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

a) caught a double type


b) compile time error
c) abnormal program termination
d) runtime error
View Answer
Answer: c
Explanation: As we are throwing integer to double it will raise as abnormal program
after termination throw statement.
Output:
$ g++ excep4.cpp
$ a.out
terminate called after throwing an instance of 'int'
Aborted

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

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. How many types of linkages are there in C++?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: There are three types of linkage in c++. They are an internal linkage,
external linkage, and no linkage.

2. To use internal linkage we have to use which keyword?


a) static
b) extern
c) static or extern
d) public
View Answer
Answer: a
Explanation: static keyword is used for internal linkage.

3. Which one is used to refer to program elements in any translation units?


a) internal linkage
b) external linkage
c) no linkage
d) internal & external linkage
View Answer
Answer: b
Explanation: In the external linkage, it is used to refer to identifiers in various
programs.
advertisement
4. What will be the output of the following C++ codes?
i.

1. #ifndef Exercise_H
2. #define Exercise_H
3. int number = 842;
4. #endif

ii.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

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

5. What is the default type of linkage that is available for identifiers?


a) internal
b) external
c) no linkage
d) single linkage
View Answer
Answer: b
Explanation: external is the default type of linkage that is available for identifiers.

6. To use external linkage we have to use which keyword?


a) static
b) extern
c) const
d) argc
View Answer
Answer: b
Explanation: Extern keyword is used to represent identifiers from other programs.

7. Which is used to use a function from one source file to another?


a) code
b) declaration
c) prototype
d) variable
View Answer
Answer: c
Explanation: By defining a function’s prototype in another file means, we can inherit
all the features from the source function.

8. What is the use of no linkage?


a) make the entity visible to other programs
b) make the entity visible to other blocks in the same program
c) make the entity visible only to that block
d) make the entity invisible
View Answer
Answer: c
Explanation: None.

1. What is the user-defined header file extension in c++?


a) cpp
b) h
c) hf
d) hg
View Answer
Answer: b
Explanation: .h extensions are used for user defined header files. To include a user
defined header file one should use #include”name.h” i.e. enclosed within double
quotes.

2. Which of the following keyword is used to declare the header file?


a) include
b) exclude
c) string
d) namespace
View Answer
Answer: a
Explanation: The include keyword is used to include all the required things to
execute the given code in the program.

3. Identify the incorrect statement.


a) iostream is a standard header and iostream.h is a non-standard header
b) iostream is a non-standard header and iostream.h is a non-standard header
c) iostream is a standard header and iostream.h is a standard header
d) iostream is a non-standard header
View Answer
Answer: a
Explanation: The iostream.h is used in the older versions of c++ and iostream is
evolved from it in the std namespace.
advertisement
4. What does a default header file contain?
a) prototype
b) implementation
c) declarations
d) pointing
View Answer
Answer: c
Explanation: In the header file, we define something that to be manipulated in the
program.
5. What will be the output of the following C++ code?

Subscribe Now: C++ Newsletter | Important Subjects Newsletters

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.

6. setprecision requires which of the following header file?


a) stdlib.h
b) iomanip.h
c) console.h
d) conio.h
View Answer
Answer: b
Explanation: The iomanip header file is used to correct the precision of the values.

7. Which of the following header file does not exist?


a) <iostream>
b) <string>
c) <sstring>
d) <sstream>
View Answer
Answer: c
Explanation: There is no such header file <sstring> in C++.

8. Which of the header file must be included to use stringstream?


a) <iostream>
b) <string>
c) <sstring>
d) <sstream>
View Answer
Answer: b
Explanation: stringstream is available under the header file <string> in C++.

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.

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

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. What does a class in C++ holds?


a) data
b) functions
c) both data & functions
d) arrays
View Answer
Answer: c
Explanation: The classes in C++ encapsulates(i.e. put together) all the data and
functions related to them for manipulation.

2. How many specifiers are present in access specifiers in class?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: There are three types of access specifiers. They are public, protected
and private.

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


a) :
b) ::
c) #
d) !!$
View Answer
Answer: b
Explanation: :: operator is used to define the body of any class function outside the
class.
advertisement
4. Which other keywords are also used to declare the class other than class?
a) struct
b) union
c) object
d) both struct & union
View Answer
Answer: d
Explanation: Struct and union take the same definition of class but differs in the
access techniques.
5. What will be the output of the following C++ code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.


Participate Now!

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

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

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 (&param == 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

7. Which of the following is a valid class declaration?


a) class A { int x; };
b) class B { }
c) public class A { }
d) object A { int x; };
View Answer
Answer: a
Explanation: A class declaration terminates with semicolon and starts with class
keyword. only option (a) follows these rules therefore class A { int x; }; is correct.

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.

9. Constructors are used to ____________


a) initialize the objects
b) construct the data members
c) both initialize the objects & construct the data members
d) delete the objects
View Answer
Answer: a
Explanation: Once the object is declared means, the constructor are also declared
by default.

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.

1. Which category of data type a class belongs to?


a) Fundamental data type
b) Derived data type
c) User defined derived data type
d) Atomic data type
View Answer
Answer: c
Explanation: Fundamental/Atomic data type includes int, char, float, double and
void. Derived data type includes arrays, pointers, references, function and
constants. User defined derived data type includes class, structure, union and
enumeration.

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.

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

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.

Note: Join free Sanfoundry classes at Telegram or Youtube

4. What is the correct syntax of accessing a static member of a Class?

---------------------------
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.

5. How the objects are self-referenced in a member function of that class.


a) Using a special keyword object
b) Using this pointer
c) Using * with the name of that object
d) By passing self as a parameter in the member function
View Answer
Answer: b
Explanation: In Classes objects are self-referenced using this pointer inside the
member functions. for example this->value to access the data member value of that
object.

6. What does a mutable member of a class mean?


a) A member that can never be changed
b) A member that can be updated only if it not a member of constant object
c) A member that can be updated even if it a member of constant object
d) A member that is global throughout the class
View Answer
Answer: c
Explanation: Mutable members are those which can be updated even if it a member
of a constant object. You can change their value even from a constant member
function of that class.
7. What will be the output of the following C++ code?

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

};

int main(int argc, char const *argv[])


{
A obj;
obj.assign(5);
cout<<obj.return_value();
}

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.

8. Pick the incorrect statement about inline functions in C++?


a) They reduce function call overheads
b) These functions are inserted/substituted at the point of call
c) Saves overhead of a return call from a function
d) They are generally very large and complicated function
View Answer
Answer: d
Explanation: Inline are functions that are expanded when it is called. The whole
code of the inline function gets inserted/substituted at the point of call. In this, they
help in reducing the function call overheads. Also they save overhead of a return call
from a function. Inline functions are generally kept small.

9. Inline functions are avoided when ____________________________


a) function contains static variables
b) function have recursive calls
c) function have loops
d) all of the mentioned
View Answer
Answer: d
Explanation: Inline functions are avoided in all the above cases as whole inline code
is copied to the point of call so compiler avoids to make large functions as inline.
Even if you yourself mention inline but the function is large compiler ignores your
request of inline and treats that function as a normal function.

10. Pick the correct statement.


a) Macros and inline functions are same thing
b) Macros looks like function calls but they are actually not
c) Inline functions looks like function but they are not
d) Inline function are always large
View Answer
Answer: b
Explanation: Macros in C++ looks like function calls but actually they are not
function calls.

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

#include <iostream>
using namespace std;
class S
{
int m;
public:
#define MAC(S::m)
};

int main(int argc, char const *argv[])


{
cout<<"Hello World";
return 0;
}

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.

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

#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 main(int argc, char const *argv[])


{
A a1 = A();
a1.change(5);
a1.value_of_a();
return 0;
}

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.

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

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

int main(int argc, char const *argv[])


{
A a1 = A();
A a2 = A();
A a3 = A();
a1.change(10);
a1.value_of_a();
a2.value_of_a();
a3.value_of_a();
return 0;
}

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.

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

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

int main(int argc, char const *argv[])


{
A a1 = A();
a1.change(10);
a1.value_of_a();
return 0;
}

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.

15. Which functions of a class are called inline functions?


a) All the functions containing declared inside the class
b) All functions defined inside or with the inline keyword
c) All the functions accessing static members of the class
d) All the functions that are defined outside the class
View Answer
Answer: b
Explanation: All the functions defined inside the class or functions having inline
keyword before them are inline functions of a class provided they are small and
simple otherwise compiler ignores the request of inline.

1. Which keyword is used to define the user defined data types?


a) def
b) union
c) typedef
d) type
View Answer
Answer: c
Explanation: Typedef is used to define user defined datatypes.
eg:
typedef int INT;
INT a;
here INT is used defined data type.

2. Identify the correct statement.


a) typedef does not create different types. It only creates synonyms of existing types
b) typedef create different types
c) typedef create own types
d) typedef will not creates synonyms of existing types
View Answer
Answer: a
Explanation: By using typedef, we can create a type of pre-existing type only not our
own type of data.

3. What does the data type defined by union will do?


a) It allow one different portion of memory to be accessed as same data types
b) It allow one same portion of memory to be accessed as same data types
c) It allow one different portion of memory to be accessed as different data types
d) It allow one same portion of memory to be accessed as different data types
View Answer
Answer: d
Explanation: Union is used to define the data types of our choice and it will store the
data type in one location make them accessible.
advertisement
4. What will be the output of the following C++ code?

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

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

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

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

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}’.

7. What is the syntax of user-defined data types?


a) typedef ExistingDataType NameByUser
b) typedef NameByUser ExistingDataType
c) def NameByUser ExistingDataType
d) def NameByUser ExistingData
View Answer
Answer: a
Explanation: correct syntax is typedef ExistingDataType NameByUser.
typedef int INT; (typedef existing-datatype New-name;).

8. How many types of user-defined data type are in c++?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: There are three types of user-defined data types. They are typedef,
union, enumerator.

9. What is the scope of typedef defined data types?


a) inside that block only
b) whole program
c) outside the program
d) main function
View Answer
Answer: b
Explanation: We are defining the user-defined data type to be availed only inside
that program, if we want to use anywhere means we have to define those types in
the header file.

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.

1. Where does the object is created?


a) class
b) constructor
c) destructor
d) attributes
View Answer
Answer: a
Explanation: In class, only all the listed items except class will be declared.

2. How to access the object in the class?


a) scope resolution operator
b) ternary operator
c) direct member access operator
d) resolution operator
View Answer
Answer: c
Explanation: Objects in the method can be accessed using direct member access
operator which is (.).

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

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

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

a) recta area: 30 rectb area: 42


b) recta area: 20 rectb area: 34
c) recta area: 30 rectb area: 21
d) recta area: 30 rectb area: 33
View Answer
Answer: a
Explanation: We are calculating the area of rectangle by two objects.

6. Pick out the other definition of objects.


a) member of the class
b) associate of the class
c) attribute of the class
d) instance of the class
View Answer
Answer: d
Explanation: An Object represents an instance of a class i.e. a variable of that class
type having access to its data members and member functions from outside if
allowed.

7. How many objects can present in a single class?


a) 1
b) 2
c) 3
d) as many as possible
View Answer
Answer: d
Explanation: Because a class may contain any number of objects according to its
compliance.

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

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(;).

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

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. Pick the other name of operator function.


a) function overloading
b) operator overloading
c) member overloading
d) object overloading
View Answer
Answer: b
Explanation: Operator function means operation defined for that operator so if user
defines a function for an operator then that is called operator overloading i.e.
overloading already present operator function.

2. Which of the following operators can’t be overloaded?


a) ::
b) +
c) –
d) []
View Answer
Answer: a
Explanation: :: operator cannot be overloaded because this operator operates on
names rather than values and C++ has no syntax for writing codes that works on
names than values so using syntax these operators cannot be overloaded.

3. How to declare operator function?


a) operator sign
b) operator
c) name of the operator
d) name of the class
View Answer
Answer: a
Explanation: We have to declare the operator function by using the operator,
operator sign. Example “operator +” where the operator is a keyword and + is the
symbol need to be overloaded.
advertisement
ADVERTISEMENT
ADVERTISEMENT

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

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

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

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)

Volume of Box1 : 210

Volume of Box2 : 1560

Volume of Box3 : 5400

b)

Volume of Box1 : 200

Volume of Box2 : 1560

Volume of Box3 : 5400

c)

Volume of Box1 : 210

Volume of Box2 : 1550

Volume of Box3 : 5400

d)

Volume of Box1 : 200

Volume of Box2 : 1000

Volume of Box3 : 5260

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

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


1. #include <iostream>
2. using namespace std;
3. class Integer
4. {
5. int i;
6. public:
7. Integer(int ii) : i(ii) {}
8. const Integer
9. operator+(const Integer& rv) const
10. {
11. cout << "operator+" << endl;
12. return Integer(i + rv.i);
13. }
14. Integer&
15. operator+=(const Integer& rv)
16. {
17. cout << "operator+=" << endl;
18. i += rv.i;
19. return *this;
20. }
21. };
22. int main()
23. {
24. int i = 1, j = 2, k = 3;
25. k += i + j;
26. Integer ii(1), jj(2), kk(3);
27. kk += ii + jj;
28. }

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+=

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

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

8. Which of the following statements is NOT valid about operator overloading?


a) Only existing operators can be overloaded
b) The overloaded operator must have at least one operand of its class type
c) The overloaded operators follow the syntax rules of the original operator
d) None of the mentioned
View Answer
Answer: b
Explanation: The overloaded operator must not have at least one operand of its
class type.

9. Operator overloading is ___________


a) making c++ operator works with objects
b) giving new meaning to existing operator
c) making the new operator
d) adding operation to the existing operators
View Answer
Answer: d
Explanation: Operator overloading is the way adding operation to the existing
operators.

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

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

1. Who invented OOP?


a) Andrea Ferro
b) Adele Goldberg
c) Alan Kay
d) Dennis Ritchie
View Answer
Answer: c
Explanation: Alan Kay invented OOP, Andrea Ferro was a part of SmallTalk
Development. Dennis invented C++ and Adele Goldberg was in team to develop
SmallTalk but Alan actually had got rewarded for OOP.

2. Which is not a feature of OOP in general definitions?


a) Efficient Code
b) Code reusability
c) Modularity
d) Duplicate/Redundant data
View Answer
Answer: d
Explanation: Duplicate/Redundant data is dependent on programmer and hence
can’t be guaranteed by OOP. Code reusability is done using inheritance. Modularity
is supported by using different code files and classes. Codes are more efficient
because of features of OOP.

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.

4. When OOP concept did first came into picture?


a) 1980’s
b) 1995
c) 1970’s
d) 1993
View Answer
Answer: c
Explanation: OOP first came into picture in 1970’s by Alan and his team. Later it was
used by some programming languages and got implemented successfully, SmallTalk
was first language to use pure OOP and followed all rules strictly.

5. Which feature of OOP indicates code reusability?


a) Abstraction
b) Polymorphism
c) Encapsulation
d) Inheritance
View Answer
Answer: d
Explanation: Inheritance indicates the code reusability. Encapsulation and
abstraction are meant to hide/group data into one element. Polymorphism is to
indicate different tasks performed by a single entity.
advertisement
6. Which header file is required in C++ to use OOP?
a) OOP can be used without using any header file
b) stdlib.h
c) iostream.h
d) stdio.h
View Answer
Answer: a
Explanation: We need not include any specific header file to use OOP concept in
C++, only specific functions used in code need their respective header files to be
included or classes should be defined if needed.

7. Why Java is Partially OOP language?


a) It allows code to be written outside classes
b) It supports usual declaration of primitive data types
c) It does not support pointers
d) It doesn’t support all types of inheritance
View Answer
Answer: b
Explanation: As Java supports usual declaration of data variables, it is partial
implementation of OOP. Because according to rules of OOP, object constructors
must be used, even for declaration of variables.

8. Which among the following doesn’t come under OOP concept?


a) Data hiding
b) Message passing
c) Platform independent
d) Data binding
View Answer
Answer: c
Explanation: Platform independence is not feature of OOP. C++ supports OOP but
it’s not a platform independent language. Platform independence depends on the
programming language.

9. Which is the correct syntax of inheritance?


a) class base_classname :access derived_classname{ /*define class body*/ };
b) class derived_classname : access base_classname{ /*define class body*/ };
c) class derived_classname : base_classname{ /*define class body*/ };
d) class base_classname : derived_classname{ /*define class body*/ };
View Answer
Answer: b
Explanation: Firstly, keyword class should come, followed by the derived class name.
Colon is must followed by access in which base class has to be derived, followed by
the base class name. And finally the body of class. Semicolon after the body is also
must.

10. Which feature of OOP is indicated by the following code?

class student{ int marks; };


class topper:public student{ int age; topper(int age){ this.age=age; } };

a) Encapsulation and Inheritance


b) Inheritance and polymorphism
c) Polymorphism
d) Inheritance
View Answer
Answer: a
Explanation: Encapsulation is indicated by use of classes. Inheritance is shown by
inheriting the student class into topper class. Polymorphism is not shown here
because we have defined the constructor in the topper class but that doesn’t mean
that default constructor is overloaded.

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.

15. What is encapsulation in OOP?


a) It is a way of combining various data members and member functions that
operate on those data members into a single unit
b) It is a way of combining various data members and member functions into a
single unit which can operate on any data
c) It is a way of combining various data members into a single unit
d) It is a way of combining various member functions into a single unit
View Answer
Answer: a
Explanation: It is a way of combining both data members and member functions,
which operate on those data members, into a single unit. We call it a class in OOP
generally. This feature have helped us modify the structures used in C language to
be upgraded into class in C++ and other languages.

16. Which of the following is not true about polymorphism?


a) Helps in redefining the same functionality
b) Increases overhead of function definition always
c) It is feature of OOP
d) Ease in readability of program
View Answer
Answer: b
Explanation: It never increases function definition overhead, one way or another if
you don’t use polymorphism, you will use the definition in some other way, so it
actually helps to write efficient codes.

17. Which constructor will be called from the object created in the below C++ code?

class A
{
int i;
A()
{
i=0; cout&lt;&lt;i;
}
A(int x=0)
{
i=x; cout&lt;&lt;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.

18. What is an abstraction in object-oriented programming?


a) Hiding the implementation and showing only the features
b) Hiding the important data
c) Hiding the implementation
d) Showing the important data
View Answer
Answer: a
Explanation: It includes hiding the implementation part and showing only the
required data and features to the user. It is done to hide the implementation
complexity and details from the user. And to provide a good interface in
programming.

19. Which among the following can show polymorphism?


a) Overloading &&
b) Overloading <<
c) Overloading ||
d) Overloading +=
View Answer
Answer: b
Explanation: Only insertion operator can be overloaded among all the given options.
And the polymorphism can be illustrated here only if any of these is applicable of
being overloaded. Overloading is type of polymorphism.

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

a) Program will give compile time error


b) Object s3, syntax error
c) Only object s1 and s2 will be created
d) Program runs and all objects are created
View Answer
Answer: d
Explanation: It is a special case of constructor with only 1 argument. While calling a
constructor with one argument, you are actually implicitly creating a conversion
from the argument type to the type of class. Hence you can directly specify the
value of that one argument with assignment operator.

22. The copy constructors can be used to ________


a) Copy an object so that it can be passed to another primitive type variable
b) Copy an object for type casting
c) Copy an object so that it can be passed to a function
d) Copy an object so that it can be passed to a class
View Answer
Answer: c
Explanation: When an object is passed to a function, actually its copy is made in the
function. To copy the values, copy constructor is used. Hence the object being
passed and object being used in function are different.

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.

24. Which among the following represents correct constructor?


a) –classname()
b) classname()
c) ()classname
d) ~classname()
View Answer
Answer: b
Explanation: The constructors must contain only the class name. The class name is
followed by the blank parenthesis or we can have parameters if some values are to
be passed.

25. What happens when an object is passed by reference?


a) Destructor is called at end of function
b) Destructor is called when called explicitly
c) Destructor is not called
d) Destructor is called when function is out of scope
View Answer
26. Which access specifier is usually used for data members of a class?
a) Protected
b) Private
c) Public
d) Default
View Answer
Answer: b
Explanation: All the data members should be made private to ensure the highest
security of data. In special cases we can use public or protected access, but it is
advised to keep the data members private always.

27. How to access data members of a class?


a) Dot, arrow or direct call
b) Dot operator
c) Arrow operator
d) Dot or arrow as required
View Answer
Answer: d
Explanation: The data members can never be called directly. Dot operator is used to
access the members with help of object of class. Arrow is usually used if pointers
are used.

28. Which feature of OOP reduces the use of nested classes?


a) Inheritance
b) Binding
c) Abstraction
d) Encapsulation
View Answer
Answer: a
Explanation: Using inheritance we can have the security of the class being inherited.
The subclass can access the members of parent class. And have more feature than a
nested class being used.

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++.

31. Which of the following is not a property of an object?


a) Properties
b) Names
c) Identity
d) Attributes
View Answer
Answer: b
Explanation: The names are not property of an object. The identity can be in any
form like address or name of object but name can’t be termed as only identity of an
object. The objects contain attributes that define what type of data an object can
store.

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.

33. Which among the following best describes the Inheritance?


a) Using the data and functions into derived segment
b) Using already defined functions in a programming language
c) Using the code already written once
d) Copying the code already written
View Answer
Answer: a
Explanation: It can only be indicated by using the data and functions that we use in
derived class, being provided by parent class. Copying code is nowhere similar to
this concept, also using the code already written is same as copying. Using already
defined functions is not inheritance as we are not adding any of our own features.

34. Single level inheritance supports _____________ inheritance.


a) Language independency
b) Multiple inheritance
c) Compile time
d) Runtime
View Answer
Answer: d
Explanation: The runtime inheritance is done when object of a class is created to call
a method. At runtime the function is searched if it is in class of object. If not, it will
search in its parent classes and hierarchy for that method.

35. How to overcome diamond problem?


a) Using seperate derived class
b) Using virtual keyword with same name function
c) Can’t be done
d) Using alias name
View Answer
Answer: b
Explanation: To overcome the ambiguity and conflict we can use keyword virtual.
This will help us to differentiate the functions with same name that came to last
derived class in diamond problem.

36. Which keyword is used to declare virtual functions?


a) virt
b) virtually
c) virtual
d) anonymous
View Answer
Answer: c
Explanation: The virtual keyword is used to declare virtual functions. Anonymous
keyword is used with classes and have a different meaning. The virtual functions are
used to call the intended function of the derived class.

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.

38. What is friend member functions in C++?


a) Non-member functions which have access to all the members (including private)
of a class
b) Member function which doesn’t have access to private members
c) Member function which can modify any data of a class
d) Member function which can access all the members of a class
View Answer
Answer: a
Explanation: A non-member function of a class which can access even the private
data of a class is a friend function. It is an exception on access to private members
outside the class. It is sometimes considered as a member functions since it has all
the access that a member function in general have.

39. Where is the memory allocated for the objects?


a) Cache
b) ROM
c) HDD
d) RAM
View Answer
Answer: d
Explanation: The memory for the objects or any other data is allocated in RAM
initially. This is while we run a program all the memory allocation takes place in
some RAM segments. Arrays in heap and local members in stack etc.

40. Which of the following best describes member function overriding?


a) Member functions having the same name in derived class only
b) Member functions having the same name and different signature inside main
function
c) Member functions having the same name in base and derived classes
d) Member functions having the same name in base class only
View Answer
Answer: c
Explanation: The member function which is defined in base class and again in the
derived class, is overridden by the definition given in the derived class. This is
because the preference is given more to the local members. When derived class
object calls that function, definition from the derived class is used.

41. Encapsulation and abstraction differ as ____________


a) Hiding and hiding respectively
b) Binding and Hiding respectively
c) Hiding and Binding respectively
d) Can be used any way
View Answer
Answer: b
Explanation: Abstraction is hiding the complex code. For example, we directly use
cout object in C++ but we don’t know how is it actually implemented. Encapsulation
is data binding, as in, we try to combine a similar type of data and functions
together.

42. Which feature of OOP is exhibited by the function overriding?


a) Polymorphism
b) Encapsulation
c) Abstraction
d) Inheritance
View Answer
Answer: a
Explanation: The polymorphism feature is exhibited by function overriding.
Polymorphism is the feature which basically defines that same named functions can
have more than one functionalities.
43. How to access the private member function of a class?
a) Using class address
b) Using object of class
c) Using object pointer
d) Using address of member function
View Answer
Answer: d
Explanation: Even the private member functions can be called outside the class. This
is possible if address of the function is known. We can use the address to call the
function outside the class.

44. Which keyword should be used to declare static variables?


a) const
b) common
c) static
d) stat
View Answer
Answer: c
Explanation: The keyword used to declare static variables is static. This is must be
used while declaring the static variables. The compiler can make variables static if
and only if they are mentioned with static keyword.

45. Which is correct syntax for declaring pointer to object?


a) *className objectName;
b) className* objectName;
c) className objectName();
d) className objectName;
View Answer
Answer: b
Explanation: The syntax must contain * symbol after the className as the type of
object. This declares an object pointer. This can store address of any object of the
specified class.

46. Which class/set of classes can illustrate polymorphism in the following C++
code?

abstract class student


{
public : int marks;
calc_grade();
}
class topper:public student
{
public : calc_grade()
{
return 10;
}
};
class average:public student
{
public : calc_grade()
{
return 20;
}
};
class failed{ int marks; };

a) Only class student and topper together can show polymorphism


b) Only class student can show polymorphism
c) Class failed should also inherit class student for this code to work for
polymorphism
d) All class student, topper and average together can show polymorphism
View Answer
Answer: d
Explanation: Since Student class is abstract class and class topper and average are
inheriting student, class topper and average must define the function named
calc_grade(); in abstract class. Since both the definition are different in those classes,
calc_grade() will work in different way for same input from different objects. Hence
it shows polymorphism.

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.

49. Object being passed to a copy constructor ___________


a) Must not be mentioned in parameter list
b) Must be passed with integer type
c) Must be passed by value
d) Must be passed by reference
View Answer
Answer: d
Explanation: This is mandatory to pass the object by reference. Otherwise, the
object will try to create another object to copy its values, in turn a constructor will be
called, and this will keep on calling itself. This will cause the compiler to give out of
memory error.

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.

51. Instance of which type of class can’t be created?


a) Parent class
b) Abstract class
c) Anonymous class
d) Nested class
View Answer
Answer: b
Explanation: Instance of abstract class can’t be created as it will not have any
constructor of its own, hence while creating an instance of class, it can’t initialize the
object members. Actually the class inheriting the abstract class can have its instance
because it will have implementation of all members.

52. ___________ underlines the feature of Polymorphism in a class.


a) Virtual Function
b) Inline function
c) Enclosing class
d) Nested class
View Answer
Answer: a
Explanation: Virtual Functions can be defined in any class using the keyword virtual.
All the classes which inherit the class containing the virtual function, define the
virtual function as required. Redefining the function on all the derived classes
according to class and use represents polymorphism.

53. Which feature in OOP is used to allocate additional functions to a predefined


operator in any language?
a) Function Overloading
b) Function Overriding
c) Operator Overloading
d) Operator Overriding
View Answer
Answer: c
Explanation: The feature is operator overloading. There is not a feature named
operator overriding specifically. Function overloading and overriding doesn’t give
addition function to any operator.

54. Which feature can be implemented using encapsulation?


a) Polymorphism
b) Overloading
c) Inheritance
d) Abstraction
View Answer
Answer: d
Explanation: Data abstraction can be achieved by using encapsulation. We can hide
the operation and structure of actual program from the user and can show only
required information by the user.

You might also like