Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Multiple choice question for engineering

Set 1

1. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *str = "hello, worldn";
  5.         char *strc = "good morningn";
  6.         strcpy(strc, str);
  7.         printf("%sn", strc);
  8.         return 0;
  9.     }

a) hello, world
b) Crash/segmentation fault
c) Undefined behaviour
d) Run time error

View Answer

Answer: b

2. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strc[] = "good morning indian";
  6.         strcpy(strc, str);
  7.         printf("%sn", strc);
  8.         return 0;
  9.     }

a) hello world
b) hello worldg india
c) Compile time error
d) Undefined behaviour

View Answer

Answer: a

3. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *str = "hello, world!!n";
  5.         char strc[] = "good morningn";
  6.         strcpy(strc, str);
  7.         printf("%sn", strc);
  8.         return 0;
  9.     }

a) hello, world!!
b) Compile time error
c) Undefined behaviour
d) Segmenation fault

View Answer

Answer: c

4. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *str = "hello, worldn";
  5.         str[5] = '.';
  6.         printf("%sn", str);
  7.         return 0;
  8.     }

a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault

View Answer

Answer: d

5. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char str[] = "hello, world";
  5.         str[5] = '.';
  6.         printf("%sn", str);
  7.         return 0;
  8.     }

a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault

View Answer

Answer: a

6. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strary[] = "hello world";
  6.         printf("%d %dn", sizeof(str), sizeof(strary));
  7.         return 0;
  8.     }

a) 11 11
b) 12 12
c) 4 12
d) 4 11

View Answer

Answer: c

7. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strary[] = "hello world";
  6.         printf("%d %dn", strlen(str), strlen(strary));
  7.         return 0;
  8.     }

a) 11 11
b) 12 11
c) 11 12
d) x 11 where x can be any positive integer.

View Answer

Answer: a

8. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void f(char *k)
  3.     {
  4.         k++;
  5.         k[2] = 'm';
  6.         printf("%cn", *k);
  7.     }
  8.     void main()
  9.     {
  10.         char s[] = "hello";
  11.         f(s);
  12.     }

a) l
b) e
c) h
d) o

View Answer

Answer: b

9. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void fun(char *k)
  3.     {
  4.         printf("%s", k);
  5.     }
  6.     void main()
  7.     {
  8.         char s[] = "hello";
  9.         fun(s);
  10.     }

a) hello
b) Run time error
c) Nothing
d) h

View Answer

Answer: a

Set 2

1. Which class is used to design the base class?
a) abstract class
b) derived class
c) base class
d) none of the mentioned

View Answer

Answer: a

2. Which is used to create a pure virtual function ?
a) $
b) =0
c) &
d) !

View Answer

Answer: b [Reason:] For making a method as pure virtual function, We have to append ‘=0’ to the class or method.

3. Which is also called as abstract class?
a) virtual function
b) pure virtual function
c) derived class
d) none of the mentioned

View Answer

Answer: b [Reason:] Classes that contain at least one pure virtual function are called as abstract base classes.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class p 
  4.     {
  5.         protected:
  6.         int width, height;
  7.         public:
  8.         void set_values (int a, int b)
  9.         {
  10.             width = a; height = b; 
  11.         }
  12.         virtual int area (void) = 0;
  13.     };
  14.     class r: public p
  15.     {
  16.         public:
  17.         int area (void)
  18.         { 
  19.             return (width * height);
  20.         }
  21.     };
  22.     class t: public p 
  23.     {
  24.         public:
  25.         int area (void)
  26.         {
  27.             return (width * height / 2); 
  28.         }
  29.     };
  30.     int main () 
  31.     {
  32.         r rect;
  33.         t trgl;
  34.         p * ppoly1 = &rect;
  35.         p * ppoly2 = &trgl;
  36.         ppoly1->set_values (4, 5);
  37.         ppoly2->set_values (4, 5);
  38.         cout << ppoly1 -> area() ;
  39.         cout << ppoly2 -> area();
  40.         return 0;
  41.     }

a) 1020
b) 20
c) 10
d) 2010

View Answer

Answer: d [Reason:] In this program, We are calculating the area of rectangle and triangle by using abstract class. Output: $ g++ abs.cpp $ a.out 2010

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class MyInterface 
  4.     {
  5.         public:
  6.         virtual void Display() = 0;
  7.     };
  8.     class Class1 : public MyInterface 
  9.     {
  10.         public:
  11.         void Display() 
  12.         {
  13.             int  a = 5;
  14.             cout << a;
  15.         }
  16.     };
  17.     class Class2 : public MyInterface 
  18.     {
  19.         public:
  20.         void Display()
  21.         {
  22.             cout <<" 5" << endl;
  23.         }
  24.     };
  25.     int main()
  26.     {
  27.         Class1 obj1;
  28.         obj1.Display();
  29.         Class2 obj2;
  30.         obj2.Display();
  31.         return 0;
  32.     }

a) 5
b) 10
c) 5 5
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are displaying the data from the two classes by using abstract class. Output: $ g++ abs1.cpp $ a.out 5 5

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         public:
  6.         virtual void example() = 0; 
  7.     };
  8.     class Ex1:public sample
  9.     {
  10.         public:
  11.         void example()
  12.         {
  13.             cout << "ubuntu";
  14.         }
  15.     };
  16.     class Ex2:public sample
  17.     {
  18.         public:
  19.         void example()
  20.         {
  21.             cout << " is awesome";
  22.         }
  23.     };
  24.     int main()
  25.     {
  26.         sample* arra[2];
  27.         Ex1 e1;
  28.         Ex2 e2;
  29.         arra[0]=&e1;
  30.         arra[1]=&e2;
  31.         arra[0]->example();
  32.         arra[1]->example();
  33.     }

a) ubuntu
b) is awesome
c) ubuntu is awesome
d) none of the mentioned

View Answer

Answer: c [Reason:] In this program, We are combining the two statements from two classes and printing it by using abstract class. Output: $ g++ abs3.cpp $ a.out ubuntu is awesome

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         public:
  6.         virtual void print() const = 0;
  7.     };
  8.     class DerivedOne : virtual public Base
  9.     {
  10.         public:
  11.         void print() const
  12.         {
  13.             cout << "1";
  14.         }
  15.     };
  16.     class DerivedTwo : virtual public Base
  17.     {
  18.         public:
  19.         void print() const
  20.         {
  21.             cout << "2";
  22.         }
  23.     };
  24.     class Multiple : public DerivedOne, DerivedTwo
  25.     {
  26.         public:
  27.         void print() const
  28.         {
  29.             DerivedTwo::print();
  30.         }
  31.     };
  32.     int main()
  33.     {
  34.         Multiple both;
  35.         DerivedOne one;
  36.         DerivedTwo two;
  37.         Base *array[ 3 ];
  38.         array[ 0 ] = &both;
  39.         array[ 1 ] = &one;
  40.         array[ 2 ] = &two;
  41.         for ( int i = 0; i < 3; i++ )
  42.         array[ i ] -> print();
  43.         return 0;
  44.     }

a) 121
b) 212
c) 12
d) none of the mentioned

View Answer

Answer: b [Reason:] In this program, We are executing these based on the condition given in array. So it is printing as 212. Output: $ g++ abs4.cpp $ a.out 212

8. What is meant by pure virtual function?
a) Function which does not have definition of its own
b) Function which does have definition of its own
c) Function which does not have any return type
d) None of the mentioned

View Answer

Answer: a [Reason:] As the name itself implies, it have to depend on other class only.

9. 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) We can make an instance of an abstract super class
d) None of the mentioned

View Answer

Answer: a

10. Where does the abstract class is used?
a) base class only
b) derived class
c) both derived & base class
d) none of the mentioned

View Answer

Answer: a

Set 3

1. Which header file is used to declare the complex number?
a) complexnum
b) complex
c) complexnumber
d) none of the mentioned

View Answer

Answer: b

2. How to declare the complex number?
a) (3,4)
b) complex(3,4)
c) (3,4i)
d) none of the mentioned

View Answer

Answer: b [Reason:] We can declare the complex number by using complex(3,4) where 3 is a real number and 4 is imaginary part.

3. How many real types are there in complex numbers?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are three real types in complex numbers. They are float complex, double complex, long double complex.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main() 
  5.     {
  6.        complex<double> c1(4.0, 16.0), c2;
  7.        c2 = pow(c1, 2.0);
  8.        cout << c2;
  9.        return 0;          
  10.     }

a) (-240, 128)
b) (240, 128)
c) (240, 120)
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, we are finding the square of the complex number. Output: $ g++ comp.cpp $ a.out (-240,128)

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         complex<double> c_double(2, 3);
  7.         complex<int> c_int(4, 5);
  8.         c_double *= 2;
  9.         c_double = c_int;
  10.         cout << c_double;
  11.         return 0;
  12.     }

a) (2, 3)
b) (4, 5)
c) (8, 15)
d) None of the mentioned

View Answer

Answer: b [Reason:] We are just copying the value of c_int into c_double, So it’s printing as (4,5). Output: $ g++ comp1.cpp $ a.out (4,5)

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         complex<int> i(2, 3);
  7.         i = i * 6 / 3;
  8.         cout << i;
  9.         return 0;
  10.     }

a) (4, 6)
b) (2, 3)
c) (6, 12)
d) None of the mentioned

View Answer

Answer: a [Reason:] We are multiplying the complex number by 2. Output: $ g++ comp2.cpp $ a.out (4,6)

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.        complex<double> c1(4.0,3.0);
  7.        cout << "c1: " << c1;
  8.        complex<float> c2(polar(5.0,0.75));
  9.        cout << c1 + complex<double>(c2.real(),c2.imag());
  10.        return 0;
  11.     }

a) c1: (4,3)(7.65844,6.40819)
b) c1: (4,3)(7,6)
c) both c1: (4,3)(7.65844,6.40819) & c1: (4,3)(7,6)
d) None of the mentioned

View Answer

Answer: a [Reason:] We are adding the two complex numbers and printing the result. Output: $ g++ comp3.cpp $ a.out c1: (4,3)(7.65844,6.40819)

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         complex<double> c1(4.0, 3.0);
  7.         complex<float> c2(polar(5.0, 0.75));
  8.         cout  << (c1 += sqrt(c1)) << endl;
  9.         return 0;
  10.     }

a) (4.0, 3.0)
b) (6.12132, 3.70711)
c) (5.0, 0.75)
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, we are adding both complex number and finding the square root of it. Output: $ g++ comp4.cpp $ a.out (6.12132,3.70711)

9. Which of the following is not a function of complex values?
a) real
b) imag
c) norm
d) cartesian

View Answer

Answer: d

10. What is the output of this program?

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         complex<double> mycomplex (20.0, 2.0);
  7.         cout << imag(mycomplex) << endl;
  8.         return 0;
  9.     }

a) 2
b) 20
c) 40
d) None of the mentioned

View Answer

Answer: a [Reason:] imag part will return the imaginery part of the complex number. Output: $ g++ comp5.cpp $ a.out 2

Set 4

1. What is the user-defined header file extension in c++?
a) cpp
b) h
c) hf
d) none of the mentioned

View Answer

Answer: b

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 [Reason:] 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) none of the mentioned

View Answer

Answer: a [Reason:] The iostream.h is used in the older versions of c++ and iostream is evolved from it in the std namespace.

4. What does a default header file contain?
a) prototype
b) implementation
c) declarations
d) none of the mentioned

View Answer

Answer: c [Reason:] In the header file, we define something that to be manipulated in the program.

5. What is the output of this program?

  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 [Reason:] In this program,we need to string header file to run this program.

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 [Reason:] 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

8. Which of the header file must be included to use stringstream?
a) <iostream>
b) <string>
c) <sstring>
d) <sstream>

View Answer

Answer: b

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 [Reason:] In this fstream.h header file is used for accessing the files only.

10. What is the output of this program?

  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 [Reason:] In this program, we are finding the average of first 10 numbers using stdarg header file Output: $ g++ std.cpp $ a.out 4

Set 5

1. subscript operator is used to access which elements?
a) string
b) char
c) array
d) all of the mentioned

View Answer

Answer: c

2. How many arguments will the subscript operator will take for overloading?
a) 1
b) 2
c) 0
d) as many as possible

View Answer

Answer: a [Reason:] The subscript operator overload takes only one argument, but it can be of any type.

3. Pick out the correct statement.
a) subscript operator has a higher precedence than the assignment operator
b) subscript operator has a lower precedence than the assignment operator
c) subscript operator is used with string elements
d) none of the mentioned

View Answer

Answer: a

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     const int SIZE = 10;
  4.     class safe
  5.     {
  6.         private:
  7.         int arr[SIZE];
  8.         public:
  9.         safe()
  10.         {
  11.             register int i;
  12.             for (i = 0; i < SIZE; i++)
  13.             {
  14.                 arr[i] = i;
  15.             }
  16.         }
  17.         int &operator[](int i)
  18.         {
  19.             if (i > SIZE)
  20.             {
  21.                 cout << "Index out of bounds" <<endl;
  22.                 return arr[0];
  23.             }
  24.             return arr[i];
  25.         }
  26.     };
  27.     int main()
  28.     {
  29.         safe A;
  30.         cout << A[5];
  31.         cout  << A[12];
  32.         return 0;
  33.     }

a) 5Index out of bounds
      0
b) 40
c) 50
d) 51

View Answer

Answer: a [Reason:] In this program, We are returning the elements in the specified array location and if it is out of bound means it will return the first element. Output: $ g++ sub.cpp $ a.out 5Index out of bounds 0

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class numbers
  4.     {
  5.         private:
  6.         int m_nValues[10];
  7.         public:
  8.         int& operator[] (const int nValue);
  9.     };
  10.     int& numbers::operator[](const int nValue)
  11.     {
  12.         return m_nValues[nValue];
  13.     }
  14.     int main()
  15.     {
  16.         numbers N;
  17.         N[5] = 4;
  18.         cout <<  N[5];
  19.         return 0;
  20.     }

a) 5
b) 4
c) 3
d) 6

View Answer

Answer: b [Reason:] In this program, We are getting the values and returning it by overloading the subscript operator. Output: $ g++ sub1.cpp $ a.out 4

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     const int limit = 4;
  4.     class safearray
  5.     {
  6.         private:
  7.         int arr[limit];
  8.         public:
  9.         int& operator [](int n)
  10.         {
  11.             if (n == limit - 1)
  12.             {
  13.                 int temp;
  14.                 for (int i = 0; i < limit; i++)
  15.                 {
  16.                     if (arr[n + 1] > arr[n])
  17.                     {
  18.                         temp = arr[n];
  19.                         arr[n] = arr[n + 1];
  20.                         arr[n + 1] = temp;
  21.                     }     
  22.                 }  
  23.             }
  24.             return arr[n];
  25.         }
  26.     };
  27.     int main()
  28.     {
  29.         safearray sa1;
  30.         for(int j = 0; j < limit; j++)
  31.             sa1[j] = j*10;
  32.         for(int j = 0; j < limit; j++)
  33.         {
  34.             int temp = sa1[j];
  35.             cout << "Element " << j << " is " << temp;
  36.         }
  37.         return 0;
  38.     }

a) 0102030
b) 1020300
c) 3020100
d) error

View Answer

Answer: a [Reason:] In this program, we are returning the array element by the multiple of 10. Output: $ g++ sub2.cpp $ a.out 0102030

7. What is the output of this program?

  1.     #include <iostream> 
  2.     using namespace std;
  3.     class A
  4.     {
  5.         public:
  6.         int x;
  7.         A(int n = 0) : x(n) {};
  8.         int& operator[](int n)
  9.         {
  10.              cout << "0" ;
  11.              return x;
  12.         }
  13.         int operator[](int n) const
  14.         {
  15.              cout << "1" ;
  16.              return x;
  17.         }
  18.      };
  19.     void foo(const A& a)
  20.     {
  21.         int z = a[2];
  22.     }
  23.     int main()
  24.     {
  25.         A a(7);
  26.         a[3]  = 8;
  27.         int z = a[2];
  28.         foo(a);
  29.         return 0;
  30.     }

a) 110
b) 111
c) 011
d) 001

View Answer

Answer: d [Reason:] In this program, we overloading the operator[] by using subscript operator. Output: $ g++ sub3.cpp $ a.out 001

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         private:
  6.         int* i;
  7.         int j;
  8.         public:
  9.         sample (int j);
  10.         ~sample ();
  11.         int& operator [] (int n);
  12.     };
  13.     int& sample::operator [] (int n)
  14.     {
  15.         return i[n];
  16.     }
  17.     sample::sample (int j)
  18.     {
  19.         i = new int [j];
  20.         j = j;
  21.     }
  22.     sample::~sample ()
  23.     {
  24.         delete [] i;
  25.     }
  26.     int main ()
  27.     {
  28.         sample m (5);
  29.         m [0] = 25;
  30.         m [1] = 20;
  31.         m [2] = 15;
  32.         m [3] = 10;
  33.         m [4] = 5;
  34.         for (int n = 0; n < 5; ++ n)
  35.         cout << m [n];
  36.         return 0;
  37.     }

a) 252015105
b) 510152025
c) 51015
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, we are printing the array in the reverse order by using subscript operator. Output: $ g++ sub4.cpp $ a.out 252015105

9. What do we need to do to pointer for overloading the subscript operator?
a) reference pointer
b) dereference pointer
c) store it in heap
d) none of the mentioned

View Answer

Answer: b [Reason:] If you have a pointer to an object of some class type that overloads the subscript operator, you have to dereference that pointer in order to free the memory.

10. What do we need to use when we have multiple subscripts?
a) operator()
b) operator[].
c) operator
d) none of the mentioned

View Answer

Answer: a [Reason:] The reason is that operator[] always takes exactly one parameter, but operator() can take any number of parameters.