0% found this document useful (0 votes)
123 views

C++Q

The document contains 37 multiple choice questions related to C++ programming concepts like functions, loops, arrays, pointers, classes etc. The questions test understanding of basic syntax, flow control, I/O operations and object oriented programming concepts in C++.

Uploaded by

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

C++Q

The document contains 37 multiple choice questions related to C++ programming concepts like functions, loops, arrays, pointers, classes etc. The questions test understanding of basic syntax, flow control, I/O operations and object oriented programming concepts in C++.

Uploaded by

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

1. What will be the output of the following program?

#include <iostream>
using namespace std;
int main()
{
int x, y;
x = 5;
x++ * ++x;
cout <<x << " " <<y;
return 0;
}
Select one:
o 30
o6 0
o7 0
o 36

2. If a function does not have return type, it is declared as _____________.


Select one:
o void
o double
o long
o int

3. What will be the output of the following program?


#include <iostream>
using namespace std;
int main()
{
int i = 3;
int l = i / -2;
int k = i % -2;
cout << l << " " << k;
return 0;
}
Select one:
o -1.5 -1
o -1 -1
o -1 1
o -1.5 -1.97

4. Which of the following variable can store the address of a variable?


Select one:
o float variable
o string variable
o Pointer variable
o int variable
5. What is the starting index of a matrix or array?
Select one:
o0
o true
o1
o size of array

6. What will be the output of the following program?


#include <iostream>
using namespace std;
int main()
{
int i = 1;
int *ptr = &i;
cout << *ptr;
return 0;
}
Select one:
o1
o0
o address of ptr
o address of i

7. What will be the output of the following program?


#include <iostream>
using namespace std;
int main()
{
int i, j;
j = 10;
i = (j++, j + 100, 999 + j);
cout << i;
return 0;
}
Select one:
o 11, 110, 1009
o 11
o 10
o 1010

8. What will be the output of the following program?


#include <iostream>
using namespace std;
int g = 100;
int main()
{
int a;
{
int b;
b = 20;
a = 35;
g = 65;
a = 50;
cout <<b << " " << a <<" " << g;
}
return 0;
}
Select one:
Compilation error
o 20 50 65
o 20 50 100
o 20 35 100
o 11, 111, 1010

9. What will be the output of the following program?


#include <iostream>
using namespace std;
int main ()
{
int array[] = {0, 2, 4, 6, 7, 5, 3};
int n, result = 0;
for (n = 0 ;n < 5 ;n++)
{
result += billy[n];
}
cout << result;
return 0;
}
Select one:
Compilation error
o 19
o 24
o0

10. Which of the following variables is enclosed in single quotes?


Select one:
o float variable
o character variable
o None of these
o int variable

11. Which of the following access specifies is used in a class definition by default?
Select one:
o Protected
o Friend
o Public
o Private

12. Which of the following statements is correct, when a class is inherited publicly?
Select one:
o Public members of the base class become protected members of derived class.
o Public members of the base class become public members of derived class.
o Private members of the base class become protected members of derived class.
o Public members of the base class become private members of derived class.

13. Which of the following accesses the 7th element stored in array?
Select one:
o array[6];
o array[7];
o array=7;
o array[8];

14. Which of the following symbols acts as a statement terminator?


Select one:
)
;
{
Return 0

15. What is the break statement used for?


Select one:
o To add two numbers
o To pass arguments
o All of these
o To interrupt the normal flow of control of a program

16. Which of the following signifies new line?


Select one:
o \n
o \r
o \a
o \s

17. Which of the following is used to display a single character from a specified file on the screen?
[email protected]
Select one:
o putchar
o None of these
o getchar
o cin>>

18. Which of the following operator is used to get value stored at an address that is pointed by a pointer.
Select one:
o &(ampersand)
o .(dot)
o ~(tilde)
o *(asterisk)

19. Which of the following is true for switch statement in C++?


Select one:
o We can put a range for cases such as case 1..3
o None of the these
o We need to put break statement at the end of the group of statements of a condition
o It uses labels instead of blocks

20. What is the command to compile a C++ program named welcome.cpp?


Select one:
o t++ welcome
o g++ welcome.cppwelcome.cpp
o g++ welcme.c
o welcome.cpp

21. What will be the output of the following program?


#include <iostream>
using namespace std;
int main()
{
int a = 8;
cout << "Integer 'a' with 'true' :" << a && true;
return 0;
}
Select one:
o Integer 'a' with 'true' :8
o integer ‘a’ with ‘true’ :1
o Compilation Error
o Integer ‘a’ with ‘true’ :0

22. Point out the error line in the following program.


1 #include <iostream>
2 using namespace std;
3 void main()
4 {
5 long int num = 10;
6 long int *ptr;
7 cout<<"num's address :"<< &num<<"\n";
8 ptr = &num;
9 cout<<"pointer's address :"<< &ptr<<"\n";
10 cout<<"pointer's size bytes "<< sizeof(ptr)<<"\n";
11 cout<<"pointer's value: "<< ptr<<"\n";
12 cout<<"value pointed to: "<< *ptr<<"\n";
13 return 0;
14 }
Select one:
o Line number 10
o Line number 12
o Line number 3
o Line number 5

23. Point out the error line in the following program.


1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5 int x=0;
6 int y=0;
7 while(x<=10);
8 {
9 y=y+x;
10 cout<<y<<"\n";
11 x++;
12 }
13 return 0;
14 }
Select one:
o Line number 11
o Line number 5
o Line number 7
o No error

24. To increase the value of c by one, which of the following statement should not be used?
Select one or more:
o c--;
o c-=1;
o c=c+1
o c+1 => c;

25. Which of the following is the correct syntax for functions.


Select one or more:
o ret-type fun-name(parameters);
o fun-name(parameters);
o ret-type fun-name( )
o ret-type fun-name(parameters)

26. Which of the following statement is correct?


Select one or more:
o None of these
o Constructor has the same name as that of the class.
o Destructor has the same name as the first member function of the class.
o Destructor has the same name as that of the class with a tilde symbol at the beginning.

27. Select the missing code lines from the dropdown provided.
The C++ code given below should find the smallest number out of the five numbers entered by the user.
But, line numbers 7 and 16 are missing.
1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5. int num[5];
6. cout << "Enter five integers\n";
7. ---------------Missing code -------------------
8. {
9. cin >> num[i];
10. }
11. int smallest = num[0];
12. for(int i=1; i<=5; i++)
13. {
14. if(num[i]<smallest)
15. {
16. ---------------Missing code -------------------
17. }
18. }
19. cout << "Smallest number is " << smallest << "\n";
20. return 0;
21. }

 Line 7
 Line 16

28. Select the missing code lines from the dropdown provided.
The C++ code given below should find the area of circle. Radius of the circle is entered by user.
But, line numbers 7 and 10 are missing.
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 int r;
6 float pi = 3.14;
7 ---------------Missing code -------------------
8 cout <<"Enter the radius of the circle\n";
9 cin >>r;
10 ---------------Missing code -------------------
11 cout <<"Area of the circle is " <<area << "\n";
12 return 0;
13 }

 Line 7
 Line 11

29. Select the missing code lines from the dropdown provided.
The C++ code given below should find sum of two numbers entered by user.
But, line numbers 3 and 10 are missing.
1 #include <iostream>
2 using namespace std;
3 ---------------Missing code -------------------
4 {
5 int c = a + b;
6 cout<<"Sum of a and b is "<<c<<"\n";
7 }
8 int main()
9 {
10 ---------------Missing code -------------------
11 sum = add(5,4);
12 return 0;
13 }

 Line 3 - Answer
 Line 10 - Answer

30. What will be the output of the following program? Write the answer in digits not alphabet.
#include<iostream>
using namespace std;
void MyFunction(int a, int b = 40)
{

cout << a << " " << b;


}
int main()
{
MyFunction(20, 30);
return 0;
}

Answer:

31. Which of the following is true for switch statement in C++?


Select one:
o We can put a range for cases such as case 1..3
o It uses labels instead of blocks
o We need to put break statement at the end of the group of statements of a condition
o None of the these

32. Which is the condition where no more data can be read from a data source?
Select one:
o switch condition
o for condition
o EOF(end of file)
o if condition

33. Which of the following is used to accept a character from a file?


Select one:
o putstring
o printf();
o getchar
o None of these

34. What will be the output of the following program?


#include <iostream>
using namespace std;
int main()
{
char str[10] = "ICECREAM";
cout << str[3];
cout << str;
return 0;
}
Select one:
o ICECREAM
o Compilation error
o ICE
o CICECREAM

35. Which of the following two entities (reading from left to right) can be connected by the dot operator?
Select one:
o A class and a member of that class.
o A class member and a class object.
o A class object and a class.
o A class object and a member of that class.

36. What will be the output of the following program?


#include <iostream>
using namespace std;
int main ()
{
int x, y;
x = 5;
cout <<++x * ++x;
return 0;
}
Select one:
o 49
o 30
o 36
o 25

37. What will be the output of the following program? Write the answer in digits not alphabet.
#include<iostream>
using namespace std;
long FactFinder(long = 5);
int main()
{
for(int i = 0; i<= 0; i++)
cout<< FactFinder() << endl;
return 0;
}
long FactFinder(long x)
{
if(x < 2)
return 1;
long fact = 1;
for(long i = 1; i <= x-1; i++)
fact = fact * i;
return fact;
}

 Answer:

38. What does the return 0; statement in main function indicate?


Select one:
o The program worked as expected without any errors during its execution
o Do not end the program yet
o The program did nothing, completed 0 tasks
o None of these

39. What is the output of the following program?


#include <iostream>
using namespace std;
int main()

{
double a = 21.09399;
float b = 10.20;
int c ,d;
c = (int) a;
d = (int) b;
cout << c <<" " << d;
return 0;
}
Select one:
o Compilation error
o 21.09399 10.20
o 21 10
o 0

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


#include <iostream>
using namespace std;
int array1[] = {1200, 200, 2300, 1230, 1543};
int array2[] = {12, 14, 16, 18, 20};
int temp, result = 0;
int main()
{
for (temp = 0; temp < 4; temp++)
{
result += array1[temp];
}
for (temp = 0; temp < 5; temp++)
{
result += array2[temp];
}
cout << result;
return 0;
}

Select one:

o None of these
o 5010
o 4990
o 6533

41. What will be the output of the following program?


#include <iostream>
using namespace std;
int main ()
{
int array[] = {0, 2, 4, 6, 7, 5, 3};
int n, result = 0;
for (n = 0 ;n < 5 ;n++)
{
result += billy[n];
}
cout << result;
return 0;
}
Select one:
o Compilation error
o 19
o 0
o 24

42. Which of the following functions is used to read data from the console?
Select one:
o scanf();
o main()
o cout <<
o cin>>;

43. Which of the following variables is enclosed in single quotes?


Select one:
o None of these
o character variable
o int variable
o float variable

44. Which format specifier will be used to provide precision of 4 digits after decimal point?
Select one:
o %.4f
o .%4f
o %f4
o %4f

45. The region of code within which a variable can be accessed is known as _______________.
Select one:
o Arguments
o Scope of variable
o Functions
o Token

46. Which of the following is used to display a single character from a specified file on the screen?
[email protected]
Select one:
o getchar
o None of these
o cin>>
o putchar

47. Which format specifier will be used to provide precision of 4 digits after decimal point?
Select one:
o %4f
o %f4
o .%4f
o %.4f
48. Which format specifier will be used to provide precision of 4 digits after decimal point?
Select one:
o %4f
o %f4
o .%4f
o %.4f

49. Point out the error line in the following program.


1 using namespace std;
2 int add(int a, int b)
3 {

4 int c = a + b;
5 cout<<"Sum of a and b is "<<c<<"\n";
6 }
7 int main()
8 {
9 int sum;
10 sum = add(5);
11 return 0;
12 }
Select one:
Line number 5
o No error
o Line number 1
o Line number 2
o Line number 10

50. Point out the error line in the following program.


1 #include <iostream>
2 using namespace std;
3 int a=5;
4 int b=2;
5 void add()
6 {
7 int sum;
8 sum = a + b;
9 cout <<"Sum of a and b is "<<sum<<"\n";
10 }
11 int main()
12 {
13 add;
14 return 0;
15 }
Select one:
o Line number 14
o Line number 13
o Line number 8
o Line number 5

51. Which of the following statement is correct?


Select one or more:
o Constructor has the same name as that of the class.
o None of these
o Destructor has the same name as that of the class with a tilde symbol at the beginning.
o Destructor has the same name as the first member function of the class.

52. Based on the below definition statements, which of the following is/are not correct options?
int x;
float f;
int *pi;
float *pf;
Select one or more:
 pi=x;
 pi=*x;
 x=a;
 pi=&x;

53. Which of the following declarations are legal?


Select one or more:
o char str = “hello”;
o void *ptr;
o char *str = “hello”;
o const *int p1

54. Select the missing code lines from the dropdown provided.
The C++ code given below should find the cube of a number entered by the user.
But, line numbers 2 and 8 are missing.
1. #include<iostream>
2. ---------------Missing code -------------------
3. int main()
4. {
5. int num;
6. cout << "Enter an integers\n";
7. cin >> num;
8. ---------------Missing code -------------------
9. cout << "Cube of " << num << " is "<< cube << "\n";
10. return 0;
11. }

o Line 2
o Line 8
55. Select the missing code lines from the dropdown provided.
The C++ code given below has class that should give dimensions and volume of the Box.
But, line numbers 20 and 26 are missing.
#include<iostream>
using namespace std;
class Box
{
public:
double length;
double breadth;
double height;
void dimension(double l, double b, double h)
{
length = l;
breadth = b;
height = h;
cout << "Length is " << length << "\n";
cout << "Breadth is " << breadth << "\n";
cout << "height is " << height << "\n";
}
double volume()
{
---------------Missing code -------------------
}
};
int main()
{
Box b;
---------------Missing code -------------------
cout << "Volume is "<< b.volume() << "\n";
return 0;
}

o Line 20
o Line 26

56. Select the missing code lines from the dropdown provided.

The C++ code given below should find the sum of the ten numbers entered by the user.

But, line numbers 9 and 12 are missing.

1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5. int num[10];
6. cout << "Enter ten integers\n";
7. for(int i=0; i<10; i++)
8. {
9. ---------------Missing code -------------------
10. }
11. int sum=0;
12. ---------------Missing code -------------------
13. {
14. sum = sum + num[i];
15. }
16. cout << "Sum is " << sum << "\n";
17. return 0;
18. }
o Line 9
o Line 12

57. What will be the output of the following program? Write the answer in digits not alphabet.
#include<iostream>
using namespace std;
int main(){
int i;
for(i=0;i<=5;i++);
cout<<i;
return 0;
}
Answer:

58. What will be the output of the following program? Write the answer in digits not alphabet.
#include<iostream>
using namespace std;
class Base
{
public:
int S, A, M;
Base(int x, int y)
{
S = y - y;
A = x + x;
M = x * x;
}
Base(int, int y = 'A', int z = 'B')
{
S = y;
A = y + 1 - 1;
M = z - 1;
}
void Display(void)
{
cout<< S << " " << A << " " << M << endl;
}
};
class Derived : public Base
{
int x, y, z;
public:
Derived (int xx = 65, int yy = 66, int zz = 67): Base(x)
{
x = xx;
y = yy;
z = zz;
}
void Display(int n)
{

if(n)
Base::Display ();
else
cout<< x << " " << y << " " << z << endl;
}
};
int main()
{
Derived objDev;
objDev.Display (-1);
Return 0;
}

Answer:

You might also like