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

a) 1
b) 8
c) 9
d) 0

View Answer

Answer: a

2. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         unsigned int a = 10;
  5.         a = ~a;
  6.         printf("%dn", a);
  7.     }

a) -9
b) -10
c) -11
d) 10

View Answer

Answer: c

3. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         if (7 & 8)
  5.         printf("Honesty");
  6.             if ((~7 & 0x000f) == 8)
  7.                 printf("is the best policyn");
  8.     }

a) Honesty is the best policy
b) Honesty
c) is the best policy
d) No output

View Answer

Answer: c

4. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a = 2;
  5.         if (a >> 1)
  6.            printf("%dn", a);
  7.     }

a) 0
b) 1
c) 2
d) No Output.

View Answer

Answer: c

5. Comment on the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i, n, a = 4;
  5.         scanf("%d", &n);
  6.         for (i = 0; i < n; i++)
  7.             a = a * 2;
  8.     }

a) Logical Shift left
b) No output
c) Arithmetic Shift right
d) bitwise exclusive OR

View Answer

Answer: b

6. What is the output of this C code?

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

a) x is 97
b) x is 98
c) x is 99
d) Run time error

View Answer

Answer: a

7. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 4, y, z;
  5.         y = --x;
  6.         z = x--;
  7.         printf("%d%d%d", x, y, z);
  8.     }

a) 3 2 3
b) 2 2 3
c) 3 2 2
d) 2 3 3

View Answer

Answer: d

8. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 4;
  5.         int *p = &x;
  6.         int *k = p++;
  7.         int r = p - k;
  8.         printf("%d", r);
  9.     }

a) 4
b) 8
c) 1
d) Run time error

View Answer

Answer: c

Set 2

1. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void foo(const int *);
  3.     int main()
  4.     {
  5.         const int i = 10;
  6.         printf("%d ", i);
  7.         foo(&i);
  8.         printf("%d", i);
  9.  
  10.     }
  11.     void foo(const int *i)
  12.     {
  13.         *i = 20;
  14.     }

a) Compile time error
b) 10 20
c) Undefined value
d) 10

View Answer

Answer: a [Reason:] Cannot change a const type value. Output: $ cc pgm1.c pgm1.c: In function ‘foo’: pgm1.c:13: error: assignment of read-only location ‘*i’

2. Comment on the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         const int i = 10;
  5.         int *ptr = &i;
  6.         *ptr = 20;
  7.         printf("%dn", i);
  8.         return 0;
  9.     }

a) Compile time error
b) Compile time warning and printf displays 20
c) Undefined behaviour
d) 10

View Answer

Answer: b [Reason:] Changing const variable through non-constant pointers invokes compiler warning Output: $ cc pgm2.c pgm2.c: In function ‘main’: pgm2.c:5: warning: initialization discards qualifiers from pointer target type $ a.out 20

3. What is the output of this C code?

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

a) 10
b) 11
c) Compile time error
d) 0

View Answer

Answer: c [Reason:] Variable j is not defined. Output: $ cc pgm3.c pgm3.c: In function ‘main’: pgm3.c:4: error: ‘j’ undeclared (first use in this function) pgm3.c:4: error: (Each undeclared identifier is reported only once pgm3.c:4: error: for each function it appears in.)

4. Does this compile without error?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         for (int k = 0; k < 10; k++);
  5.             return 0;
  6.     }

a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) None of the mentioned

View Answer

Answer: c [Reason:] Compilers implementing C90 does not allow this but compilers implementing C99 allow it. Output: $ cc pgm4.c pgm4.c: In function ‘main’: pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code

5. Does this compile without error?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int k;
  5.         {
  6.             int k;
  7.             for (k = 0; k < 10; k++);
  8.         }
  9.     }

a) Yes
b) No
c) Depends on the compiler
d) Depends on the C standard implemented by compilers

View Answer

Answer: a [Reason:] There can be blocks inside block and within blocks variables have only block scope. Output: $ cc pgm5.c

6. Which of the following declaration is not supported by C?
a) String str;
b) char *str;
c) float str = 3e2;
d) Both String str; & float str = 3e2;

View Answer

Answer: a [Reason:] It is legal in Java, not in C.

7.

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *var = "Advanced Training in C by institute";
  5.     }

Which of the following format identifier can never be used for the variable var?
a) %f
b) %d
c) %c
d) %s

View Answer

Answer: a [Reason:] %c can be used to print the indexed position. %d can still be used to display its ASCII value. %s is recommended. %f cannot be used.

Set 3

1. Choose the correct option.
    extern int i;
    int i;
a) both 1 and 2 declare i
b) 1 declares the variable i and 2 defines i
c) 1 declares and defines i, 2 declares i
d) 1 declares i,2 declares and defines i

View Answer

Answer: d [Reason:] The keyword extern is not a definition and is not allocated storage until it is initialized.

2. Pick the right option
    Statement 1:A definition is also a declaration.
    Statement 2:An identifier can be declared just once.
a) Statement 1 is true, Statement 2 is false
b) Statement 2 is true, Statement 1 is false
c) Both are false
d) Both are true

View Answer

Answer: b [Reason:] An identifier can be declared many times must be defined just once.

3. Which of the given statements are false.
1. extern int func;
2. extern int func2(int,int);
3. int func2(int,int);
4. extern class foo;
a) 3 and 4 only
b) 2 and 3 only
c) only 4
d) 2, 3 and 4

View Answer

Answer: c [Reason:] No extern are allowed for class declarations.

4. Pick the right option
    Statement 1:Global values are not initialized by the stream.
    Statement 2:Local values are implicitly initialised to 0.
a) Statement 1 is true, Statement 2 is false
b) Statement 2 is true, Statement 1 is false
c) Both are false
d) Both are true

View Answer

Answer: c [Reason:] Global values are implicitly initialised to 0, but local values have to be initialised by the system.

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int g = 100;
  4.     int main()
  5.     {
  6.         int a;
  7.         {
  8.             int b;
  9.             b = 20;
  10.             a = 35;
  11.             g = 65;
  12.            cout << b << a << g;
  13.         }
  14.         a = 50;
  15.         cout << a << g;
  16.         return 0;
  17.     }

a) 2035655065
b) 2035655035
c) 2035635065
d) none of the mentioned

View Answer

Answer: a [Reason:] The local values of a and g within the block are more dominant than the global values. Output: $ g++ dec1.cpp $ a.out 2035655065

6. Can two functions declare variables(non static) with the same name.
a) No
b) Yes
c) Yes, but not a very efficient way to write programs
d) No, it gives a runtime error

View Answer

Answer: c [Reason:] We can declare variables with the same name in two functions because their scope lies within the function.

7. What is the output of this program?

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

a) 234
b) 111
c) 123
d) 235

View Answer

Answer: a [Reason:] The variable that is declared as static has a file scope. Output:

  1. $ g++ dec2.cpp
  2. $ a.out
  3. 234

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 10;
  6.         if (a < 10) {
  7.             for (i = 0; i < 10; i++)
  8.                cout << i;
  9.         }
  10.         else {
  11.             cout << i;
  12.         }
  13.         return 0;
  14.     }

a) 0123456789
b) 123456789
c) 0
d) error

View Answer

Answer: d [Reason:] We will get compilation error because ‘i’ is an undeclared identifier.

9. Identify the incorrect statements.
    int var = 10;
    int *ptr = &(var + 1); //statement 1
    int *ptr2 = &var; //statement 2
    &var = 40; //statement 3
a) Statement 1 and 2 are wrong
b) Statement 2 and 3 are wrong
c) Statement 1 and 3 are wrong
d) All the three are wrong

View Answer

Answer: c [Reason:] In statement 1 lvalue is required as unary ‘&’ operand and in statement 3 lvalue is required as left operand.

10. Identify the type of the variables.
    typedef char* CHAR;
    CHAR p,q;
a) char*
b) char
c) CHAR
d) unknown

View Answer

Answer: a [Reason:] The statement makes CHAR a synonym for char*.

Set 4

1. What do container adapter provide to interface?
a) Restricted interface
b) More interface
c) No interface
d) None of the mentioned

View Answer

Answer: a [Reason:] A container adapter provides a restricted interface to a container.In particular, adapters do not provide iterators; they are intended to be used only through their specialized interfaces.

2. What does the sequence adaptor provide?
a) Insertion
b) Deletion
c) Interface to sequence container
d) None of the mentioned

View Answer

Answer: c

3. Which are presented in the container adaptors?
a) stack
b) queue
c) priority_queue
d) all of the mentioned

View Answer

Answer: d [Reason:] These mentioned things are presented in container adapters.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <queue>  
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         queue<int> myqueue;
  7.         myqueue.push(12);
  8.         myqueue.push(75);  
  9.         myqueue.back() -= myqueue.front();
  10.         cout << myqueue.back() << endl;
  11.         return 0;
  12.     }

a) 12
b) 75
c) 63
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We used the queue operation and performed the back operation. Because of that operation, We got the output as 63. Output: $ g++ sca.cpp $ a.out 63

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <queue>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         queue<int> myqueue;
  7.         int sum (0);
  8.         for (int i = 1; i <= 10; i++) 
  9.             myqueue.push(i);
  10.         while (!myqueue.empty())
  11.         {
  12.             sum += myqueue.front();
  13.             myqueue.pop();
  14.         }
  15.         cout << sum << endl;
  16.         return 0;
  17.     }

a) 51
b) 52
c) 54
d) 55

View Answer

Answer: d [Reason:] In this program, We used the push and pop operation of quueue to find out the total of all the number from 1 to 10. Output: $ g++ sca1.cpp $ a.out 55

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <queue>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         priority_queue<int> mypq;
  7.         mypq.push(30);
  8.         mypq.push(100);
  9.         mypq.push(25);
  10.         mypq.push(40);
  11.         while (!mypq.empty())
  12.         {
  13.             cout << " " << mypq.top();
  14.             mypq.pop();
  15.         }
  16.         cout << endl;
  17.         return 0;
  18.     }

a) 100 40 30 25
b) 100 40 30
c) 100 40
d) None of the mentioned

View Answer

Answer: a [Reason:] In this program, We used priority_queue and with that we are pushing and popping out the elements. Output: $ g++ sca2.cpp $ a.out 100 40 30 25

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <stack>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         stack<int> myints;
  7.         cout  << (int) myints.size();
  8.         for (int i = 0; i < 5; i++) myints.push(i);
  9.         cout  << (int) myints.size() << endl;
  10.         return 0;
  11.     }

a) 05
b) 15
c) 24
d) 102

View Answer

Answer: a [Reason:] In this program, We declared myints and not initialized in first option, So it’s value is 0 and on another, We are pushing 5 values, So it’s size is 5. Output: $ g++ sca3.cpp $ a.out 05

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <stack>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         stack<int> mystack;
  7.         mystack.push(10);
  8.         mystack.push(20);
  9.         mystack.top() -= 5;
  10.         cout << mystack.top() << endl;
  11.         return 0;
  12.     }

a) 10
b) 20
c) 13
d) 15

View Answer

Answer: d [Reason:] In this program, We used top option and this will return the reference to the next element. Output: $ g++ sca4.cpp $ a.out 15

9. In which context does the stack operates?
a) FIFO
b) LIFO
c) Both FIFO & LIFO
d) None of the mentioned

View Answer

Answer: b [Reason:] A stack is a container where elements operate in a LIFO context, where elements are inserted (pushed) and removed (popped) from the end of the container.

10. Which operator is used in priority queue?
a) operator<
b) operator>
c) operator)
d) none of the mentioned

View Answer

Answer: a

Set 5

1. Pick out the wrong header file about strings.
a) <string>
b) <regex>
c) <ios>
d) None of the mentioned

View Answer

Answer: c [Reason:] The standard header files for string is string and regex. So the wrong one presented here is ios.

2. Which is best for coding the standard libary for c++?
a) no trailing underscores on names
b) complex objects are returned by value
c) have a member-swap()
d) all of the mentioned

View Answer

Answer: d

3. What is meant by vector in the container library contains?
a) It is a sequence container that encapsulates dynamic size arrays
b) It is a sequence container that encapsulates static size arrays
c) It manages the memory
d) None of the mentioned

View Answer

Answer: a [Reason:] vector in the container library contains sequence container that manipulates and encapsulates dynamic size arrays.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         vector<int> v;
  7.         v.assign( 10, 42 );
  8.         for (int i = 0; i < v.size(); i++) 
  9.         {
  10.             cout << v[i] << " ";
  11.         }
  12.     }

a) 42
b) 42 42
c) 424
d) 42 for 10 times

View Answer

Answer: d [Reason:] In this program, We used the vector to print the 42 for 10 times. Output: $ g++ std.cpp $ a.out 42 42 42 42 42 42 42 42 42 42

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <list>
  3.     #include <queue>
  4.     using namespace std;
  5.     int main()
  6.     {
  7.         queue<char> q;
  8.         q.push('a');
  9.         q.push('b');
  10.         q.push('c');
  11.         cout << q.front();
  12.         q.pop();
  13.         cout << q.front();
  14.         q.pop();
  15.         cout << q.front();
  16.         q.pop();
  17.     }

a) ab
b) abc
c) a
d) error

View Answer

Answer: b [Reason:] In this program, We used the queue to process the given input. Output: $ g++ std1.cpp $ a.out abc

6. What is the output of this program?

  1.     #include <list>
  2.     #include <string>
  3.     #include <iostream>
  4.     using namespace std ;
  5.     typedef list<string> LISTSTR; 
  6.     int main()
  7.     {
  8.         LISTSTR :: iterator i;
  9.         LISTSTR test;
  10.         test.insert(test.end(), "one");
  11.         test.insert(test.end(), "two");
  12.         LISTSTR test2(test);
  13.         LISTSTR test3(3, "three");
  14.         LISTSTR test4(++test3.begin(),
  15.         test3.end());
  16.         cout << "test:";
  17.         for (i =  test.begin(); i != test.end(); ++i)
  18.             cout << " " << *i << endl;
  19.         cout << "test:";
  20.         for (i =  test2.begin(); i != test2.end(); ++i)
  21.             cout << " " << *i << endl;
  22.         cout << "test:";
  23.         for (i =  test3.begin(); i != test3.end(); ++i)
  24.             cout << " " << *i << endl;
  25.         cout << "test:";
  26.         for (i =  test4.begin(); i != test4.end(); ++i)
  27.             cout << " " << *i << endl;
  28.     }

a) test
b) test one
c) test two
d) none of the mentioned

View Answer

Answer: d [Reason:] In this program, We used the list to manipulate the given value. Output: $ g++ std3.cpp $ a.out test: one two test: one two test: three three three test: three three

7. Pick out the wrong header file.
a) <algorithm>
b) <containers>
c) <iterators>
d) <process>

View Answer

Answer: d

8. What is meant by standard c++ library?
a) It is the collection of class definitions for standard data structures and a collection of algorithms
b) It is a header file
c) Collection of algorithms
d) None of the mentioned

View Answer

Answer: a [Reason:] It is the collection of class definitions for standard data structures. This part of the library was derived from the Standard Template Library.

9. Pick out parameter for rehash method in unordered_set in c++?
a) count
b) size
c) hash
d) none of the mentioned

View Answer

Answer: a [Reason:] count is used to return the new number of buckets.

10. What is the use of <exception> header
a) Contains the standard exception files
b) Contains the standard library files
c) It is used to arise an exception in the program
d) None of the mentioned

View Answer

Answer: a