Home / Interview / “C++” Interview Question

“C++” Interview Question

1. Which of the following correctly declares an array and why?

A. int array[10]; B. int array; C. array{10}; D. array array[10];

->A.int array[10];Before the declaration of array variable the datatype of the array should be declared.Here ‘int’ indicates the integer datatype of the array variable.

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

->B. 8 Every array starts with 0th position. So, if we count the last index position of the 9th element of the array should be in 8.

3. What is an array?

->An array is a series of elements of the same type in contiguous memory locations. An array always stores homogenious data. We can not put a character and  an integer value in the same array.

4. What is the output of this program?#include < stdio.h >using namespace std;int main(){char str[5] = “ABC”;cout << str[3];cout << str;return 0;}
->Answer: It will print ‘ABC’.Because we are just printing the values of first 3 values.

5. What is the output of this program?#include < stdio.h >using namespace std;int main(){int array[] = {10, 20, 30};cout << -2[array];return 0;}

->It will print ‘-30’ as output. Because ‘-2’ reffers the position of the index value preceeded by a negetive sign.

6. Where does a cin stops it extraction of data?A. By seeing a blankspace B. By seeing () C. Both a & b D. None of the mentioned

-> A.By seeing a blankspace

7. What is the output of this program?#include < iostream >using namespace std;int main (){int i;cout << “Please enter an integer value: “;cin >> i + 4;return 0;}

-> An error will be occurred. We are not allowed to do any arithmetic operation on cin.

8. What can be used to input a string with blankspace?

->If a user wants to input a sentence with blankspaces, then he may use the function getline.getline function can take multiple lines.

9. What is meant by ofstream in c++?

->ofstream is a stream class to write on files.

10. How many types of output stream classes are there in c++?

->There are three output stream classes in c++. They are ostream, ofstream and ostrstream.

11.What is the output of this program?
#include < iostream >using namespace std;int main () {char str[] = “Steve jobs”;int val = 65;char ch = ‘A’;cout.width (5);cout << right;cout << val << endl;return 0;}

->In this program, We are printing the five spaces and then we are printing the value of 65.

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

->The answer is C.pointer to function taking a char* argument and returns an int.

13. The operator used for dereferencing or indirection is ____A. * B. & C. -> D. ->>

->A. *

14. What do you mean by this statement- string* x, y;

->Here two variables are declared.The first variable x is a pointer to a string and the second one y is a simple string variable.
15. 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.

->The answer is option D.A pointer can be in only 3 states a,b and c but can not point to a datatype.

15. Which of the following is illegal and why?A. int *ip; B. string s, *sp = 0; C. int i; double* dp = &i; D. int *pi = 0;

->The answer is option C. A double in datatype pointer variable can not be intialized with an integer value.

16.What will happen in this code?     int a = 100, b = 200;     int *p = &a, *q = &b;     p = q;

->p will point to b as *q holds the address of variable b.

17. What is the output of this program?#include < iostream >using namespace std;int main(){int a = 5, b = 10, c = 15;int *arr[ ] = {&a, &b, &c};cout << arr[1];return 0;}

->It will return some random number.Array element cannot be address of auto variable. It can be address of static or extern variables.

18.What is the output of this program?#include < iostream >using namespace std;int main(){char *ptr;char Str[] = “abcdefg”;ptr = Str;ptr += 5;cout << ptr;return 0;}

->It will print ‘fg’ on the monitor screen because pointer ptr points to string ‘fg’.

19. What is meaning of following declaration?int(*p[5])();

->p is an array of pointer to a function here. The variable p is an array not the pointer in this statement.

20. Void pointer can point to which type of objects,why?

->A ‘void’ pointer can point to any type of objects wheather it is int or float or double. Void pointer doen’t know the type of the object it is printing to. It can point to all types of object.

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

->A void pointer is used to cast a variable only. So we can not perform pointer arithematic in a void pointer.

22. Which looping process is best used when the number of iterations is known?

->A for loop.

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

->The answer is option A.

24. Which class is used to design the base class?

->An abstract class.

25. Which is also called as abstract class?

->Classes that contain at least one pure virtual function are called as abstract base classes.

26. Pick out the correct option.A. We cannot make an instance of an abstract base class B. We can make an instance of an abstract base class C. Both a & b D. None of the mentioned

->The correct answer is A. We cannot make an instance of an abstract base class

27. Pick out the correct statement and explain.A. A derived class’s constructor cannot explicitly invokes its base class’s constructor. B. A derived class’s destructor cannot invoke its base class’s destructor. C. A derived class’s destructor can invoke its base class’s destructor.

->The answer is B.Destructors are automatically invoked when a object goes out of scope or when a dynamically allocated object is deleted. Inheritance does not change this behavior. This is the reason a derived destructor cannot invoke its base class destructor.

28. What is the use of clog?

->clog is an object of class ostream that represents the standard logging stream. It is associated with the cstdio stream stderr, like cerr.

29. What is similar to interface in c++A. methods B. instance of class C. pure abstract class D. none of the mentioned

->The answer is C.pure abstract class. Like an interface a pure abstract class also don’t have the defination of its members.

30. By which we can implement the module in a program?
->We can include the group of code by using the #include header file.

31. How many types of constructor are there in C++?

->There are three types of constructor in C++. They are Default constructor, Parameterized constructor, Copy constructor.
32. How many constructors can present in a class?

->There can be multiple constructors of the same class, provided they have different signatures i.e. the different number of passing parameter through the constructor.

33. What is meant by polymorphism?

->Polymirphism is literally means class having many forms.’Poly’ means many and ‘morph’ means forms.

34. How the constants are declared?

->By using ‘const’ keyword and #define preprocessor where the const will declare with a specific type values and #define is used to declare user definied constants.

35. What advantages we lose by using multiple inheritance?
->Dynamic binding and polymorphism.The benefit of dynamic binding and polymorphism is that they help making the code easier to extend but by multiple inheritance it makes harder to track.

About Shanku

Check Also

‘ Do you prefer to work in night shifts?’ answered By Expert & Successful Candidates

‘ Do you prefer to work in night shifts?’ answered By Expert & Successful Candidates …

Leave a Reply

Your email address will not be published. Required fields are marked *