CPP
CPP
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.
advertisement
2. What is C++?
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”.
a) /* comment */
b) // comment */
c) // 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.
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
a) VAR_1234
b) $var_name
c) 7VARNAME
d) 7var_name
View Answer
Answer: a
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.
b) C++ technique to ensure that a private member of the base class can be accessed somehow
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.
advertisement
10. What happens if the following C++ statement is compiled and executed?
delete ptr;
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.
#include <iostream>
#include <string>
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.
advertisement
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
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.
#include <stdio.h>
int main(void)
int new = 5;
printf("%d", new);
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.
advertisement
#include <stdio.h>
void func(void)
printf("Hello");
void main()
func();
func(2);
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.
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.
#include <iostream>
#include <string>
#include <algorithm>
int main()
a) spacesintext
b) spaces in text
c) spaces
d) spaces in
View Answer
Answer: a
Output:
$ g++ dan.cpp
$ a.out
spacesintext
17. Which of the following C++ code will give error on compilation?
================code 1=================
#include <iostream>
cout<<"Hello World";
return 0;
========================================
================code 2=================
#include <iostream>
std::cout<<"Hello World";
return 0;
========================================
a) Code 1 only
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.
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.
int main()
int p;
bool a = true;
bool b = false;
int x = 10;
int y = 5;
p = ((x | y) + (a + b));
cout << p;
return 0;
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.
int main()
register int i = 1;
return 0;
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.
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.
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.
#include<iostream>
int main ()
int cin;
a) Segmentation fault
b) Nothing is printed
c) Error
View Answer
Answer: d
Explanation: cin is a variable hence overrides the cin object. cin >> cin has no meaning so no error.
View Answer
Answer: c
Explanation: To distinguish between different parts of the program like comments, codes, etc.
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.
#include <iostream>
#include <string>
#include <cstring>
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
#include <iostream>
int main()
char c = 74;
cout << c;
return 0;
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.
#include <iomanip>
#include <iostream>
int main()
double d = 0.1;
return 0;
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:
---------------------------
a) A->value
b) A^value
c) A.value
d) A::value
View Answer
Answer: d
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.
b) Overloading of classes
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.
#include <iostream>
int a = 5;
float b;
cout << a;
return 0;
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
45
36. Which of the following symbol is used to declare the preprocessor directives in C++?
a) $
b) ^
c) #
d) *
View Answer
Answer: c
#include<iostream>
int main()
int a = 5;
a = 10;
};
check();
cout<<"Value of a: "<<a<<endl;
return 0;
a) Segmentation fault
b) Value of a: 5
c) Value of a: 10
d) Error
View Answer
Answer: d
Explanation: As this lambda expression is capturing the extrenal variable by value therefore the value of
a cannot be changes inside the lambda expression hence the program gives error.
#include <iostream>
*x = (*x) * --(*y);
int main ( )
square(&number, &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.
View Answer
Answer: c
#include <iostream>
#include <string>
int main ()
str.back() = '!';
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.
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.
#include <iostream>
int main()
int n = 5;
void *p = &n;
a) 5
b) 6
d) runtime error
View Answer
Answer: a
Output:
$ g++ poi1.cpp
$ a.out
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
View Answer
Answer: d
Explanation: If a programmer does not define the above constructors in a class the C++ compiler by
default provides these constructors to avoid error on basic operations.
#include <iostream>
int main()
try
try
throw 20;
catch (int n)
throw;
}
catch (int x)
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?
#include <iostream>
return (a * b);
return (a / b);
int main()
int x = 5, y = 2;
return 0;
a) 10.0 5
b) 10 2.5
c) 10.0 5.0
d) 5.0 2.5
View Answer
Answer: b
Output:
$ g++ over3.cpp
$ a.out
10 2.5
b) In Structures, members are public by default whereas, in Classes, they are private by default
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.
#include <iostream>
int main ()
int a, b, c;
a = 2;
b = 7;
c = (a > b) ? a : b;
cout << c;
return 0;
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
50. What is the benefit of c++ input and output over c input and output?
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?
#include <stdio.h>
#include<iostream>
int main ()
int n, result = 0;
result += array[n];
return 0;
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.
#include <iostream>
#include <string>
int main ()
return 0;
a) runtime error
b) Sanfo
c) S
d) Sanfoundry
View Answer
Answer: a
Explanation: This program will terminate because the cout element is out of range.
#include <iostream>
class A{
public:
A(){
cout<<"Constructor called\n";
~A(){
cout<<"Destructor called\n";
};
A *a = new A[5];
delete[] a;
return 0;
a) Segmentation fault
b) “Constructor called” five times and then “Destructor called” five times
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.