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

Here is a listing of online C test questions on “While Loops” along with answers, explanations and/or solutions:

1. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         while ()
  5.             printf("In while loop ");
  6.         printf("After loopn");
  7.     }

a) In while loop after loop
b) After loop
c) Compile time error
d) Infinite loop

View Answer

Answer: c

2. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         do
  5.             printf("In while loop ");
  6.         while (0);
  7.             printf("After loopn");
  8.     }

a) In while loop
b) In while loop
after loop
c) After loop
d) Infinite loop

View Answer

Answer: b

3. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         do {
  6.             i++;
  7.             printf("In while loopn");
  8.         } while (i < 3);
  9.     }

a) In while loop
In while loop
In while loop
b) In while loop
In while loop
c) Depends on the compiler
d) Compile time error

View Answer

Answer: a

4. How many times i value is checked in the below code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         do {
  6.             i++;
  7.             printf("in while loopn");
  8.         } while (i < 3);
  9.     }

a) 2
b) 3
c) 4
d) 1

View Answer

Answer: b

5. How many times i value is checked in the below code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         while (i < 3)
  6.             i++;
  7.         printf("In while loopn");
  8.     }

a) 2
b) 3
c) 4
d) 1

View Answer

Answer: c

6. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int i = 2;
  5.         do
  6.         {
  7.             printf("Hi");
  8.         } while (i < 2)
  9.     }

a) Compile time error
b) Hi Hi
c) Hi
d) Varies

View Answer

Answer: a

7. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int i = 0;
  5.         while (++i)
  6.         {
  7.             printf("H");
  8.         }
  9.     }

a) H
b) H is printed infinite times
c) Compile time error
d) Varies

View Answer

Answer: b

8. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int i = 0;
  5.         do
  6.         {
  7.             printf("Hello");
  8.         } while (i != 0);
  9.     }

a) Nothing
b) H is printed infinite times
c) Hello
d) Run time error

View Answer

Answer: c

Set 2

1. To declare a 3 dimension array using pointers, which of the following is the correct syntax:
a) char *a[][];
b) char **a[];
c) char ***a;
d) all of the mentioned

View Answer

Answer: a

2. Comment on the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *a = {"p", "r", "o", "g", "r", "a", "m"};
  5.         printf("%s", a);
  6.     }

a) Output will be program
b) Output will be p
c) No output
d) Compile-time error

View Answer

Answer: b

3. An array of strings can be initialized by:
a) char *a[] = {“Hello”, “World”};
b) char *a[] = {“Hello”, “Worlds”};
c) char *b = “Hello”;
    char *c = “World”;
    char *a[] = {b, c};
d) All of the mentioned

View Answer

Answer: d

4. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         int i = 0;
  6.         for (i = 0;i < 10; i++)
  7.         printf("%s", *(a[i]));
  8.     }

a) segmentation fault
b) hi hello how followed by 7 null values
c) 10 null values
d) depends on compiler

View Answer

Answer: a

5. What is the output of this C code?

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

a) hi hello how Segmentation fault
b) hi hello how followed by 7 null values
c) hey hello how Segmentation fault
d) depends on compiler

View Answer

Answer: c

6. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         printf("%dn", sizeof(a));
  6.     }

a) 10
b) 13
c) Run time error
d) 40

View Answer

Answer: d

7. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         printf("%dn", sizeof(a[1]));
  6.     }

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

View Answer

Answer: b

8. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         int i = 0;
  6.         for (i = 0;i < 10; i++)
  7.         printf("%s", a[i]);
  8.     }

a) hi hello how Segmentation fault
b) hi hello how null
c) hey hello how Segmentation fault
d) hi hello how followed by 7 nulls

View Answer

Answer: d

Set 3

1. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int a[3] = {1, 2, 3};
  5.         int *p = a;
  6.         printf("%pt%p", p, a);
  7.     }

a) Same address is printed.
b) Different address is printed.
c) Compile time error
d) Nothing

View Answer

Answer: a

2. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *s = "hello";
  5.         char *p = s;
  6.         printf("%pt%p", p, s);
  7.     }

a) Different address is printed
b) Same address is printed
c) Run time error
d) Nothing

View Answer

Answer: b

3. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *s= "hello";
  5.         char *p = s;
  6.         printf("%ct%c", p[0], s[1]);
  7.     }

a) Run time error
b) h h
c) h e
d) h l

View Answer

Answer: c

4. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *s= "hello";
  5.         char *p = s;
  6.         printf("%ct%c", *(p + 3),  s[1]);
  7.     }

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

View Answer

Answer: d

5. What is the output of this C code?

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

a) h h
b) Run time error
c) l l
d) e e

View Answer

Answer: d

6. What is the output of the code given below?

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

a) 10 10
b) Compile time error
c) 10 1
d) Undefined behaviour

View Answer

Answer: c

7. What is the output of the code given below?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int ary[4] = {1, 2, 3, 4};
  5.         int *p = ary + 3;
  6.         printf("%dn", p[-2]);
  7.     }

a) 1
b) 2
c) Compile time error
d) Some garbage value

View Answer

Answer: b

8. What is the output of the code given below?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int ary[4] = {1, 2, 3, 4};
  5.         int *p = ary + 3;
  6.         printf("%d %dn", p[-2], ary[*p]);
  7.     }

a) 2 3
b) Compile time error
c) 2 4
d) 2 somegarbagevalue

View Answer

Answer: d

Set 4

1. Which header file is used to manipulate the string?
a) iostream
b) iomanip
c) string
d) container

View Answer

Answer: c [Reason:] To use the string class, We have to use #include header file.

2. How many maximum number of parameters does a string constructor can take?
a) 1
b) 2
c) 3
d) 4

View Answer

Answer: c [Reason:] string( other_string, position, count ). It is a type of constructor for the string.

3. Which constant member functions does not modify the string?
a) bool empty()
b) assign
c) append
d) none of the mentioned

View Answer

Answer: a [Reason:] Because bool empty is a constant member function, So it can’t be modified.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("I like to code in C");
  7.         unsigned sz = str.size();
  8.         str.resize (sz + 2, '+');
  9.         str.resize (14);
  10.         cout << str << 'n';
  11.         return 0;
  12.     }

a) I like to code in c
b) I like to code
c) I like to code in c++
d) None of the mentioned

View Answer

Answer: b [Reason:] In this program, We are resizing the string by adding + and then we are resizing it to 14. Output: $ g++ basicst.cpp $ a.out I like to code

5. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Steve jobs");
  7.         cout << str.capacity() << "n";
  8.         return 0;
  9.     }

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

View Answer

Answer: b [Reason:] In this program, We are finding the capacity that the str can hold. Output: $ g++ basicst1.cpp $ a.out 10

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Steve jobs founded the apple");
  7.         string str2 ("apple");
  8.         unsigned found = str.find(str2);
  9.         if (found != string :: npos)
  10.             cout << found << 'n';
  11.         return 0;
  12.     }

a) apple
b) 12
c) 23
d) Steve jobs founded the

View Answer

Answer: c [Reason:] In this program, We are finding a string by using the find method. Output: $ g++ basicst2.cpp $ a.out 23

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Steve jobs");
  7.         unsigned found = str.find_first_of("aeiou");
  8.         while (found != string :: npos)
  9.         {
  10.             str[found] = '*';
  11.             found = str.find_first_of("aeiou", found + 1);
  12.         }
  13.         cout << str << 'n';
  14.         return 0;
  15.     }

a) Steve
b) jobs
c) St*v* j*bs
d) St*v*

View Answer

Answer: c [Reason:] In this program, We are replacing the vowels with a asterisk by using find_first_of method. Output: $ g++ basicst3.cpp $ a.out St*v* j*bs

8. What is the output of this program?

  1.     #include <iostream>  
  2.     #include <cstring>
  3.     #include <string>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         string str ("Steve jobs");
  8.         char * cstr = new char [str.length() + 1];
  9.         strcpy (cstr, str.c_str());
  10.         char * p = strtok (cstr," ");
  11.         while (p != 0)
  12.         {
  13.             cout << p << 'n';
  14.             p = strtok(NULL," ");
  15.         }
  16.         delete[] cstr;
  17.         return 0;
  18.     }

a) Steve jo
b) Steve jobs
c) Steve
jobs
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are breaking up the strings into the form of tokens. Output: $ g++ basicst4.cpp $ a.out Steve jobs

9. What is the difference between unsigned int length() and unsigned int size()?
a) Returns a different value
b) They are same
c) Returns a different value but they are same
d) None of the mentioned

View Answer

Answer: b [Reason:] Both of them will return the length of strings in same notations.

10. How many parameters can a resize method take?
a) 1
b) 2
c) 1 or 2
d) 2

View Answer

Answer: c [Reason:] There can be one or two parameters in resize method. They are string length and an optional new character to be inserted.

Set 5

1. Pick out the in correct type of function in header file.
a) Partitions
b) Sort
c) Merge
d) Join

View Answer

Answer: d [Reason:] First three type of options are available in header file.

2. What type of algorithm is not available in creating our own STL style algorithms?
a) copy_if()
b) remove_copy_if()
c) sort
d) none of the mentioned

View Answer

Answer: a

3. What is meant by hash tables in C++?
a) Array data structure
b) Keyed array data structure
c) Data structure
d) None of the mentioned

View Answer

Answer: b [Reason:] In hash table, every value will have a key, So that it can be accessed easily.

4. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main ()  
  6.     {
  7.         int myints[] = {10, 20, 30, 5, 15};
  8.         vector<int> v(myints, myints + 5);
  9.         make_heap (v.begin(), v.end());
  10.         pop_heap (v.begin(), v.end());
  11.         v.pop_back();
  12.         cout << v.front() << 'n';
  13.         return 0;
  14.     }

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

View Answer

Answer: b [Reason:] In this program, We are forming a heap with the vector and then we are popping one element and finding the maximum element in the heap. Output: $ g++ style.cpp $ a.out 20

5. What is the output of this program?

  1.     #include <iostream> 
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         int first[] = {5, 10, 15, 20, 25};
  8.         int second[] = {50, 40, 30, 20, 10};
  9.         vector<int> v(10);
  10.         vector<int> :: iterator it;
  11.         sort (first, first + 5);
  12.         sort (second, second + 5);
  13.         it = set_union (first, first + 5, second, second + 5, v.begin());  
  14.         v.resize(it-v.begin());
  15.         for (it = v.begin(); it != v.end(); ++it)
  16.             cout << ' ' << *it;
  17.         cout << 'n';
  18.         return 0;
  19.     }

a) 5 10 15
b) 20 25 30
c) 40 50
d) All of the mentioned

View Answer

Answer: d [Reason:] In this kind of style algorithm, We are finding the elements in the both the vector by using set_union function. Output: $ g++ style1.cpp $ a.out 5 10 15 20 25 30 40 50

6. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     bool myfunction (int i, int j)
  6.     { 
  7.         return (i < j);
  8.     }
  9.     struct myclass {
  10.     bool operator() (int i, int j)
  11.     {
  12.         return (i < j);
  13.     } 
  14.     } myobject;
  15.     int main () 
  16.     {
  17.         int myints[] = {10, 9, 8};
  18.         vector<int> myvector (myints, myints + 3);
  19.         sort (myvector.begin(), myvector.begin() + 2);
  20.         sort (myvector.begin() + 1, myvector.end(), myfunction);
  21.         sort (myvector.begin(), myvector.end(), myobject);
  22.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  23.             cout << ' ' << *it;
  24.         return 0;
  25.     }

a) 8 9 10
b) 10 8 9
c) 9 8 10
d) None of the mentioned

View Answer

Answer: a [Reason:] In this style algorithm, We have sorted the elements in the vector by using the sort method. Output: $ g++ style2.cpp $ a.out 8 9 10

7. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     bool myfunction (int i,int j) 
  6.     {
  7.         return (i < j);
  8.     }
  9.     int main () 
  10.     {
  11.         int myints[] = {9, 8, 7, 6};
  12.         vector<int> myvector (myints, myints + 4);
  13.         partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end());
  14.         partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end(),
  15.         myfunction);
  16.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  17.             cout << ' ' << *it;
  18.         return 0;
  19.     }

a) 6 7 8 9
b) 9 8 6 7
c) 6 7 9 8
d) None of the mentioned

View Answer

Answer: c [Reason:] In this program, We are partial sorting the vector by using the partial sort method. Output: $ g++ style3.cpp $ a.out 6 7 9 8

8. What is the output of this program?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         int first[] = {5, 10, 15};
  8.         int second[] = {50, 40, 30};
  9.         vector<int> v(4);
  10.         vector<int> :: iterator it;
  11.         sort (first, first + 3);
  12.         sort (second, second + 3);
  13.         it = set_symmetric_difference (first, first + 2, second, second + 2, 
  14.         v.begin());
  15.         v.resize(it - v.begin());
  16.         for (it = v.begin(); it != v.end(); ++it)
  17.         cout << ' ' << *it;
  18.         return 0;
  19.     }

a) 5 10
b) 30 40
c) 50 40
d) 5 10 30 40

View Answer

Answer: d [Reason:] In this style algorithm, We are finding the symmetric difference between the vectors and printing it. Output: $ g++ style4.cpp $ a.out 5 10 30 40

9. What is the use of includes function in c++?
a) Compares two ranges of data
b) Compares two sorted ranges of data
c) Includes a new element in the range
d) None of the mentioned

View Answer

Answer: b [Reason:] Returns true if the first sorted range contains all the elements in the second sorted range.

10. How many parameters are required for sort_heap function?
a) 1
b) 2
c) 2 or 3
d) 3

View Answer

Answer: c [Reason:] There are three parameters required for sort_heap. There are first element in heap and last element in heap and an optional compare.