C++ MCQ
C++ MCQ
1. When the compiler cannot differentiate between two overloaded constructors, they are called A. C. overloaded ambiguous B. D. Destructed Dubious
Option : C
2. Adding a derived class to a base class requires fundamental changes to the base class A. True B. False
Option B
3. To expose a data member to the rest of the program, you must declare the data member in the _____ section of the class A. C. E. Common Public User B. D. Exposed Unrestricted
Option C
A. B. C. D.
all thrown objects no thrown objects any thrown object that has not been caught by an earlier catch block all thrown objects that have been caught by an earlier catch block
Option : C
5.
If you design a class that needs special initialization tasks, you will want to design a(n) _____ A. B. C. D. housekeeping routine Initialize Constructor Compiler
Option C
6.
Which of the following statements is false? A. A function is a block of code that performs a specific task Functions allow programmers to break large and complex problems into small and manageable tasks Functions allow programmers to use existing code to perform common tasks Functions can be called, or invoked, only once in a program Programmer-defined functions can be either value-returning or void
B.
C. D. E.
Option D
7.
C. D.
cannot be T for functions you create, but may be for C++'s built-in functions cannot be T
Option B
8.
When a child class function is called, the compiler looks first for a matching function name in the _____ A. B. C. D. class of the object using the function name immediate ancestor class base class descendant class
Option A
9. A function that is called automatically each time an object is destroyed is a A. C. Constructor Destroyer B. D. Destructor Terminator
Option B
10. If you create an instantiation of a class template with an int, and then create a second instantiation with a double, then A. you must precede each function call with the word int or double
B. C. D.
once a function is used as one type, it becomes unavailable for use with the other type there is no difference in the procedure to call a member function you cannot perform this operation in C++
Option A
11. Overloaded functions are required to A. B. C. D. have the same return type have the same number of parameters perform the same basic functions None of the above
Option D
12.
C++ allows you to define the same functions more than once in the same program _____ A. B. C. if the definitions are identical if the definitions are included in two separate #include files if the definitions are located in a single #include file that is included more than once C++ does not allow you to define the same functions more than once in the same program
D.
Option D
13. When you create a derived class and instantiate an object _____ A. B. C. D. the parent class object must be constructed first the child class object must be constructed first the parent class object must not be constructed the child class object must not be constructed
Option A
14. A function in a derived class that has the same name as a function in the parent class A. B. C. D. will override the base class function will cause an error message to display will be overridden by the base class function will execute immediately often the base class function executes
Option A
15. To hide a data member from the program, you must declare the data member in the _____ section of the class A. C. E. concealed hidden restricted B. D. Confidential Private
Option D
16.
You may override the class access specifier for_____ A. B. C. D. public members public and protected members any specific class members you choose no clas smembers
Option C
17.
Assume that your version of C++ can recognize only the first 8 characters of an identifier name, through identifier names may be arbitrarily long. Which of the following identifier names is not distinct: A. B. C. D. E. list, list2 address, Address identifier_l, identifier_2 answer, ANSWER None of the above
Option C
18.
When you define an object that is a member of a class, such as Student Abby; _____ A. B. C. D. a block of memory is set aside and all data members are assigned valid values a block of memory is set aside no memory is set aside until values are defined no memory is set aside, but valid values are assigned to data members
Option B
19.
An advantage of using local variables is that _____ A. they are known to all functions in the file names used in one function do not affect data stored in variables with the same names in other functions
B.
C. D.
values given to local variables are retained when those parameters go out of scope the program does not become "crowded" with too many variable names
Option D
20.
The difference between a return and a throw is that _____ A. B. C. D. with a throw, no value can be sent with a return, no value can be sent with a throw, execution takes place at the location from which the function was called with a return, execution takes place at the location from which the function was called
Option D
21.
You typically initialize a String variable to _____ A. B. C. D. an asterisk a space enclosed in single quotes the number 0 a zero-length string
Option D
22.
The prototype for a derived class constructor may include arguments for
A. B. C. D.
data members of the derived class data members of the base class both (a) and (b) neither (a) nor (b)
Option C
23.
You___________overload function templates A. B. C. D. may, as long as each version has the same arguments may, as long as each version has different arguments Must must not
Option B
24.
A member function uses the correct object when you call it because A. B. C. D. a copy of the object is passed to the function the address of the object is passed to the function the address of the function is passed to another function the address of the object is returned from the function
Option B
25. Function templates _____________ A. B. C. D. must have exactly one parameter may have more than one parameter as long as they are of the same type may have more than one parameter of any type may not have parameters
Option C
26. Private data can be accessed by A. B. C. D. class member functions functions in derived classes both (a) and (b) neither (a) nor (b)
Option A
27.
When a function performs tasks based on a decision, it has _____ A. B. C. D. functional cohesion coincidental cohesion logical cohesion no cohesion
Option C
28,
To create a template class, you begin with _________ A. B. C. D. the template definition the keyword class the function definitions the keyword definition
Option A
29. When the compiler places a copy of a small function's statements directly into a program, the function is said to be _____ A. C. overloaded inline B. D. Mangled Redundant
Option C
30. A function stub typically contains A. B. C. D. E. the function header the function braces the return statement, if it's value- returning function a display message All of the above
Option E
31.
_____ variables are declared outside of any statement block A. C. E. Area Local Value B. D. Global Reference
Option B
32.
If you omit any constructor argument when you instantiate an object, you must use default values A. B. C. D. for all parameters to the constructor for all parameters to the right of the argument for all parameters to the left of the argument for no other parameters
Option B
33.
Code that has already been tested is said to be _____ A. C. inherited reliable B. D. Reusable Polymorphic
Option C
34.
A class D can be derived from a class C, which is derived from a class B, which is derived from a class A A. True B. False
Option A
35.
A. C.
constructor destroyer
B. D.
Destructor Terminator
Option B
36.
We can output text to an object of class ofstream using the insertion operator because A. B. C. D. the ofstream class is a stream the insertion operator works with all classes we are actually outputting to cout the insertion operator is overloaded in ofstream
Option D
37.
he operator that allocates new memory is _____ A. B. C. D. Allocate Mem New Next
Option C
38.
Which is a good guideline for creating function names? A. B. C. D. Use all lowercase letters to identify the functions as C++ functions Use long names to reduce the likelihood of creating a duplicate function name Use abbreviations as much as possible to save both keystrokes and memory Avoid the use of digits because they are easily confused with letters
Option D
39.
Reserving memory during program execution is known as reserving it A. C. dynamically functionally B. D. Statically Powerfully
Option A
40.
When you write to a file rather than the screen, you use the C++ class _____ A. C. iostream fstream B. D. Filed Diskclass
Option C
41.
A function whose purpose is to send messages to other functions is known as a _____ A. C. dispatcher messenger B. D. Courier Sender
Option A
42.
The type of value that a function sends back to the function that calls it is known as its _____ A. Type
B. C. D.
Option B
43. Which of the following are never inherited? A. B. C. D. public data members constructor functions void functions overloaded + operators
Option B
44.
In a template function, _____ argument is generic, or parameterized A. B. C. D. No exactly one at least one more than one
Option C
45.
Assume that a program creates and initializes a Short Integer variable named age and a pointer named agePtr, to which it assigns the address of the age variable. Which of the following statements will assign the number 21 to the age variable? A. B. C. D. age = 21; *age = 21; agePtr = 21; *agePtr = 21;
E.
Option E
46.
The statement fwrite ( (char*)&objl, sizeof(objl) ); A. B. C. D. writes the member functions of objl to fl writes the data in objl to fl writes the member functions and me data of obj 1 to fl writes the address of objl to fl
Option B
47.
The loosest type of coupling is A. B. C. D. data coupling control coupling external coupling pathological coupling
Option A
48.
The main() function is always A. B. C. D. a called function a calling function recursive function used at the end of the program
E.
Option B
Option A
50.
If the description of function is "input the quantity, validate that the quantity is greater than 1, subtract 1 from the quantity, and print the quantity," the function is A. B. C. D. sequentially cohesive logically cohesive communicationally cohesive functionally cohesive
Option B
51.
A function that changes an object's state belongs to the category of A. B. C. D. inspector functions mutator functions auxiliary functions manager functions
Option B
52.
When you create a derived class and instantiate on object A. B. C. D. the parent class object must be constructed first the child class object must be constructed first the parent class object must not be constructed the child class object must not be constructed
Option A
53.
If you want to override constructor default values for an object you are instantiating, you must also override A. B. C. D. all other parameters to that constructor all parameters to the left of that value all parameters to the right of that value no other parameters to that constructor
Option B
54.
The return type for all destructors is A. B. C. D. the class Void the same as the first data in the class None
Option D
55.
When a program calls a function that has default parameters, if you omit an argument, you must _____ A. B. C. D. not omit any other arguments omit all arguments omit all arguments to the right of that argument omit all arguments to the left of that argument
Option C
a. a group function with the same name b. all have the same number and type of arguments c. functions with same name and same number and type of arguments
Option a
57.
How constructor differ from destructor a. constructors can be overloaded but destructors can't be overloaded b. constructors can take arguments but destructor can't c. both a and b
option c
58.
A. B. C.
Directly only through other private members of the class only through other public members of the class None of the above - the program cannot access the private members of a class in any way
D.
Option C
59.
The function whose prototype is void getData(Item *thing); receives A. B. C. D. a pointer to a structure a reference to a structure a copy of a structure Nothing
Option A.
60.
If you want only one memory location to be reserved for a class variable, no matter how many objects are instantiated, you should declare the variable as________ A. C. dynamic static B. D. Unary Volatile
Option C
C Questions
1. On executing the following program how many times would the message keep it up would get printed? #include<stdio.h> void main() { int x; for(x=1;x< =10;x++) { if(x<5) continue; else break; printf(keep it up); } } Choices: A. Infinite times B. 11 times C. 0 times D. Once E. 10 times Solution: C 2. Which of the following is the correct output for the program given below? #include<stdio.h> void main() { int i=1; switch(i); { printf(Hello); case 1: printf(\n Hi); break; case 2: printf(\n Bye); break; } } Choices:
A. Hello Hi B. Hello Bye C. Hi D. Bye E. Hello Hi Bye Solution: C 3. Which of the following is the correct output for the program given below? #include<stdio.h> void main() { int i=5; while(i-->=0) printf(%d,i); i=5; printf(\n); while(i -->=0) printf(%d,i); } Choices: A. 4 3 2 1 0 -1 4 3 2 1 0 -1 B. 5 4 3 2 1 0 543210 C. Error D. 5 4 3 2 1 0 543210 543210 Solution: A 4. Which of the following is the correct output for the program given below? #include<stdio.h> void main() { unsigned int i=65535; //assume a 2 byte integer
while(i++!=0) printf(%d,++i); } Choices: Choices: A. Infinite loop B. 0 1 2 ..... 65535 C. 1 2 .....32767 -32766 -32765 .... -1 0 D. No output
Solution: A 5. Which of the following statements are correct about the program given below? #include <stdio.h> void main() { int i=0; i++; if(i< =5); { printf(adds wings to your thoughts); exit(); main(); } } Choices: A. B. C. D. E. The program prints adds wings to your thoughts five times. The program prints adds wings to your thoughts infinite times. The program prints adds wings to your thoughts once. The call to main() after exit doesnt materialize. The compiler reports on error since main() cannot call itself. Solution: C and D Arrays and Pointers: 6. Which of the following is the correct output for the program given below? #include <stdio.h> void main() { int arr[1]={10}; printf(%d, 0[arr]);
} Choices: A. 1 B. 1 C. 0 D. None of the above Solution: B 7 . Which of the following is the correct output for the program given below?
#include <stdio.h> void main() { float a[]={12.4,2.3,4.5,6.7}; printf(\n%d, sizeof(a)/sizeof(a[0])); } Choices: A. 8 B. 16 C. 4 D. 1 Solution: C
8. Which of the following is the correct output for the program given below? #include <stdio.h> void main() { static int arr[] = [0,1,2,3,4}; int *p[]={arr,arr+1;arr+2;arr+3;arr+4}; int **ptr=p; ptr++; printf(\n%d%d%d,ptr-p,*ptr-arr,**ptr); *ptr++;
printf(\n%d%d%d,ptr-p,*ptr-arr,**ptr); *++ptr; printf(\n%d%d%d,ptr-p,*ptr-arr,**ptr); ++*ptr; printf(\n%d%d%d,ptr-p,*ptr-arr,**ptr); printf(\n%d%d%d,ptr-p,*ptr-arr,**ptr); } Choices: A. 0 0 0 111 222 333 B. 1 1 2 223 334 441 C. 1 1 1 222 333 344 D. 0 1 2 123 234 3 4 Garbage Solution: C 9. Which of the following is the correct output for the program given below? #include <stdio.h> void main() { static int a[2][2]={1,2,3,4}; int i,j; static int *p[]={int *)a,(int *)a+1,(int*)a+2; for(i=0;i<2;i++) { for(j=0;j<2;j++) printf(%d%d%d%d\n,
*(*(p+i)+j); *(*(j+p)+i); *(*(i+p)+j); *(*(p+j)+i); } } Choices: A. 1 1 1 1 2323 3232 4444 B.1212 2323 3434 4242 C. 1 1 1 1 2222 3333 4444 D. 1 2 3 4 2341 3412 4123 Solution: A
10 .Which of the following is the correct output for the program given below? #include <stdio.h> void main() { Int arr[2] [2] [2] ={10,2,3,4,5,6,7,8}; Int *p,*q; P=&arr[1][1][1];
*q=***arr; Printf(%d%d,*p,*q); } Choices: A. 8 10 B. 10 2 C. 8 1 D. Garbage values Solution: A 11 .Which of the following is the correct output for the program given below? #include <stdio.h> void main() { void *vp; char ch = 74, *cp=JACK; int j=65; vp = &ch; printf(%c,*(char*)vp); vp=&j; printf(%c,*(int*)vp); vp=cp; printf(%s,(char*)vp+2); } Choices: A. JCK B. J65K C. JAK D. JACK Solution: D
12 . Which of the following is the correct output for the program given below? #include <stdio.h> void main() { Static char *s[]={black,white,pink,violet}; Char **ptr[]={s+3,s+2,s+1,s},***p;
P=ptr; ++p; Printf(%s,**p+1); } Choices: A. Ink B. Ack C. Ite D. Let Solution: A 13. Which of the following is the correct output for the program given below? #include <stdio.h> Fun(void *p); Int i; void main() { Void *vptr; Vptr=&i; Fun(vptr); } Fun(void *p) { Int **q; Q=(int**)&p; Printf(%d,**q); } Choices: A. Error: Cannot convert from void ** to int** B. Garbage value C. 0 D. No output Solution: C 14. Which of the following is the correct output for the program given below? #include <stdio.h> void main() {
15. A pointer to a block of memory is effectively same as an array. A. True B. False Solution: True Strings:
16. Which of the following is the correct output for the program given below? #include<stdio.h> #include<string.h> void main() { char str1[20]=Hello, str2[2] =WORLD; printf(%s,strcpy(str2,strcat(str1,str2))); printf(%d,strlen(str1)); } Choices: A. Hello, 4 B. World,9 C. Hello World,4 D. WorldHello,3 Solution: C
17.
main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above
Ans: d, printf( ) prints address/garbage of i, scanf() dont hav & sign, so scans address for i +1, -1 dont hav any effect on code
18.
main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above
19. main()
{ int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above
Ans: d) 0
Ans: c) 1020
21.
Ans: b) 225
int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0
Ans: c) 100, 4, 0
23. union u { union u { int i; int j; }a[10]; int b[10]; }u; main() { printf("\n%d", sizeof(u)); printf(" %d", sizeof(u.a)); // printf("%d", sizeof(u.a[4].i)); } a. 4, 4, 4 b. 40, 4, 4 c. 1, 100, 1 d. 40 400 4
Ans: 20, 200, error for 3rd printf 24. main() { int (*functable[2])(char *format, ...) ={printf, scanf}; int i = 100; (*functable[0])("%d", i); (*functable[1])("%d", i); (*functable[1])("%d", i); (*functable[0])("%d", &i); } a. 100, Runtime error. b. 100, Random number, Random number, Random number. c. Compile error d. 100, Random number
25. main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000
Ans: c) Error becoz i/(*p) is 25/25 i.e 1 which is int & printed as a float, So abnormal program termination, runs if (float) i/(*p) -----> Type Casting
26. main() { int i, j; scanf("%d %d"+scanf("%d %d", &i, &j)); printf("%d %d", i, j); } a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered by the user
27. main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. Hello world c. Compile error d. hello world
28. main() { char * strA; char * strB = I am OK; memcpy( strA, strB, 6); }
Ans: c) I am OK is not in
" "
30. const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above
Ans: c)
31. struct Foo { char *pName; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); strcpy(obj->pName,"Your Name"); printf("%s", obj->pName); }
Ans a)
32. struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above
Ans: d) printd Nothing, as after free(obj), no memory is there containing obj->pName & pbj->pAddress
33. main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcat(a,b)); } a. Hello b. Hello World c. HelloWorld d. None of the above
Ans: b)
34. main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); }
35. void func1(int (*a)[10]) { printf("Ok it works"); } void func2(int a[][10]) { printf("Will this work?"); } main() { int a[10][10]; func1(a); func2(a); } a. Ok it works b. Will this work? c. Ok it worksWill this work? d. None of the above
Ans: a) 2, 2
37. main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); } a. 2 b. 100 c. 4 d. none of the above
Ans: d) -56
40.
void main () { int x = 10; printf ("x = %d, y = %d", x,--x++); } a. 10, 10 b. 10, 9 c. 10, 11 d. none of the above
41. main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10
Ans: c)
42. main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\n", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above
Ans: d) Infinite loop as x is decremented twice, it never be 0 and loop is going on & on 44 main() { int x=5; clrscr(); Fo
45. main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error
47.
48. main() { signed int bit=512, i=5; for(;i;i--) { printf("%d\n", bit >> (i - (i -1))); } } a. 512, 256, 0, 0, 0 b. 256, 256, 0, 0, 0 c. 512, 512, 512, 512, 512 d. 256, 256, 256, 256, 256
49. main() { if (!(1&&0)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above
Ans: a)
50. main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above
Ans: a)
51 main() { signed int bit=512, mBit; { mBit = ~bit; bit = bit & ~bit ; printf("%d %d", bit, mBit); } }
Ans: d)