question number 1

28
#include <iostream> using namespace std; int main () /*question number 1*/ Code: int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a; What number will z in the sample code above contain? Choice 1 5 Choice 2 6 Choice 3 10 [Ans] Choice 4 11 Choice 5 12 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /*question number 2*/ "My salary was increased by 15%!" Select the statement which will EXACTLY reproduce the line of text above. Choice 1 cout<<"My salary was increased by 15/%!" Choice 2 cout>>"My salary was increased by 15%!"; Choice 3 cin<<"My salary was increased by 15’%’"; Choice 4 cin>>"My salary was increased by 15%"; Choice 5 cout<<"My salary was increased by 15/%!"<<endl; [Ans] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /*question number 3*/ What is a difference between a declaration and a

Upload: steve-mus

Post on 03-Apr-2015

503 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: question number 1

#include <iostream>using namespace std;int main ()

/*question number 1*/Code:

int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a;

What number will z in the sample code above contain? Choice 15 Choice 26 Choice 310 [Ans] Choice 411 Choice 512 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 2*/"My salary was increased by 15%!" Select the statement which will EXACTLY reproduce the line of text above. Choice 1cout<<"My salary was increased by 15/%!"Choice 2cout>>"My salary was increased by 15%!"; Choice 3cin<<"My salary was increased by 15’%’"; Choice 4cin>>"My salary was increased by 15%";Choice 5cout<<"My salary was increased by 15/%!"<<endl; [Ans]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 3*/What is a difference between a declaration and a definition of a variable? Choice 1Both can occur multiple times, but a declaration must occur first. Choice 2There is no difference between them. Choice 3A definition occurs once, but a declaration may occur many times. Choice 4A declaration occurs once, but a definition may occur many times. [Ans] Choice 5Both can occur multiple times, but a definition must occur first. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Page 2: question number 1

/*question number 4*/ int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; What value does testarray[2][1][0] in the sample code above contain? Choice 13 Choice 25 Choice 37 Choice 49 Choice 511[Ans]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 5*/Code:

int a=10,b;b=a++ + ++a;cout<<b<<”,”<<a++<<”,”<<a<<”,”<<++a<<endl;

what will be the output when following code is executedChoice 112,10,11,13Choice 222,10,11,13Choice 322,11,11,11Choice 412,11,11,11Choice 522,13,13,13[Ans]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 6*/Code:

void myFunc (int x) { if (x > 0) myFunc(--x); cout<<x<<”,”; } int main() { myFunc(5); return 0; }

What will the above sample code produce when executed? Choice 11, 2, 3, 4, 5, 5,

Page 3: question number 1

Choice 24, 3, 2, 1, 0, 0, Choice 35, 4, 3, 2, 1, 0, Choice 40, 0, 1, 2, 3, 4, [Ans] Choice 50, 1, 2, 3, 4, 5, - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 7*/11 ^ 5 What does the operation shown above produce? Choice 11 Choice 26 Choice 38 Choice 414 [Ans] Choice 515 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 8*/#define MAX_NUM 15 Referring to the sample above, what is MAX_NUM? Choice 1MAX_NUM is an integer variable. Choice 2MAX_NUM is a linker constant. Choice 3MAX_NUM is a precompiler constant. Choice 4MAX_NUM is a preprocessor macro. [Ans] Choice 5MAX_NUM is an integer constant. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 9*/int x = 2 * 3 + 4 * 5; What value will x contain in the sample code above? Choice 122 Choice 226[Ans] Choice 346 Choice 4

Page 4: question number 1

50 Choice 570 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 10*/int var1; If a variable has been declared with file scope, as above, can it safely be accessed globally from another file? Choice 1Yes; it can be referenced through the register specifier. Choice 2No; it would have to have been initially declared as a static variable. Choice 3No; it would need to have been initially declared using the global keyword.[Ans] Choice 4Yes; it can be referenced through the publish specifier. Choice 5Yes; it can be referenced through the extern specifier.

/*question number 11*/C is which kind of language? Choice 1Machine Choice 2Procedural[Ans] Choice 3Assembly Choice 4Object-oriented Choice 5Strictly-typed - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 12*/Code:

int i,j; int ctr = 0; int myArray[2][3]; for (i=0; i<3; i++) for (j=0; j<2; j++) { myArray[j][i] = ctr; ++ctr; }

What is the value of myArray[1][2]; in the sample code above? Choice 11 Choice 22

Page 5: question number 1

Choice 33 Choice 44 Choice 55 [Ans] 00,10,01,11,12 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 13*/Code:

int x = 0; for (x=1; x<4; x++); cout<<"x="<<x<<endl;

What will be printed when the sample code above is executed? Choice 1x=0 Choice 2x=1 Choice 3x=3 Choice 4x=4[Ans] Choice 5x=5 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 14*/Code:

int x = 3; if( x == 2 ); x = 0; if( x == 3 ) x++; else x += 2;

What value will x contain when the sample code above is executed? Choice 11 Choice 22[Ans] Choice 33 Choice 44 Choice 55 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 15*/Code:

Page 6: question number 1

int x = 5; int y = 2; char op = '*'; switch (op) { default : x += 1; case '+' : x += y; /*It will go to all the cases*/ case '-' : x -= y; }

After the sample code above has been executed, what value will the variable x contain? Choice 14 Choice 25 Choice 36 [Ans] Choice 47 Choice 58 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 16*/Code:

x = 3, counter = 0; while ((x-1)) { ++counter; x--; }

Referring to the sample code above, what value will the variable counter have when completed? Choice 10 Choice 21 Choice 32[Ans] Choice 43 Choice 54

/*question number 17*/Code:

short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} }; cout<<sizeof( testarray );

Page 7: question number 1

Assuming a short is two bytes long, what will be printed by the above code? Choice 1It will not compile because not enough initializers are given. Choice 26 Choice 37 Choice 412 Choice 524 [Ans]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 18*/char buf [] = "Hello world!";

char * buf = "Hello world!"; In terms of code generation, how do the two definitions of buf, both presented above, differ? Choice 1The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not. Choice 2The first definition is not suitable for usage as an argument to a function call; the second definition is. Choice 3The first definition is not legal because it does not indicate the size of the array to be allocated; the second definition is legal. Choice 4They do not differ -- they are functionally equivalent. [Ans]Choice 5The first definition does not allocate enough space for a terminating NUL-character, nor does it append one; the second definition does. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 19*/Which one of the following will read a character from the keyboard and will store it in the variable c? Choice 1c = getc(); Choice 2getc( &c ); Choice 3c = getchar( stdin ); Choice 4getchar( &c ) Choice 5c = getchar(); [Ans] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Page 8: question number 1

/*question number 20*/Code:

#include <iostream>Using namespace std; int i; void increment( int i ) { i++; }

int main() { for( i = 0; i < 10; increment( i ) ) { } Cout<<"i="<<i<<endl; return 0; }

What will happen when the program above is compiled and executed? Choice 1It will not compile. Choice 2It will print out: i=9. Choice 3It will print out: i=10. Choice 4It will print out: i=11. Choice 5It will loop indefinitely.[Ans] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 21*/Code:

int i = 4; switch (i) { default: ; case 3: i += 5; if ( i == 8) { i++; if (i == 9) break; i *= 2; } i -= 4; break; case 8: i += 5; break;} printf("i = %d\n", i);

Page 9: question number 1

What will the output of the sample code above be? Choice 1i = 5[Ans] Choice 2i = 8 Choice 3i = 9 Choice 4i = 10 Choice 5i = 18 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 22*/How do you include a system header file called sysheader.h in a C source file? Choice 1#include <sysheader.h> [Ans]Choice 2#incl "sysheader.h" Choice 3#includefile <sysheader> Choice 4#include sysheader.h Choice 5#incl <sysheader.h>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 23*/Code:

int x = 0; for ( ; ; ) { if (x++ == 4) break; continue; } Cout<<"x="<<x<<endl;

What will be printed when the sample code above is executed? Choice 1x=0 Choice 2x=1 Choice 3x=4 Choice 4x=5[Ans] Choice 5

Page 10: question number 1

x=6 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 24*/According to the Standard C++ specification, what are the respective minimum sizes (in bytes) of the following three data types: short; int; and long? Choice 11, 2, 2 Choice 21, 2, 4 Choice 31, 2, 8 Choice 42, 2, 4[Ans] Choice 52, 4, 8 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 25*/char txt [20] = "Hello world!\n"; How many bytes are allocated by the definition above? Choice 111 bytes Choice 212 bytes Choice 313 bytes Choice 420 bytes[Ans] Choice 521 bytes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 26*/Code:

int i = 4; int x = 6; double z; z = x / i; cout<<"z="<<z<<endl;

What will print when the sample code above is executed? Choice 1z=0.00 Choice 2z=1.00[Ans] Choice 3z=1.50 Choice 4z=2.00 Choice 5

Page 11: question number 1

z=NULL - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 27*/int a [8] = { 0, 1, 2, 3 }; The definition of a above explicitly initializes its first four elements. Which one of the following describes how the compiler treats the remaining four elements? Choice 1Standard C++ defines this particular behavior as implementation-dependent. The compiler writer has the freedom to decide how the remaining elements will be handled. Choice 2The remaining elements are initialized to zero(0).[Ans] Choice 3It is illegal to initialize only a portion of the array. Either the entire array must be initialized, or no part of it may be initialized. Choice 4As with an enum, the compiler assigns values to the remaining elements by counting up from the last explicitly initialized element. The final four elements will acquire the values 4, 5, 6, and 7, respectively. Choice 5They are left in an uninitialized state; their values cannot be relied upon. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 28*/Code:

long factorial (long x) { ???? return x * factorial(x - 1); }

With what do you replace the ???? to make the function shown above return the correct answer? Choice 1if (x == 0) return 0; Choice 2return 1; Choice 3if (x >= 2) return 2; Choice 4if (x == 0) return 1; Choice 5if (x <= 1) return 1; [Ans]{more probable} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 30*/How is a variable accessed from another file? Choice 1The global variable is referenced via the extern specifier.[Ans]

Page 12: question number 1

Choice 2The global variable is referenced via the auto specifier. Choice 3The global variable is referenced via the global specifier. Choice 4The global variable is referenced via the pointer specifier. Choice 5The global variable is referenced via the ext specifier. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 31*/Which one of the following is NOT a valid identifier? Choice 1__ident Choice 2auto [Ans]Choice 3bigNumber Choice 4g42277 Choice 5peaceful_in_space - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 32*/Which one of the following is NOT a valid C++ identifier? Choice 1___S Choice 21___ [Ans]Choice 3___1 Choice 4___ Choice 5S___ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 33*/According to Standard C++, what is the type of an unsuffixed floating-point literal, such as 123.45? Choice 1long double Choice 2Unspecified Choice 3float[Ans] Choice 4double Choice 5

Page 13: question number 1

signed float - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 34*/short int x; /* assume x is 16 bits in size */ What is the maximum number that can be printed using printf("%d\n", x), assuming that x is initialized as shown above? Choice 1127 Choice 2128 Choice 3255 Choice 432,767 [Ans] Choice 565,536 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 35*/What number is equivalent to -4e3? Choice 1-4000 [Ans] Choice 2-400 Choice 3-40 Choice 4.004 Choice 5.0004 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 36*/How does variable definition differ from variable declaration? Choice 1Definition allocates storage for a variable, but declaration only informs the compiler as to the variable's type. Choice 2Declaration allocates storage for a variable, but definition only informs the compiler as to the variable's type. Choice 3Variables may be defined many times, but may be declared only once.[Ans] Choice 4Variable definition must precede variable declaration. Choice 5There is no difference in C between variable declaration and variable definition. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Page 14: question number 1

/*question number 37*/Code:

#include <iostream> using namespace std;void func() { int x = 0; static int y = 0; x++; y++; cout<<x<<y; }

int main() { func(); func(); return 0; }

What will the code above print when it is executed? Choice 11 -- 1 1 -- 1 Choice 21 -- 1 2 -- 1 Choice 31 -- 1 2 -- 2 Choice 41 -- 0 1 -- 0 Choice 51 -- 1 1 -- 2 [Ans]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 38*/short int x; /* assume x is 16 bits in size */ What is the maximum number that can be printed using cout<< x, assuming that x is initialized as shown above? Choice 1127 Choice 2128 Choice 3255 Choice 432,767[Ans] Choice 565,536 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Page 15: question number 1

/*question number 39*/Code:

int m = -14; int n = 6; int o; o = m % ++n; n += m++ - o; m <<= (o ^ n) & 3;

Assuming two's-complement arithmetic, which one of the following correctly represents the values of m, n, and o after the execution of the code above? Choice 1m = -26, n = -7, o = 0 [Ans]Choice 2m = -52, n = -4, o = -2 Choice 3m = -26, n = -5, o = -2 Choice 4m = -104, n = -7, o = 0 Choice 5m = -52, n = -6, o = 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*question number 40*/Which one of the following will define a function that CANNOT be called from another source file? Choice 1void function() { ... } Choice 2extern void function() { ... } Choice 3const void function() { ... } Choice 4private void function() { ... } [Ans]Choice 5static void function() { ... }

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 41*/

The following is an example of what type of error? int num1;int num2;cout << "Enter two numbers: ";cin >> num1 >> num1;

1. syntax error2. semantic error3. logic error4. compile-time error5. no errors [Ans]

Page 16: question number 1

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 42*/

Which of the following is NOT a valid identifier (i.e. CANNOT be used as a name for a variable).

1. phone_number2. EXTRACREDIT3. DOUBLE4. my course [Ans]5. All are invalid

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 43*/

Which statement has a syntax error?

1. double a = 3.5 + 4;2. cout << b + c;3. x + y = total; [Ans]4. double Main;5. All of the above.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 44*/

What is the output of the following statement?

int num = 26;cout << "Here it is." << num << "\n There it is.";

1. Here it is.There it is.2. Here it is.26nThere it is.3. Here it is.26 [Ans]

There it is.4. Here it is.

26There it is.

5. Here it is.26\nThere it is.

Page 17: question number 1

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 45*/

Assuming that the user types 14.92 followed by a return, what is the output of the following code:

int num;cout << "Enter a number: ";cin >> num;cout << num;

1. 02. 14 [Ans]3. 14.924. 155. None of the above

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 46*/

What is the result of the following code:

int a = 53;int b = 6;cout << a / b;

1. 8.8332. 93. 8 [Ans]4. 55. None of the above

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 47*/

We want to translate the following algebraic expression into a C++ arithmetic expression. Which of the following is the correct form?

1 1-a--- + -------- c 1+b

1. 1 / c + 1 - a / 1 + b2. 1 / c + (1 - a / 1 + b)3. (1 / c) + (1 - a / 1 + b)4. 1 / c + (1 - a) / (1 + b) [Ans]5. None of the above

Page 18: question number 1

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 48*/

What is the result of the following arithmetic expression? 7.0 * 3 % 2

1. 7.02. 03. 1.04. 10.55. invalid expression [Ans]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 49*/

What is the output of the following code?

int a = 28;int b = 19;int c = 3;int d = 2;cout << a % b % c / d;

1. 1.52. 13. 0.254. 0 [Ans]5. None of the above

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 50*/What is the output of the following code?

int a = 9;int b = 6;double c = 2.2;cout << 4.4 + c * (a / b);

1. 7.72. 6.6 [Ans]3. 9.94. 115. None of the above

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 51*/

What is the output of the following code?

string fun = "I love C++";cout << fun.length();

1. 1

Page 19: question number 1

2. 93. 10 [Ans]4. 85. None of the above

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 52*/

Given that str = "Snoozing under a tree", what would be the result of the following statement

cout << str.substr(1, 1) + str.substr(9, 2);

1. Sun 2. nun3. nnd4. S u [Ans]5. None of the above

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 53*/

What does the following output produce? (Note: a dash is representing a space.)

double x = 1.0;double y = 777.777;cout << fixed << setprecision(2) << setw(8);cout << x << "\n";cout << y << "\n";

1. ----1.00777.78

2. -------1777.78

3. ----1.00--777.78

4. ----1.00777.777

5. None of the above [Ans]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 54*/

Given the format of the if-else structure below, one and only one of the statement sets inside of the bodies of the if and else will be executed.

if (------){ stmt set 1;}else

Page 20: question number 1

{ stmt set 2;}

(T/F) (T)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 55*/

Given the following code, and that the user entered the string "Goodbye", what will be the output?

string str = "Hello";string word;cout << "Enter a word: ";cin >> word;if (word < str){ str = word;}cout << str;

Hello [Ans]

Goodbye

None of the above

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 56*/

Given the following code, what will be the output?

int x = 1;int y = 2;if (x < y){ cout << x;}cout << y;

1. 1 2. 23. x4. y5. 12 [Ans]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 57*/

What will be the output of the following code segment?

Page 21: question number 1

int x = 0;int y = 3;if (x < 1){ cout << "stmt 1 "; x = y;}if (x > 1){ cout << "stmt 2 ";}else{ cout << "stmt 3 ";}

1. stmt 12. stmt 23. stmt 1 stmt 2 [Ans]4. stmt 1 stmt 2 stmt 35. stmt 1 stmt 3

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/*question number 58*/

Use the code below for the next 3 questions. double func1(double x){

if (x <= 20){

return x;}else{

return 100;}

}

double func2(double a){

double b = pow(a, a);cout << b << endl; // OUTPUT 1

double c = func1(b);cout << c << endl; // OUTPUT 2

return b + c;}

int main(){

cout << func2(3) + 5 << endl; // OUTPUT 3return 0;

}

Page 22: question number 1

What will OUTPUT 1 print?

1. 32. 93. 27 [Ans]4. 325. None of the above

What will OUTPUT 2 print?

1. 32. 203. 274. 100 [Ans]

None of the above

What will OUTPUT 3 print?

1. 82. 413. 1274. 132 [Ans]5. None of the above