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.         int i = -3;
  5.         int k = i % 2;
  6.         printf("%dn", k);
  7.     }

a) Compile time error
b) -1
c) 1
d) Implementation defined

View Answer

Answer: b

2. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 3;
  5.         int l = i / -2;
  6.         int k = i % -2;
  7.         printf("%d %dn", l, k);
  8.         return 0;
  9.     }

a) Compile time error
b) -1 1
c) 1 -1
d) Implementation defined

View Answer

Answer: b

3. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 5;
  5.         i = i / 3;
  6.         printf("%dn", i);
  7.         return 0;
  8.     }

a) Implementation defined
b) 1
c) 3
d) Compile time error

View Answer

Answer: b

4. What is the output of this C code?

  1.    #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = -5;
  5.         i = i / 3;
  6.         printf("%dn", i);
  7.         return 0;
  8.     }

a) Implementation defined
b) -1
c) -3
d) Compile time error

View Answer

Answer: b

5. What is the value of x in this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 5 * 9 / 3 + 9;
  5.     }

a) 3.75
b) Depends on compiler
c) 24
d) 3

View Answer

Answer: c

6. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 5.3 % 2;
  5.         printf("Value of x is %d", x);
  6.     }

a) Value of x is 2.3
b) Value of x is 1
c) Value of x is 0.3
d) Compile time error

View Answer

Answer: d

7. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int y = 3;
  5.         int x = 5 % 2 * 3 / 2;
  6.         printf("Value of x is %d", x);
  7.     }

a) Value of x is 1
b) Value of x is 2
c) Value of x is 3
d) Compile time error

View Answer

Answer: a

Set 2

Here is a listing of advanced C++ interview questions on “Access Control” along with answers, explanations and/or solutions:

1. How many access specifiers are there in c++?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are three access specifiers in c++. They are public, Private and Protected.

2. What of the following describes protected access specifier?
a) The variable is visible only outside inside the block
b) The variable is visible everywhere
c) The variable is visible to its block and to it’s derived class
d) None of the mentioned

View Answer

Answer: c

3. To which of the following access aspecifiers are applicable?
a) Member data
b) Functions
c) Both Member data & Functions
d) None of the mentioned

View Answer

Answer: c [Reason:] The access specifiers can be applicable to the member data and functions because they need to be accessed outside the block.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class student
  4.     {
  5.         public:
  6.         int rno , m1 , m2 ;
  7.         protected:
  8.         void get()
  9.         {
  10.             rno = 15, m1 = 10, m2 = 10;
  11.         }
  12.     };
  13.     class sports
  14.     {
  15.         public:
  16.         int sm;
  17.         void getsm()
  18.         {
  19.             sm = 10;
  20.         }
  21.     };
  22.     class statement : public student, public sports
  23.     {
  24.         int tot, avg;
  25.         public:
  26.         void display()
  27.         {
  28.             tot = (m1 + m2 + sm);
  29.             avg = tot / 3;
  30.             cout << tot;
  31.             cout << avg;
  32.         }
  33.         void setObject()
  34.         {
  35.             get();
  36.         }
  37.     };
  38.     int main()
  39.     {
  40.         statement obj;
  41.         obj.setObject();
  42.         obj.getsm();
  43.         obj.display();
  44.     }

a) 3010
b) 1010
c) 2100
d) Error

View Answer

Answer: a [Reason:] None.

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std; 
  3.     struct A 
  4.     {
  5.         int i;
  6.         char j;
  7.         float f;
  8.         void func();
  9.     };
  10.     void A :: func() {}
  11.     struct B 
  12.     {
  13.         public:
  14.         int i;
  15.         char j;
  16.         float f;
  17.         void func();
  18.     };
  19.     void B :: func() {}
  20.     int main() 
  21.     {
  22.         A a; B b;
  23.         a.i = b.i = 1; 
  24.         a.j = b.j = 'c';
  25.         a.f = b.f = 3.14159;
  26.         a.func();
  27.         b.func();
  28.         cout << "Allocated"; 
  29.         return 0;
  30.     }

a) Allocated
b) Error
c) 3.14159
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We used access specifiers for structures, As we declared all methods as public, The values can be allocated. Output: $ g++ acc.cpp $ a.out Allocated

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     struct A 
  4.     {
  5.         private:
  6.         int i, j, k;
  7.         public:
  8.         int f();
  9.         void g();
  10.     };
  11.     int A :: f() 
  12.     {
  13.         return i + j + k;
  14.     }
  15.     void A :: g() 
  16.     {
  17.         i = j = k = 0;
  18.     }
  19.     class B 
  20.     {
  21.         int i, j, k;
  22.         public:
  23.         int f();
  24.         void g();
  25.     };
  26.     int B :: f() 
  27.     {
  28.         return i + j + k; 
  29.     }
  30.     void B :: g() 
  31.     {
  32.         i = j = k = 0;
  33.     }
  34.     int main() 
  35.     {
  36.         A a;
  37.         B b;
  38.         a.f(); 
  39.         a.g();
  40.         b.f(); 
  41.         b.g();
  42.         cout << "Identical results would be produced";
  43.     }

a) 50
b) Identical results would be produced
c) Error
d) Runtime error

View Answer

Answer: b [Reason:] In this program, We apply the access specifiers to both the class and the structure. Output: $ g++ acc1.cpp $ a.out Identical results would be produced

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Cat
  4.     {
  5.         public:
  6.         int age;
  7.         int weight;
  8.     };
  9.     int main()
  10.     {
  11.         Cat f;
  12.         f.age = 56;
  13.         cout << "Gates is " ;
  14.         cout << f.age << " years old.n";
  15.     }

a) Gates is
b) Gates is 56 years old
c) Error
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, We passed the value from main function to class and returning it to the main and then printing it. Output: $ g++ acc2.cpp $ a.out Gates is 56 years old

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     struct X;
  4.     struct Y 
  5.     {
  6.         void f(X*);
  7.     };
  8.     struct X 
  9.     {
  10.         private:
  11.         int i;
  12.         public:
  13.         void initialize(); 
  14.         friend void g(X* , int);
  15.         friend void Y :: f(X*);
  16.         friend struct Z;
  17.         friend void h();
  18.     };
  19.     void X :: initialize() 
  20.     {
  21.         i = 0;
  22.     }
  23.     void g(X* x, int i) 
  24.     {
  25.         x -> i = i;
  26.     }
  27.     void Y :: f(X * x) 
  28.     {
  29.         x -> i = 47;
  30.         cout << x->i;
  31.     }
  32.     struct Z 
  33.     {
  34.         private:
  35.         int j;
  36.         public:
  37.         void initialize();
  38.         void g(X* x);
  39.     };
  40.     void Z::initialize() 
  41.     {
  42.         j = 99;
  43.     }
  44.     void Z::g(X* x) 
  45.     {
  46.         x -> i += j;
  47.     }
  48.     void h() 
  49.     {
  50.         X x;
  51.         x.i = 100;
  52.         cout << x.i;
  53.     }
  54.     int main() 
  55.     {
  56.         X x;
  57.         Z z;
  58.         z.g(&x);
  59.         cout << "Data accessed";
  60.     }

a) 99
b) 47
c) Data accessed
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are using the access specifiers to friend function to manipulate the values. Output: $ g++ acc3.cpp $ a.out Data accessed

9. What is the default access level to a block of data?
a) Public
b) Protected
c) Private
d) None of the mentioned

View Answer

Answer: c

10. What is the importance of mutable keyword?
a) It allows the data member to change within a const member function
b) It will not allow the data member to change within a const member function
c) It will copy the values of the variable
d) None of the mentioned

View Answer

Answer: a [Reason:] Mutable keyword allows assigning values to a data member belonging to a class defined as “Const” or constant.

Set 3

Here is a listing of advanced C++ interview questions on “Derivation and Templates” along with answers, explanations and/or solutions:

1. Which is dependant on template parameter?
a) base class
b) abstract class
c) method
d) none of the mentioned

View Answer

Answer: a

2. Which value is placed in the base class?
a) derived values
b) default type values
c) both default type & derived values
d) none of the mentioned

View Answer

Answer: b [Reason:] We can place the default type values in a base class and overriding some of them through derivation.

3. How many bits of memory needed for internal representation of class?
a) 1
b) 2
c) 4
d) no memory needed

View Answer

Answer: d [Reason:] classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class class0 
  4.     {
  5.         public:
  6.         virtual ~class0(){}
  7.         protected:
  8.         char p;
  9.         public:
  10.         char getChar();
  11.     };
  12.     class class1 : public class0 
  13.     {
  14.         public:
  15.         void printChar();
  16.     };
  17.     void class1::printChar()
  18.     {
  19.         cout  << "True" << endl;
  20.     }
  21.     int main() 
  22.     {
  23.         class1 c;
  24.         c.printChar();
  25.         return 1;
  26.     }

a) True
b) error
c) no output
d) runtime error

View Answer

Answer: a [Reason:] In this program, We are passing the values and inheriting it to the other class and printing the result. $ g++ dert.cpp $ a.out True

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std; 
  3.     template<typename T>class clsTemplate
  4.     {
  5.         public:
  6.         T value;
  7.         clsTemplate(T i)
  8.         {
  9.             this->value = i;
  10.         }
  11.     void test()
  12.     {
  13.         cout << value << endl;
  14.     }
  15.     };
  16.     class clsChild : public clsTemplate<char>
  17.     {
  18.         public:
  19.         clsChild(): clsTemplate<char>( 0 )
  20.         {
  21.         }
  22.         clsChild(char c): clsTemplate<char>( c )
  23.         {    
  24.         }
  25.     void test2()
  26.     {
  27.         test();
  28.     }
  29.     };
  30.     int main()
  31.     {
  32.         clsTemplate <int> a( 42 );
  33.         clsChild b( 'A' );
  34.         a.test();
  35.         b.test();
  36.         return 0;
  37.     }

a) 42
b) A
c) 42
    A
d) A
    42

View Answer

Answer: c [Reason:] In this program, We are passing the values by using the template inheritance and printing it. Output: $ g++ dert.cpp $ a.out 42 A

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class T>
  4.     class A
  5.     {
  6.         public:
  7.         A(int a): x(a) {}
  8.         protected:
  9.         int x;
  10.     };
  11.     template <class T>
  12.     class B: public A<char>
  13.     {
  14.         public:
  15.         B(): A<char>::A(100) 
  16.         {
  17.             cout << x * 2 << endl;
  18.         }
  19.     };
  20.     int main()
  21.     {
  22.         B<char> test;
  23.         return 0;
  24.     }

a) 100
b) 200
c) error
d) runtime error

View Answer

Answer: b [Reason:] In this program, We are passing the values and manipulating it by using the template inheritance. Output: $ g++ dert2.cpp $ a.out 200

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class type>
  4.     class Test
  5.     {
  6.         public:
  7.         Test();
  8.         ~Test();
  9.         type Data(type);
  10.     };
  11.     template <class type>
  12.     type Test<type>::Data(type Var0)
  13.     {
  14.         return Var0;
  15.     }
  16.     template <class type>
  17.     Test<type>::Test()
  18.     {
  19.     }
  20.     template <class type>
  21.     Test<type>::~Test()
  22.     {
  23.     }
  24.     int main(void)
  25.     {
  26.         Test<char> Var3;
  27.         cout << Var3.Data('K') << endl;
  28.         return 0;
  29.     }

a) k
b) l
c) error
d) runtime error

View Answer

Answer: a [Reason:] In this program, We are passing the values and printing it by using template inheritance. Output: $ g++ dert3.cpp $ a.out k

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         public:
  6.         Base ( )
  7.         {
  8.             cout << "1" << endl;
  9.         }
  10.         ~Base ( )
  11.         {
  12.             cout << "2" << endl;
  13.         }
  14.     };
  15.     class Derived : public Base
  16.     {
  17.         public:
  18.         Derived  ( )
  19.         {
  20.             cout << "3" << endl;
  21.         }
  22.         ~Derived ( )
  23.         {
  24.             cout << "4" << endl;
  25.         }    
  26.     }; 
  27.     int main( )
  28.     {
  29.         Derived x;
  30.     }

a) 1234
b) 4321
c) 1423
d) 1342

View Answer

Answer: d [Reason:] In this program, We are printing the order of execution of constructor and destructor in the class. Output: $ g++ dert4.cpp $ a.out 1342

9. How many kinds of entities are directly parameterized in c++?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] C++ allows us to parameterize directly three kinds of entities through templates: types, constants, and templates.

10. How many kinds of parameters are there in C++?
a) 1
b) 2
c) 3
d) None of the mentioned

View Answer

Answer: c [Reason:] There are three kinds of parameters are there in C++. They are type, non-type, template.

Set 4

Here is a listing of advanced C++ interview questions on “Exception Specifications” along with answers, explanations and/or solutions:

1. What is meant by exception specification?
a) A function is limited to throwing only a specified list of exceptions
b) A catch can catch all types of exceptions
c) A function can throw any type of exceptions
d) None of the mentioned

View Answer

Answer: a [Reason:] C++ provides a mechanism to ensure that a given function is limited to throwing only a specified list of exceptions. It is called as exception specification.

2. Identify the correct statement about throw(type).
a) A function can throw any type of exceptions
b) A function can throw an exception of certain type only
c) A function can’t throw any type of exception
d) None of the mentioned

View Answer

Answer: b

3. What will happen when a programs throws any other type of exception other than specified?
a) terminate
b) arise an error
c) run
d) none of the mentioned

View Answer

Answer: b

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     void empty() throw() 
  4.     {
  5.         cout << "In empty()";
  6.     }
  7.     void with_type() throw(int) 
  8.     {
  9.         cout << "Will throw an int";
  10.         throw(1);
  11.     }
  12.     int main() 
  13.     {
  14.         try 
  15.         {
  16.             empty();
  17.             with_type();
  18.         }
  19.         catch (int) 
  20.         {
  21.             cout << "Caught an int";
  22.         }
  23.     }

a) In empty()
b) Will throw an int
c) Caught an int
d) All of the mentioned

View Answer

Answer: d [Reason:] It will print all three because we are calling all functions in the main(). Output: $ g++ exs.cpp $ a.out In empty()Will throw an intCaught an int

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <exception>
  3.     #include <typeinfo>
  4.     using namespace std;
  5.     class Test1
  6.     {    
  7.         virtual int  Funct() 
  8.         {
  9.         }
  10.     };
  11.     int main ()
  12.     {
  13.         try 
  14.         {
  15.             Test1 * var = NULL;
  16.             typeid (*var);
  17.         }
  18.         catch (std::exception& typevar)
  19.         {
  20.             cout << "Exception: " << typevar.what() << endl;   
  21.         }
  22.         return 0;
  23.     }

a) NULL
b) Exception:bad_alloc
c) Exception:std:bad_typeid
d) None of the mentioned

View Answer

Answer: c [Reason:] As we are using a bad type on pointers, So it is arising an error. Output: $ g++ exs1.cpp $ a.out Exception:std:bad_typeid

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     #include<typeinfo>
  4.     using namespace std;
  5.     int main( )
  6.     {
  7.         try
  8.         {
  9.             string strg1("Test");
  10.             string strg2("ing");
  11.             strg1.append(strg2, 4, 2);
  12.             cout << strg1 << endl;
  13.         }
  14.         catch (exception &e)
  15.         {
  16.             cout << "Caught: " << e.what() << endl;
  17.             cout << "Type: " << typeid(e).name() << endl;
  18.         };
  19.         return 0;
  20.     }

a) out of range
b) bad type_id
c) bad allocation
d) none of the mentioned

View Answer

Answer: a [Reason:] As we are using out of bound value on strings, So it arising an exception. Output: $ g++ exs2.cpp $ a.out Caught: basic_string::append Type: St12out_of_range #include

7. What is the output of this program?

  1.     #include <typeinfo>
  2.     #include <iostream>
  3.     using namespace std;
  4.     class Myshape
  5.     {
  6.         public:
  7.         virtual void myvirtualfunc() const {}
  8.     };
  9.     class mytriangle: public Myshape
  10.     {
  11.         public:
  12.         virtual void myvirtualfunc() const
  13.         {   
  14.         };
  15.     };
  16.     int main()
  17.     {
  18.         Myshape Myshape_instance;
  19.         Myshape &ref_Myshape = Myshape_instance;
  20.         try 
  21.         {
  22.             mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_Myshape);
  23.         }
  24.         catch (bad_cast)
  25.         {
  26.             cout << "Can't do the dynamic_cast lor!!!" << endl;
  27.             cout << "Caught: bad_cast exception. Myshape is not mytriangle.n";
  28.         }
  29.         return 0;
  30.     }

a) Can’t do the dynamic_cast lor!!!
b) Caught: bad_cast exception. Myshape is not mytriangle.
c) Can’t able to create the dynamic instance for the triangle, So it is arising an exception
d) None of the mentioned

View Answer

Answer: c [Reason:] As we can’t able to create the dynamic instance for the triangle, So it is arising an exception. Output: $ g++ exs3.cpp $ a.out Can’t do the dynamic_cast lor!!! Caught: bad_cast exception. Myshape is not mytriangle.

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char* ptr;
  6.         unsigned long int Test = sizeof(size_t(0) / 3);
  7.         cout << Test << endl;
  8.         try
  9.         {
  10.             ptr = new char[size_t(0) / 3];
  11.             delete[ ] ptr;
  12.         }
  13.         catch (bad_alloc &thebadallocation)
  14.         {
  15.             cout << thebadallocation.what() << endl;
  16.         };
  17.         return 0;
  18.     }

a) 4
b) 2
c) bad_alloc
d) depends on compiler

View Answer

Answer: d [Reason:] The size of unsigned long int always depends on compiler. Output: $ g++ exs4.cpp $ a.out 4

9. What do you mean by “No exception specification”?
a) It throws nothing
b) It can throw anything
c) It can catch anything
d) None of the mentioned

View Answer

Answer: b

10. Which operations don’t throw anything?
a) Operations which are reversible
b) Operations which are irreversible
c) Operations which are static
d) Operations which are dynamic

View Answer

Answer: b

Set 5

Here is a listing of advanced C++ interview questions on “Resource Management” along with answers, explanations and/or solutions:

1. What can go wrong in resource management on c++?
a) Leakage
b) Exhaustion
c) Dangling
d) All of the mentioned

View Answer

Answer: d [Reason:] If there is any mishap in memory or resource management means, the problems that are mentioned above can happen.

2. When we call that resource is leaked?
a) Arise of compile time error
b) It cannot be accessed by any standard mean
c) Arise of runtime error
d) None of the mentioned

View Answer

Answer: b [Reason:] Resource is said to be leaked when it cannot by accessed by any means of standard mean.

3. What kind of error can arise when there is problem in memory?
a) Segmentation fault
b) Produce an error
c) Both Segmentation fault & Produce an error
d) none of the mentioned

View Answer

Answer: a

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <new>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         int i, n;
  7.         int * p;
  8.         i = 2;
  9.         p= new (nothrow) int[i];
  10.         if (p == 0)
  11.             cout << "Error: memory could not be allocated";
  12.         else
  13. 	{
  14.             for (n=0; n<i; n++)
  15.             {
  16.                 p[n] = 5;
  17.             }
  18.             for (n = 0; n < i; n++)
  19.                 cout << p[n];
  20.             delete[] p;
  21.          }
  22.          return 0;
  23.     }

a) 5
b) 55
c) 555
d) Error: memory could not be allocated

View Answer

Answer: b [Reason:] As we had given i value as 2, It will print the 5 for two times. Output: $ g++ res.cpp $ a.out 55

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main(void)
  4.     {
  5.         const char *one = "Test";
  6.         cout << one << endl;
  7.         const char *two = one;
  8.         cout << two << endl;
  9.         return 0;
  10.     }

a) Test
b) TestTest
c) Te
d) None of the mentioned

View Answer

Answer: b [Reason:] We are copying the values from one variable to other, So it is printing is TestTest Output: $ g++ res1.cpp $ a.out TestTest

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int funcstatic(int)
  4.     {
  5.         int sum = 0;
  6.         sum = sum + 10;
  7.         return sum;
  8.     }
  9.     int main(void)
  10.     {
  11.         int r = 5, s;
  12.         s = funcstatic(r);
  13.         cout << s << endl;
  14.         return 0;
  15.     }

a) 10
b) 15
c) error
d) none of the mentioned

View Answer

Answer: a [Reason:] Eventhough we passed the value, we didn’t caught to manipulate it, So it is printing as 10. Output: $ g++ res2.cpp $ a.out 10

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include<string.h>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         try
  7.         {
  8.             char *p;
  9.             strcpy(p, "How r u");
  10.         }
  11.         catch(const exception& er)
  12.         {
  13.         }
  14.     }

a) How r u
b) segmentation fault
c) error
d) runtime error

View Answer

Answer: b [Reason:] As we are using a pointer value to copy a string, So it will be producing a runtime error. Output: $ g++ res3.cpp $ a.out segmentation fault

8. What is meant by garbage collection?
a) Form of manual memory management
b) Form of automatic memory management
c) Used to replace the variables
d) None of the mentioned

View Answer

Answer: b [Reason:] The garbage collectoion attempts to reclaim memory occupied by objects that are no longer in use by the program.

9. What are the operators available in dynamic memory allocation?
a) new
b) delete
c) compare
d) both new & delete

View Answer

Answer: d [Reason:] new and delete operators are mainly used to allocate and deallocate during runtime.

10. Which is used to solve the memory management problem in c++?
a) smart pointers
b) arrays
c) stack
d) none of the mentioned

View Answer

Answer: a