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. Which operator is used to insert the data into file?
a) >>
b) <<
c) <
d) None of the mentioned

View Answer

Answer: b [Reason:] You can write information to a file from your program using the stream insertion operator <<.

2. Which function is used to position back from the end of file object?
a) seekg
b) seekp
c) both seekg & seekp
d) none of the mentioned

View Answer

Answer: a [Reason:] The member function seekg is used to position back from the end of file object.

3. How many objects are used for input and output to a string?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] The stringstream, ostringstream, and istringstream objects are used for input and output to a string.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <fstream>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         int length;
  7.         char * buffer;
  8.         ifstream is;
  9.         is.open ("sample.txt", ios :: binary );
  10.         is.seekg (0, ios :: end);
  11.         length = is.tellg();
  12.         is.seekg (0, ios :: beg);
  13.         buffer = new char [length];
  14.         is.read (buffer, length);
  15.         is.close();
  16.         cout.write (buffer, length);
  17.         delete[] buffer;
  18.         return 0;
  19.     }

a) This is sample
b) sample
c) Error
d) Runtime error

View Answer

Answer: d [Reason:] In this program, if the file exist, it will read the file. Otherwise it will throw an exception. A runtime error will occur because the value of the length variable will be “-1” if file doesn’t exist and in line 13 we are trying to allocate an array of size “-1”.

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         char first, second;
  6.         cout << "Enter a word: ";
  7.         first = cin.get();
  8.         cin.sync();
  9.         second = cin.get();
  10.         cout << first << endl;
  11.         cout << second << endl;
  12.         return 0;
  13.     }

a) first
b) second
c) Returns first 2 letter or number from the entered word
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are using the sync function to return the first two letters of the entered word. Output: $ g++ stream.cpp $ a.out Enter a word: steve s t

6. What is the output of this program?

  1.     #include<iostream>
  2.     #include <fstream>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         ofstream outfile ("test.txt");
  7.         for (int n = 0; n < 100; n++)
  8.         {
  9.             outfile << n;
  10.             outfile.flush();
  11.         }
  12.         cout << "Done";
  13.         outfile.close();
  14.         return 0;
  15.     }

a) Done
b) Error
c) Runtime error
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are using the flush function to update the contents in a file. Output: $ g++ stream1.cpp $ a.out Done

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         int a = 100;
  6.         double b = 3.14;
  7.         cout << a;
  8.         cout << endl;
  9.         cout << b << endl << a * b;
  10.         endl (cout);
  11.         return 0;
  12.     }

a) 100
b) 3.14
c) 314
d) All of the mentioned

View Answer

Answer: d [Reason:] In this program, We are printing the given value and manipulating the given value by using endl. Output: $ g++ stream2.cpp $ a.out 100 3.14 314

8. By seeing which operator thus this program stops getting the input?

  1.     #include <iostream>
  2.     #include <fstream>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         char ch;
  7.         streambuf * p;
  8.         ofstream os ("test.txt");
  9.         pbuf = os.rdbuf();
  10.         do {
  11.             ch = cin.get();
  12.             p -> sputc(ch);
  13.         } while (ch != '.');
  14.         os.close();
  15.         return 0;
  16.     }

a) dot operator
b) insertion operator
c) $ symbol
d) none of the mentioned

View Answer

Answer: a [Reason:] This program will stop getting the input, When it occurs the dot(.) operator. Output: $ g++ stream3.cpp $ a.out Steve.

9. Which member function is used to determine whether the stream object is currently associated with a file?
a) is_open
b) buf
c) string
d) none of the mentioned

View Answer

Answer: a [Reason:] The member function is_open can be used to determine whether the stream object is currently associated with a file.

10. Which header file is used for reading and writing to a file?
a) #include<iostream>
b) #include<fstream>
c) #include<file>
d) None of the mentioned

View Answer

Answer: b

Set 2

1. What is a template?
a) A template is a formula for creating a generic class
b) A template is used to manipulate the class
c) A template is used for creating the attributes
d) None of the mentioned

View Answer

Answer: a

2. Pick out the correct statement about string template.
a) It is used to replace a string
b) It is used to replace a string with another string at runtime
c) It is used to delete a string
d) None of the mentioned

View Answer

Answer: b [Reason:] Every string template is used to replace the string with another string at runtime.

3. How to declare a template?
a) tem
b) temp
c) template<>
d) none of the mentioned

View Answer

Answer: c

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class T>
  4.     inline T square(T x)
  5.     {
  6.         T result;
  7.         result = x * x;
  8.         return result;
  9.     };
  10.     template <>
  11.     string square<string>(string ss)
  12.     {
  13.         return (ss+ss);
  14.     };
  15.     int main()     
  16.     {
  17.         int i = 4, ii;
  18.         string ww("A");
  19.         ii = square<int>(i);
  20.         cout << i << ii;
  21.         cout << square<string>(ww) << endl;
  22.     }

a) 416AA
b) 164AA
c) AA416
d) none of the mentioned

View Answer

Answer: a [Reason:] In this program, We are using two template to calculate the square and to find the addition. Output: $ g++ tem.cpp $ a.out 416AA

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <typename T, typename U>
  4.     void squareAndPrint(T x, U y)
  5.     {
  6.         cout << x << x * x << endl;
  7.         cout << y << " " << y * y << endl;
  8.     };
  9.     int main()
  10.     {
  11.         int ii = 2;
  12.         float jj = 2.1;
  13.         squareAndPrint<int, float>(ii, jj);
  14.     }

a) 23
2.1 4.41
b) 24
2.1 4.41
c) 24
2.1 3.41
d) none of the mentioned

View Answer

Answer: b [Reason:] In this multiple templated types, We are passing two values of different types and producing the result. Output: $ g++ tem1.cpp $ a.out 24 2.1 4.41

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     template<typename T>
  5.     void print_mydata(T output)
  6.     {
  7.         cout << output << endl;
  8.     }
  9.     int main()
  10.     {
  11.         double d = 5.5;
  12.         string s("Hello World");
  13.         print_mydata( d );
  14.         print_mydata( s );
  15.         return 0;
  16.     }

a) 5.5
Hello World
b) 5.5
c) Hello World
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We are passing the value to the template and printing it in the template. Output: $ g++ tem2.cpp $ a.out 5.5 Hello World

7. How many types of templates are there in c++?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: b [Reason:] There are two types of templates. They are function template and class template.

8. Which are done by compiler for templates?
a) type-safe
b) portability
c) code elimination
d) all of the mentioned

View Answer

Answer: a [Reason:] The compiler can determine at compile time whether the type associated with a template definition can perform all of the functions required by that template definition.

9. What may be the name of the parameter that the template should take?
a) same as template
b) same as class
c) same as function
d) none of the mentioned

View Answer

Answer: a

10. How many parameters are legal for non-type template?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: d [Reason:] The following are legal for non-type template parameters: integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.

Set 3

1. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int a[2][3] = {1, 2, 3, 4, 5};
  5.         int i = 0, j = 0;
  6.         for (i = 0; i < 2; i++)
  7.         for (j = 0; j < 3; j++)
  8.         printf("%d", a[i][j]);
  9.     }

a) 1 2 3 4 5 0
b) 1 2 3 4 5 junk
c) 1 2 3 4 5 5
d) Run time error

View Answer

Answer: a

2. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int a[2][3] = {1, 2, 3, , 4, 5};
  5.         int i = 0, j = 0;
  6.         for (i = 0; i < 2; i++)
  7.         for (j = 0; j < 3; j++)
  8.         printf("%d", a[i][j]);
  9.     }

a) 1 2 3 junk 4 5
b) Compile time error
c) 1 2 3 0 4 5
d) 1 2 3 3 4 5

View Answer

Answer: b

3. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void f(int a[][3])
  3.     {
  4.         a[0][1] = 3;
  5.         int i = 0, j = 0;
  6.         for (i = 0; i < 2; i++)
  7.         for (j = 0; j < 3; j++)
  8.         printf("%d", a[i][j]);
  9.     }
  10.     void main()
  11.     {
  12.         int a[2][3] = {0};
  13.         f(a);
  14.     }

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values

View Answer

Answer: a

4. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void f(int a[][])
  3.     {
  4.         a[0][1] = 3;
  5.         int i = 0, j = 0;
  6.         for (i = 0;i < 2; i++)
  7.         for (j = 0;j < 3; j++)
  8.         printf("%d", a[i][j]);
  9.     }
  10.     void main()
  11.     {
  12.         int a[2][3] = {0};
  13.         f(a);
  14.     }

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values

View Answer

Answer: c

5. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void f(int a[2][])
  3.     {
  4.         a[0][1] = 3;
  5.         int i = 0, j = 0;
  6.         for (i = 0;i < 2; i++)
  7.         for (j = 0;j < 3; j++)
  8.         printf("%d", a[i][j]);
  9.     }
  10.     void main()
  11.     {
  12.         int a[2][3] = {0};
  13.         f(a);
  14.     }

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values

View Answer

Answer: c

6. Comment on the following statement:
    int (*a)[7];
a) An array “a” of pointers.
b) A pointer “a” to an array.
c) A ragged array.
d) None of the mentioned

View Answer

Answer: b

7. Comment on the 2 arrays regarding P and Q:

  1. int *a1[8];
  2.     int *(a3[8]);
  3.     P. Array of pointers
  4.     Q. Pointer to an array

a) a1 is P, a2 is Q
b) a1 is P, a2 is P
c) a1 is Q, a2 is P
d) a1 is Q, a2 is Q

View Answer

Answer: b

8. Which of the following is not possible statically in C?
a) Jagged Array
b) Rectangular Array
c) Cuboidal Array
d) Multidimensional Array

View Answer

Answer: a

Set 4

1. Which rule will not affect the friend function?
a) private and protected members of a class cannot be accessed from outside
b) private and protected member can be accessed anywhere
c) protected member can be accessed anywhere
d) none of the mentioned

View Answer

Answer: a [Reason:] Friend is used to access private and protected members of a class from outside the same class.

2. Which keyword is used to declare the friend function?
a) firend
b) friend
c) classfriend
d) myfriend

View Answer

Answer: b

3. What is the syntax of friend function?
a) friend class1 Class2;
b) friend class;
c) friend class
d) none of the mentioned

View Answer

Answer: a [Reason:] In option a, the class2 is the friend of class1 and it can access all the private and protected members of class1.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Box
  4.     {
  5.         double width;
  6.         public:
  7.         friend void printWidth( Box box );
  8.         void setWidth( double wid );
  9.     };
  10.     void Box::setWidth( double wid )
  11.     {
  12.         width = wid;
  13.     }
  14.     void printWidth( Box box )
  15.     {
  16.         box.width = box.width * 2;
  17.         cout << "Width of box : " << box.width << endl;
  18.     }
  19.     int main( )
  20.     {
  21.         Box box;
  22.         box.setWidth(10.0);
  23.         printWidth( box );
  24.         return 0;
  25.    }

a) 40
b) 5
c) 10
d) 20

View Answer

Answer: d [Reason:] We are using the friend function for printwidth and multiplied the width value by 2, So we got the output as 20 Output: $ g++ friend.cpp $ a.out 20

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample 
  4.     {
  5.         int width, height;
  6.         public:
  7.         void set_values (int, int);
  8.         int area () {return (width * height);}
  9.         friend sample duplicate (sample);
  10.     };
  11.     void sample::set_values (int a, int b) 
  12.     {
  13.         width = a;
  14.         height = b;
  15.     }
  16.     sample duplicate (sample rectparam)
  17.     {
  18.         sample rectres;
  19.         rectres.width = rectparam.width * 2;
  20.         rectres.height = rectparam.height * 2;
  21.         return (rectres);
  22.     }  
  23.     int main ()  
  24.     {
  25.         sample rect, rectb;
  26.         rect.set_values (2, 3);
  27.         rectb = duplicate (rect);
  28.         cout << rectb.area();
  29.         return 0;
  30.     }

a) 20
b) 16
c) 24
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, we are using the friend function for duplicate function and calculating the area of the rectangle. Output: $ g++ friend1.cpp $ a.out 24

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample;
  4.     class sample1 
  5.     {
  6.         int width, height;
  7.         public:
  8.         int area ()
  9.         {
  10.             return (width * height);}
  11.             void convert (sample a);
  12.         };
  13.     class sample 
  14.     {
  15.         private:
  16.         int side;
  17.         public:
  18.         void set_side (int a)
  19.         { 
  20.             side = a;
  21.         }
  22.         friend class sample1;
  23.     };
  24.     void sample1::convert (sample a) 
  25.     {
  26.         width = a.side;
  27.         height = a.side;
  28.     }
  29.     int main () 
  30.     {
  31.         sample sqr;
  32.         sample1 rect;
  33.         sqr.set_side(6);
  34.         rect.convert(sqr);
  35.         cout << rect.area();
  36.         return 0;
  37.     }

a) 24
b) 35
c) 16
d) 36

View Answer

Answer: d [Reason:] In this program, we are using the friend for the class and calculating the area of the square. Output: $ g++ friend2.cpp $ a.out 36

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class base
  4.     {
  5.         int val1, val2;
  6.         public:
  7.         int get()
  8. 	{
  9.             val1 = 100;
  10.             val2 = 300;
  11. 	}
  12.         friend float mean(base ob);
  13.     };
  14.     float mean(base ob)
  15.     {
  16.         return float(ob.val1 + ob.val2) / 2;
  17.     }
  18.     int main()
  19.     {
  20.         base obj;
  21.         obj.get();
  22.         cout << mean(obj);
  23.         return 0;
  24.     }

a) 200
b) 150
c) 100
d) 300

View Answer

Answer: a [Reason:] In this program, We are finding the mean value by declaring the function mean as a friend of class base. Output: $ g++ friend3.cpp $ a.out 200

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         private:
  6.         int a, b;
  7.         public:
  8.         void test()
  9.         {
  10.             a = 100;
  11.             b = 200;
  12.         }
  13.         friend int compute(sample e1);
  14.     };
  15.     int compute(sample e1)
  16.     {
  17.         return int(e1.a + e1.b) - 5;
  18.     }
  19.     int main()
  20.     {
  21.         sample e;
  22.         e.test();
  23.         cout  << compute(e);
  24.         return 0;
  25.     }

a) 100
b) 200
c) 300
d) 295

View Answer

Answer: d [Reason:] In this program, we are finding a value from the given function by using the friend for compute function. Output: $ g++ friend4.cpp $ a.out 295

9. Pick out the correct statement.
a) A friend function may be a member of another class
b) A friend function may not be a member of another class
c) A friend function may or may not be a member of another class
d) None of the mentioned

View Answer

Answer: c

10. Where does keyword ‘friend’ should be placed?
a) function declaration
b) function definition
c) main function
d) none of the mentioned

View Answer

Answer: a [Reason:] The keyword friend is placed only in the function declaration of the friend function and not in the function definition because it is used toaccess the member of a class.

Set 5

1. Which keyword is used to define the user defined data types?
a) def
b) union
c) typedef
d) type

View Answer

Answer: c

2. Identify the correct statement.
a) typedef does not create different types. It only creates synonyms of existing types
b) typedef create different types
c) typedef create own types
d) none of the mentioned

View Answer

Answer: a [Reason:] By using typedef, we can create a type of pre-existing type only not our own type of data.

3. What does the data type defined by union will do?
a) It allow one different portion of memory to be accessed as same data types
b) It allow one same portion of memory to be accessed as same data types
c) It allow one different portion of memory to be accessed as different data types
d) It allow one same portion of memory to be accessed as different data types

View Answer

Answer: d [Reason:] Union is used to define the data types of our choice and it will store the data type in one location make them accessible.

4. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         typedef int num;
  6.         num a = 10, b = 15;
  7.         num c = a + b + a - b;
  8.         cout << c;
  9.         return 0;
  10.     }

a) 20
b) 15
c) 30
d) 25

View Answer

Answer: a [Reason:] In this program, we are manipulating the numbers and printing the result using user-defined data types. Output: $ g++ user.cpp $ a.out 20

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int i;
  6.         enum month 
  7.         {
  8.             JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC
  9.         };
  10.         for (i = JAN; i <= DEC; i++)
  11.             cout << i;
  12.         return 0;
  13.     }

a) 012345678910
b) 0123456789
c) 01234567891011
d) none of the mentioned

View Answer

Answer: a [Reason:] In this program, we are defined the data types as enumerator and printing its value in a order. Output: $ g++ user1.cpp $ a.out 012345678910

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         typedef int num;
  6.         typedef char let;
  7.         let w = "steve";
  8.         num a = 10, b = 15;
  9.         num c = a + w;
  10.         cout << c;
  11.         return 0;
  12.     }

a) 10steve
b) steve10
c) compile time error
d) compile but not run

View Answer

Answer: c [Reason:] Error: invalid conversion from ‘const char*’ to ‘let {aka char}’.

7. What is the syntax of user-defined data types?
a) typedef_existing data type_new name
b) typedef_new name_existing data type
c) def_existing data type_new name
d) none of the mentioned

View Answer

Answer: a

8. How many types of user-defined data type are in c++?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] There are three types of user-defined data types. They are typedef, union, enumerator.

9. What is the scope of typedef defined data types?
a) inside that block only
b) whole program
c) outside the program
d) none of the mentioned

View Answer

Answer: b [Reason:] We are defining the user-defined data type to be availed only inside that program, if we want to use anywhere means we have to define those types in the header file.

10. How many types of models are available to create the user-defined data type?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: b [Reason:] There are two types of models. They are references to built-in types and multipart types.