1:
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?
5
6
10
11
12
2:
With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?
unalloc()
dropmem()
dealloc()
release()
free()
3:
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable “ptr”?
ptr = ptr + sizeof(myStruct);
++(int*)ptr;
ptr = ptr + sizeof(myArray);
increment(ptr);
ptr = ptr + sizeof(ptr);
4:
char* myFunc (char *ptr)
{
ptr += 3;
return (ptr);
}
int main()
{
char *x, *y;
x = “HELLO”;
y = myFunc (x);
printf (”y = %s \n”, y);
return 0;
}
What will print when the sample code above is executed?
y = HELLO
y = ELLO
y = LLO
y = LO
x = O
5:
struct node *nPtr, *sPtr; /* pointers for a linked list. */
for (nPtr=sPtr; nPtr; nPtr=nPtr->next)
{
free(nPtr);
}
The sample code above releases memory from a linked list. Which of the choices below accurately describes how it will work?
It will work correctly since the for loop covers the entire list.
It may fail since each node “nPtr” is freed before its next address can be accessed.
In the for loop, the assignment “nPtr=nPtr->next” should be changed to “nPtr=nPtr.next”.
This is invalid syntax for freeing memory.
The loop will never end.
6:
What function will read a specified number of elements from a file?
fileread()
getline()
readfile()
fread()
gets()
7:
“My salary was increased by 15%!”
Select the statement which will EXACTLY reproduce the line of text above.
printf(”\”My salary was increased by 15/%\!\”\n”);
printf(”My salary was increased by 15%!\n”);
printf(”My salary was increased by 15′%’!\n”);
printf(”\”My salary was increased by 15%%!\”\n”);
printf(”\”My salary was increased by 15′%’!\”\n”);
8:
What is a difference between a declaration and a definition of a variable?
Both can occur multiple times, but a declaration must occur first.
There is no difference between them.
A definition occurs once, but a declaration may occur many times.
A declaration occurs once, but a definition may occur many times.
Both can occur multiple times, but a definition must occur first.
9:
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?
3
5
7
9
11
10:
int a=10,b;
b=a++ + ++a;
printf(”%d,%d,%d,%d”,b,a++,a,++a);
what will be the output when following code is executed
12,10,11,13
22,10,11,13
22,11,11,11
12,11,11,11
22,13,13,13
11:
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?
10
12:
With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?
13:
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable “ptr”?
14:
char* myFunc (char *ptr)
{
ptr += 3;
return (ptr);
}
int main()
{
char *x, *y;
x = “HELLO”;
y = myFunc (x);
printf (”y = %s \n”, y);
return 0;
}
What will print when the sample code above is executed?
15:
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?
5
6
10
11
12
16:
If a program compiles fine, but it produces incorrect result, then the program suffers __________.
A. a compilation error
B. a runtime error
C. a logic error
17:
With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?
unalloc()
dropmem()
dealloc()
release()
free()
18:
Code: void *ptr;
mystruct myarray[10];
ptr=myarray;
Which of the following is the correct way to increment the variable “ptr”?
ptr = ptr + sizeof(myStruct);
++(int*)ptr;
ptr = ptr + sizeof(myArray);
increment(ptr);
ptr = ptr + sizeof(ptr);
19:
Code: char* myFunc (char *ptr)
{ ptr += 3; return (ptr);
} int main()
{ char *x, *y; x = “HELLO”;
y = myFunc (x);
printf (”y = %s \n”, y); return 0;}
What will print when the sample code above is executed?
y = HELLO
y = ELLO
y = LLO
y = LO
x = O
20:
Code:
struct node *nPtr, *sPtr; /* pointers for a linked list. */
for (nPtr=sPtr; nPtr; nPtr=nPtr->next)
{
free(nPtr);
}
The sample code above releases memory from a linked list. Which of the choices below accurately describes how it will work?
It will work correctly since the for loop covers the entire list.
It may fail since each node “nPtr” is freed before its next address can be accessed.
In the for loop, the assignment “nPtr=nPtr->next” should be changed to “nPtr=nPtr.next”.
This is invalid syntax for freeing memory.
The loop will never end.
21:
What function will read a specified number of elements from a file?
fileread()
getline()
readfile()
fread()
gets()
22:
“My salary was increased by 15%!”
Select the statement which will EXACTLY reproduce the line of text above.
printf(”\”My salary was increased by 15/%\!\”\n”);
printf(”My salary was increased by 15%!\n”);
printf(”My salary was increased by 15′%’!\n”);
printf(”\”My salary was increased by 15%%!\”\n”);
printf(”\”My salary was increased by 15′%’!\”\n”);
23:
What is a difference between a declaration and a definition of a variable?
Both can occur multiple times, but a declaration must occur first.
There is no difference between them.
A definition occurs once, but a declaration may occur many times.
A declaration occurs once, but a definition may occur many times.
Both can occur multiple times, but a definition must occur first.
24:
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?
3
5
7
9
11
25:
Code: int a=10,b;b=a++ + ++a;
printf(”%d,%d,%d,%d”,b,a++,a,++a);
what will be the output when following code is executed
12,10,11,13
22,10,11,13
22,11,11,11
12,11,11,11
22,13,13,13
26:
Code:
int x[] = { 1, 4, 8, 5, 1, 4 };
int *ptr, y;
ptr = x + 4;
y = ptr – x;
What does y in the sample code above equal?
-3
0
4[Ans]
4 + sizeof( int )
4 * sizeof( int
27:
Code:
void myFunc (int x)
{
if (x > 0)
myFunc(–x);
printf(”%d, “, x);
}
int main()
{
myFunc(5);
return 0;
}
What will the above sample code produce when executed?
1, 2, 3, 4, 5, 5,
4, 3, 2, 1, 0, 0,
5, 4, 3, 2, 1, 0,
0, 0, 1, 2, 3, 4,
0, 1, 2, 3, 4, 5,
28:
11 ^ 5
What does the operation shown above produce?
1
6
8
14
15
29:
#define MAX_NUM 15
Referring to the sample above, what is MAX_NUM?
MAX_NUM is an integer variable.
MAX_NUM is a linker constant.
MAX_NUM is a precompiler constant.
MAX_NUM is a preprocessor macro.
MAX_NUM is an integer constant.
30:
Which one of the following will turn off buffering for stdout?
setbuf( stdout, FALSE );
setvbuf( stdout, NULL );
setbuf( stdout, NULL );
setvbuf( stdout, _IONBF );
setbuf( stdout, _IONBF );
31:
What is a proper method of opening a file for writing as binary file?
FILE *f = fwrite( “test.bin”, “b” );
FILE *f = fopenb( “test.bin”, “w” );
FILE *f = fopen( “test.bin”, “wb” );
FILE *f = fwriteb( “test.bin” );
FILE *f = fopen( “test.bin”, “bw” );
32:
Which one of the following functions is the correct choice for moving blocks of binary data that are of arbitrary size and position in memory?
memcpy()
memset()
strncpy()
strcpy()
memmove()
33:
int x = 2 * 3 + 4 * 5;
What value will x contain in the sample code above?
22
26[Ans]
46
50
70
34:
Code:
void * array_dup (a, number, size)
const void * a;
size_t number;
size_t size;
{
void * clone;
size_t bytes;
assert(a != NULL);
bytes = number * size;
clone = alloca(bytes);
if (!clone)
return clone;
memcpy(clone, a, bytes);
return clone;
}
The function array_dup(), defined above, contains an error. Which one of the following correctly analyzes it?
If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption.
array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.
The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is nonstandard.
size_t is not a Standard C defined type, and may not be known to the compiler.
The definition of array_dup() is unusual. Functions cannot be defined using this syntax.
35:
int var1;
If a variable has been declared with file scope, as above, can it safely be accessed globally from another file?
Yes; it can be referenced through the register specifier.
No; it would have to have been initially declared as a static variable.
No; it would need to have been initially declared using the global keyword.
Yes; it can be referenced through the publish specifier.
Yes; it can be referenced through the extern specifier.
36:
time_t t;
Which one of the following statements will properly initialize the variable t with the current time from the sample above?
t = clock();
time( &t );
t = ctime();
t = localtime();
None of the above
37:
Which one of the following provides conceptual support for function calls?
The system stack
The data segment
The processor’s registers
The text segment
The heap
38:
C is which kind of language?
Machine
Procedural
Assembly
Object-oriented
Strictly-typed
39:
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?
1
2
3
4
5
40:
Code:
int x = 0;
for (x=1; x<4; x++);
printf("x=%d\n", x);
What will be printed when the sample code above is executed?
x=0
x=1
x=3
x=4
x=5
41:
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?
1
2
3
4
5
42:
Code:
char *ptr;
char myString[] = "abcdefg";
ptr = myString;
ptr += 5;
What string does ptr point to in the sample code above?
fg
efg
defg
cdefg
None of the above
43:
Code:
double x = -3.5, y = 3.5;
printf( "%.0f : %.0f\n", ceil( x ), ceil( y ) );
printf( "%.0f : %.0f\n", floor( x ), floor( y ) );
What will the code above print when executed?
ceil =>rounds up 3.2=4 floor =>rounds down 3.2=3
-3 : 4
-4 : 3
-4 : 4
-3 : 3
-4 : 3
-4 : 3
-4 : 3
-3 : 4
-3 : 3
-4 : 4
44:
Which one of the following will declare a pointer to an integer at address 0×200 in memory?
int *x;
*x = 0×200
int *x = &0×200;
int *x = *0×200;
int *x = 0×200;
int *x( &0×200 );
45:
Code:
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?
4
5
6
7
8
46:
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?
0
1
2
3
4
47:
char ** array [12][12][12];
Consider array, defined above. Which one of the following definitions and initializations of p is valid?
char ** (* p) [12][12] = array;
char ***** p = array;
char * (* p) [12][12][12] = array;
const char ** p [12][12][12] = array;
char (** p) [12][12] = array;
48:
void (*signal(int sig, void (*handler) (int))) (int);
Which one of the following definitions of sighandler_t allows the above declaration to be rewritten as follows:
sighandler_t signal (int sig, sighandler_t handler);
typedef void (*sighandler_t) (int)
typedef sighandler_t void (*) (int);
typedef void *sighandler_t (int);
#define sighandler_t(x) void (*x) (int)
#define sighandler_t void (*) (int)
49:
All of the following choices represent syntactically correct function definitions. Which one of the following represents a semantically legal function definition in Standard C?
Code:
int count_digits (const char * buf) {
assert(buf != NULL);
int cnt = 0, i;
for (i = 0; buf[i] != ‘\0′; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Code:
int count_digits (const char * buf) {
int cnt = 0;
assert(buf != NULL);
for (int i = 0; buf[i] != ‘\0′; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Code:
int count_digits (const char * buf) {
int cnt = 0, i;
assert(buf != NULL);
for (i = 0; buf[i] != ‘\0′; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Code:
int count_digits (const char * buf) {
assert(buf != NULL);
for (i = 0; buf[i] != ‘\0′; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
Code:
int count_digits (const char * buf) {
assert(buf != NULL);
int cnt = 0;
for (int i = 0; buf[i] != ‘\0′; i++)
if (isdigit(buf[i]))
cnt++;
return cnt;
}
50:
struct customer *ptr = malloc( sizeof( struct customer ) );
Given the sample allocation for the pointer “ptr” found above, which one of the following statements is used to reallocate ptr to be an array of 10 elements?
ptr = realloc( ptr, 10 * sizeof( struct customer));
realloc( ptr, 9 * sizeof( struct customer ) );
ptr += malloc( 9 * sizeof( struct customer ) );
ptr = realloc( ptr, 9 * sizeof( struct customer ) );
realloc( ptr, 10 * sizeof( struct customer ) );
51:
Which one of the following is a true statement about pointers?
Pointer arithmetic is permitted on pointers of any type.
A pointer of type void * can be used to directly examine or modify an object of any type.
Standard C mandates a minimum of four levels of indirection accessible through a pointer.
A C program knows the types of its pointers and indirectly referenced data items at runtime.
Pointers may be used to simulate call-by-reference.
52:
Which one of the following functions returns the string representation from a pointer to a time_t value?
localtime
gmtime
strtime
asctime
ctime
53:
Code:
short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} };
printf( “%d\n”, sizeof( testarray ) );
Assuming a short is two bytes long, what will be printed by the above code?
It will not compile because not enough initializers are given.
6
7
12
24
54:
char buf [] = “Hello world!”;
char * buf = “Hello world!”;
In terms of code generation, how do the two definitions of buf, both presented above, differ?
The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not.
The first definition is not suitable for usage as an argument to a function call; the second definition is.
The first definition is not legal because it does not indicate the size of the array to be allocated; the second definition is legal.
They do not differ — they are functionally equivalent. [Ans]
The first definition does not allocate enough space for a terminating NUL-character, nor does it append one; the second definition does.
55:
In a C expression, how is a logical AND represented?
@@
||
.AND.
&&
.AND
56:
How do printf()’s format specifiers %e and %f differ in their treatment of floating-point numbers?
%e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation. [Ans]
%e expects a corresponding argument of type double; %f expects a corresponding argument of type float.
%e displays a double in engineering notation if the number is very small or very large. Otherwise, it behaves like %f and displays the number in decimal notation.
%e displays an argument of type double with trailing zeros; %f never displays trailing zeros.
%e and %f both expect a corresponding argument of type double and format it identically. %e is left over from K&R C; Standard C prefers %f for new code.
57:
Which one of the following Standard C functions can be used to reset end-of-file and error conditions on an open stream?
clearerr()
fseek()
ferror()
feof()
setvbuf()
58:
Which one of the following will read a character from the keyboard and will store it in the variable c?
c = getc();
getc( &c );
c = getchar( stdin );
getchar( &c )
c = getchar();
59:
Code:
#include
int i;
void increment( int i )
{
i++;
}
int main()
{
for( i = 0; i < 10; increment( i ) )
{
}
printf("i=%d\n", i);
return 0;
}
What will happen when the program above is compiled and executed?
It will not compile.
It will print out: i=9.
It will print out: i=10.
It will print out: i=11.
It will loop indefinitely.
60:
Code:
char ptr1[] = "Hello World";
char *ptr2 = malloc( 5 );
ptr2 = ptr1;
What is wrong with the above code (assuming the call to malloc does not fail)?
There will be a memory overwrite.
There will be a memory leak.
There will be a segmentation fault.
Not enough space is allocated by the malloc.
It will not compile.
61:
Code:
int i = 4;
switch (i)
{
default:
;
case 3:
i += 5;
if ( i == ![]()
{
i++;
if (i == 9) break;
i *= 2;
}
i -= 4;
break;
case 8:
i += 5;
break;
}
printf("i = %d\n", i);
What will the output of the sample code above be?
i = 5
i = 8
i = 9
i = 10
i = 18
62:
Which one of the following C operators is right associative?
=
,
[]
^
->
63:
What does the “auto” specifier do?
It automatically initializes a variable to 0;.
It indicates that a variable’s memory will automatically be preserved
It automatically increments the variable when used.
It automatically initializes a variable to NULL.
It indicates that a variable’s memory space is allocated upon entry into the block.
64:
How do you include a system header file called sysheader.h in a C source file?
#include
#incl “sysheader.h”
#includefile
#include sysheader.h
#incl
65:
Which one of the following printf() format specifiers indicates to print a double value in decimal notation, left aligned in a 30-character field, to four (4) digits of precision?
%-30.4e
%4.30e
%-4.30f
%-30.4f [Ans] decimal notation
%#30.4f
66:
Code:
int x = 0;
for ( ; ; )
{
if (x++ == 4)
break;
continue;
}
printf(”x=%d\n”, x);
What will be printed when the sample code above is executed?
x=0
x=1
x=4
x=5
x=6
67:
According to the Standard C specification, what are the respective minimum sizes (in bytes) of the following three data types: short; int; and long?
1, 2, 2
1, 2, 4
1, 2, 8
2, 2, 4
2, 4, 8
68:
Code:
int y[4] = {6, 7, 8, 9};
int *ptr = y + 2;
printf(”%d\n”, ptr[ 1 ] ); /*ptr+1 == ptr[1]*/
What is printed when the sample code above is executed?
6
7
8
9
The code will not compile.
69:
penny = one
nickel = five
dime = ten
quarter = twenty-five
How is enum used to define the values of the American coins listed above?
enum coin {(penny,1), (nickel,5), (dime,10), (quarter,25)};
enum coin ({penny,1}, {nickel,5}, {dime,10}, {quarter,25});
enum coin {penny=1,nickel=5,dime=10,quarter=25};
enum coin (penny=1,nickel=5,dime=10,quarter=25);
enum coin {penny, nickel, dime, quarter} (1, 5, 10, 25);
70:
char txt [20] = “Hello world!\0″;
How many bytes are allocated by the definition above?
11 bytes
12 bytes
13 bytes
20 bytes
21 bytes
71:
Code:
int i = 4;
int x = 6;
double z;
z = x / i;
printf(”z=%.2f\n”, z);
What will print when the sample code above is executed?
z=0.00
z=1.00[Ans]
z=1.50
z=2.00
z=NULL
72:
Which one of the following variable names is NOT valid?
go_cart
go4it
4season[Ans]
run4
_what
73:
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?
Standard C defines this particular behavior as implementation-dependent. The compiler writer has the freedom to decide how the remaining elements will be handled.
The remaining elements are initialized to zero(0).[Ans]
It 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.
As 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.
They are left in an uninitialized state; their values cannot be relied upon.
74:
Which one of the following is a true statement about pointers?
They are always 32-bit values.
For efficiency, pointer values are always stored in machine registers.
With the exception of generic pointers, similarly typed pointers may be subtracted from each other.
A pointer to one type may not be cast to a pointer to any other type.
With the exception of generic pointers, similarly typed pointers may be added to each other.
75:
Which one of the following statements allocates enough space to hold an array of 10 integers that are initialized to 0?
int *ptr = (int *) malloc(10, sizeof(int));
int *ptr = (int *) calloc(10, sizeof(int));
int *ptr = (int *) malloc(10*sizeof(int)); [Ans]
int *ptr = (int *) alloc(10*sizeof(int));
int *ptr = (int *) calloc(10*sizeof(int));
76:
What are two predefined FILE pointers in C?
stdout and stderr
console and error
stdout and stdio
stdio and stderr
errout and conout
77:
Code:
u32 X (f32 f)
{
union {
f32 f;
u32 n;
} u;
u.f = f;
return u.n;
}
Given the function X(), defined above, assume that u32 is a type-definition indicative of a 32-bit unsigned integer and that f32 is a type-definition indicative of a 32-bit floating-point number.
Which one of the following describes the purpose of the function defined above?
X() effectively rounds f to the nearest integer value, which it returns.
X() effectively performs a standard typecast and converts f to a roughly equivalent integer.
X() preserves the bit-pattern of f, which it returns as an unsigned integer of equal size.
Since u.n is never initialized, X() returns an undefined value. This function is therefore a primitive pseudorandom number generator.
Since u.n is automatically initialized to zero (0) by the compiler, X() is an obtuse way of always obtaining a zero (0) value.
78:
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?
if (x == 0) return 0;
return 1;
if (x >= 2) return 2;
if (x == 0) return 1;
if (x <= 1) return 1; [Ans]{more probable}
79:
Code:
/* Increment each integer in the array 'p' of * size 'n'. */
void increment_ints (int p [/*n*/], int n)
{
assert(p != NULL); /* Ensure that 'p' isn't a null pointer. */
assert(n >= 0); /* Ensure that ‘n’ is nonnegative. */
while (n) /* Loop over ‘n’ elements of ‘p’. */
{
*p++; /* Increment *p. */
p++, n–; /* Increment p, decrement n. */
}
}
Consider the function increment_ints(), defined above. Despite its significant inline commentary, it contains an error. Which one of the following correctly assesses it?
*p++ causes p to be incremented before the dereference is performed, because both operators have equal precedence and are right associative.
An array is a nonmodifiable lvalue, so p cannot be incremented directly. A navigation pointer should be used in conjunction with p.
*p++ causes p to be incremented before the dereference is performed, because the autoincrement operator has higher precedence than the indirection operator.
The condition of a while loop must be a Boolean expression. The condition should be n != 0.
An array cannot be initialized to a variable size. The subscript n should be removed from the definition of the parameter p.
80:
How is a variable accessed from another file?
The global variable is referenced via the extern specifier.[Ans]
The global variable is referenced via the auto specifier.
The global variable is referenced via the global specifier.
The global variable is referenced via the pointer specifier.
The global variable is referenced via the ext specifier.
81:
When applied to a variable, what does the unary “&” operator yield?
The variable’s value
The variable’s binary form
The variable’s address [Ans]
The variable’s format
The variable’s right value
82:
Code:
/* sys/cdef.h */
#if defined(__STDC__) || defined(__cplusplus)
#define __P(protos) protos
#else
#define __P(protos) ()
#endif
/* stdio.h */
#include
div_t div __P((int, int));
The code above comes from header files for the FreeBSD implementation of the C library. What is the primary purpose of the __P() macro?
The __P() macro has no function, and merely obfuscates library function declarations. It should be removed from further releases of the C library.
The __P() macro provides forward compatibility for C++ compilers, which do not recognize Standard C prototypes.
Identifiers that begin with two underscores are reserved for C library implementations. It is impossible to determine the purpose of the macro from the context given.
The __P() macro provides backward compatibility for K&R C compilers, which do not recognize Standard C prototypes.
The __P() macro serves primarily to differentiate library functions from application-specific functions.
83:
Which one of the following is NOT a valid identifier?
__ident
auto [Ans]
bigNumber
g42277
peaceful_in_space
84:
/* Read an arbitrarily long string. */
Code:
int read_long_string (const char ** const buf) {
char * p = NULL;
const char * fwd = NULL;
size_t len = 0;
assert(buf);
do
{
p = realloc(p, len += 256);
if (!p)
return 0;
if (!fwd)
fwd = p;
else
fwd = strchr(p, ‘\0′);
} while (fgets(fwd, 256, stdin));
*buf = p;
return 1;
}
The function read_long_string(), defined above, contains an error that may be particularly visible under heavy stress. Which one of the following describes it?
The write to *buf is blocked by the const qualifications applied to its type.
If the null pointer for char is not zero-valued on the host machine, the implicit comparisons to zero (0) may introduce undesired behavior. Moreover, even if successful, it introduces machine-dependent behavior and harms portability.
The symbol stdin may not be defined on some ANCI C compliant systems.
The else causes fwd to contain an errant address.
If the call to realloc() fails during any iteration but the first, all memory previously allocated by the loop is leaked.
85:
Code:
FILE *f = fopen( fileName, “r” );
readData( f );
if( ???? )
{
puts( “End of file was reached” );
}
Which one of the following can replace the ???? in the code above to determine if the end of a file has been reached?
f == EOF[Ans]
feof( f )
eof( f )
f == NULL
!f
86:
Global variables that are declared static are ____________.
Which one of the following correctly completes the sentence above?
Deprecated by Standard C
Internal to the current translation unit
Visible to all translation units
Read-only subsequent to initialization
Allocated on the heap[Ans]
87:
/* Read a double value from fp. */
Code:
double read_double (FILE * fp) {
double d;
assert(fp != NULL);
fscanf(fp, ” %lf”, d);
return d;
}
The code above contains a common error. Which one of the following describes it?
fscanf() will fail to match floating-point numbers not preceded by whitespace.
The format specifier %lf indicates that the corresponding argument should be long double rather than double.
The call to fscanf() requires a pointer as its last argument.
The format specifier %lf is recognized by fprintf() but not by fscanf().
d must be initialized prior to usage.
88:
Which one of the following is NOT a valid C identifier?
___S
1___ [Ans]
___1
___
S___
89:
According to Standard C, what is the type of an unsuffixed floating-point literal, such as 123.45?
long double
Unspecified
float[Ans]
double
signed float
90:
Which one of the following is true for identifiers that begin with an underscore?
They are generally treated differently by preprocessors and compilers from other identifiers.
They are case-insensitive.
They are reserved for usage by standards committees, system implementers, and compiler engineers.
Applications programmers are encouraged to employ them in their own code in order to mark certain symbols for internal usage.
They are deprecated by Standard C and are permitted only for backward compatibility with older C libraries.
91:
Which one of the following is valid for opening a read-only ASCII file?
fileOpen (filenm, “r”);
fileOpen (filenm, “ra”);
fileOpen (filenm, “read”);
fopen (filenm, “read”);
fopen (filenm, “r”);[Ans]
92:
f = fopen( filename, “r” );
Referring to the code above, what is the proper definition for the variable f?
FILE f;
FILE *f;[Ans]
int f;
struct FILE f;
char *f;
93:
If there is a need to see output as soon as possible, what function will force the output from the buffer into the output stream?
flush()
output()
fflush()
dump()
write()
94:
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?
127
128
255
32,767 [Ans]
65,536
95:
Code:
void crash (void)
{
printf(”got here”);
*((char *) 0) = 0;
}
The function crash(), defined above, triggers a fault in the memory management hardware for many architectures. Which one of the following explains why “got here” may NOT be printed before the crash?
The C standard says that dereferencing a null pointer causes undefined behavior. This may explain why printf() apparently fails.
If the standard output stream is buffered, the library buffers may not be flushed before the crash occurs.
printf() always buffers output until a newline character appears in the buffer. Since no newline was present in the format string, nothing is printed.
There is insufficient information to determine why the output fails to appear. A broader context is required.
printf() expects more than a single argument. Since only one argument is given, the crash may actually occur inside printf(), which explains why the string is not printed. puts() should be used instead.
96:
Code:
char * dwarves [] = {
“Sleepy”,
“Dopey” “Doc”,
“Happy”,
“Grumpy” “Sneezy”,
“Bashful”,
};
How many elements does the array dwarves (declared above) contain? Assume the C compiler employed strictly complies
with the requirements of Standard C.
4
5
6
7
8
97:
Which one of the following can be used to test arrays of primitive quantities for strict equality under Standard C?
qsort()
bcmp()
memcmp()
strxfrm()
bsearch()
98:
Code:
int debug (const char * fmt, …) {
extern FILE * logfile;
va_list args;
assert(fmt);
args = va_arg(fmt, va_list);
return vfprintf(logfile, fmt, args);
}
The function debug(), defined above, contains an error. Which one of the following describes it?
The ellipsis is a throwback from K&R C. In accordance with Standard C, the declaration of args should be moved into
the parameter list, and the K&R C macro va_arg() should be deleted from the code.
vfprintf() does not conform to ISO 9899: 1990, and may not be portable.
Library routines that accept argument lists cause a fault on receipt of an empty list. The argument list must be
validated with va_null() before invoking vfprintf().
The argument list args has been improperly initialized.
Variadic functions are discontinued by Standard C; they are legacy constructs from K&R C, and no longer compile
under modern compilers.
99:
Code:
char *buffer = “0123456789″;
char *ptr = buffer;
ptr += 5;
printf( “%s\n”, ptr );
printf( “%s\n”, buffer );
What will be printed when the sample code above is executed?
0123456789
56789
5123456789
5123456789
56789
56789
0123456789
0123456789
56789
0123456789 [Ans]
100:
/* Get the rightmost path element of a Unix path. */
Code:
char * get_rightmost (const char * d)
{
char rightmost [MAXPATHLEN];
const char * p = d;
assert(d != NULL);
while (*d != ‘\0′)
{
if (*d == ‘/’)
p = (*(d + 1) != ‘\0′) ? d + 1 : p;
d++;
}
memset(rightmost, 0, sizeof(rightmost));
memcpy(rightmost, p, strlen(p) + 1);
return rightmost;
}
The function get_rightmost(), defined above, contains an error. Which one of the following describes it?
The calls to memset() and memcpy() illegally perform a pointer conversion on rightmost without an appropriate cast.
The code does not correctly handle the situation where a directory separator ‘/’ is the final character.
The if condition contains an incorrectly terminated character literal.
memcpy() cannot be used safely to copy string data.
The return value of get_rightmost() will be invalid in the caller’s context.
101:
What number is equivalent to -4e3?
-4000 [Ans]
-400
-40
.004
.0004
102:
Code:
void freeList( struct node *n )
{
while( n )
{
????
}
}
Which one of the following can replace the ???? for the function above to release the memory allocated to a linked list?
n = n->next;
free( n );
struct node m = n;
n = n->next;
free( m );
struct node m = n;
free( n );
n = m->next;
free( n );
n = n->next;
struct node m = n;
free( m );
n = n->next;
103:
How does variable definition differ from variable declaration?
Definition allocates storage for a variable, but declaration only informs the compiler as to the variable’s type.
Declaration allocates storage for a variable, but definition only informs the compiler as to the variable’s type.
Variables may be defined many times, but may be declared only once.[Ans]
Variable definition must precede variable declaration.
There is no difference in C between variable declaration and variable definition.
104:
Code:
int x[] = {1, 2, 3, 4, 5};
int u;
int *ptr = x;
????
for( u = 0; u < 5; u++ )
{
printf("%d-", x[u]);
}
printf( "\n" );
Which one of the following statements could replace the ???? in the code above to cause the string 1-2-3-10-5- to be
printed when the code is executed?
*ptr + 3 = 10;
*ptr[ 3 ] = 10;
*(ptr + 3) = 10;[Ans]
(*ptr)[ 3 ] = 10;
*(ptr[ 3 ]) = 10;
105:
Code:
/*sum_array() has been ported from FORTRAN. No * logical changes have been made*/
double sum_array(double d[],int n)
{
register int i;
double total=0;
assert(d!=NULL);
for(i=l;i<=n;i++)
total+=d[i];
return total;
}
The function sum_array(), defined above, contains an error. Which one of the following accurately describes it?
The range of the loop does not match the bounds of the array d.
The loop processes the incorrect number of elements.
total is initialized with an integer literal. The two are not compatible in an assignment.
The code above fails to compile if there are no registers available for i.
The formal parameter d should be declared as double * d to allow dynamically allocated arrays as arguments.
106:
Code:
#include
void func()
{
int x = 0;
static int y = 0;
x++; y++;
printf( “%d — %d\n”, x, y );
}
int main()
{
func();
func();
return 0;
}
What will the code above print when it is executed?
1 — 1
1 — 1
1 — 1
2 — 1
1 — 1
2 — 2
1 — 0
1 — 0
1 — 1
1 — 2 [Ans]
107:
$$No other seems that sound$$
Code:
int fibonacci (int n)
{
switch (n)
{
default:
return (fibonacci(n – 1) + fibonacci(n – 2));
case 1:
case 2:
}
return 1;
}
The function above has a flaw that may result in a serious error during some invocations. Which one of the following
describes the deficiency illustrated above?
For some values of n, the environment will almost certainly exhaust its stack space before the calculation completes.
[Ans]
An error in the algorithm causes unbounded recursion for all values of n.
A break statement should be inserted after each case. Fall-through is not desirable here.
The fibonacci() function includes calls to itself. This is not directly supported by Standard C due to its unreliability.
Since the default case is given first, it will be executed before any case matching n.
108:
When applied to a variable, what does the unary “&” operator yield?
The variable’s address [Ans]
The variable’s right value
The variable’s binary form
The variable’s value
The variable’s format
109:
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?
127
128
255
32,767[Ans]
65,536
110:
int x = 011 | 0×10;
What value will x contain in the sample code above?
3
13
19
25
27
111:
Which one of the following calls will open the file test.txt for reading by fgetc?
fopen( “test.txt”, “r” );
read( “test.txt” )
fileopen( “test.txt”, “r” );
fread( “test.txt” )
freopen( “test.txt” )
112: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?
m = -26, n = -7, o = 0
m = -52, n = -4, o = -2
m = -26, n = -5, o = -2
m = -104, n = -7, o = 0
m = -52, n = -6, o = 0
113:
How do printf()'s format specifiers %e and %f differ in their treatment of floating-point numbers?
%e displays an argument of type double with trailing zeros; %f never displays trailing zeros.
%e displays a double in engineering notation if the number is very small or very large. Otherwise, it behaves like %f and displays the number in decimal notation.
%e always displays an argument of type double in engineering notation; %f always displays an argument of type double in decimal notation. [Ans]
%e expects a corresponding argument of type double; %f expects a corresponding argument of type float.
%e and %f both expect a corresponding argument of type double and format it identically. %e is left over from K&R C; Standard C prefers %f for new code.
114:
$$Except 1 all choices are O.K.$$
c = getchar();
What is the proper declaration for the variable c in the code above?
char *c;
unsigned int c;
int c;
unsigned char c;
char c;[Ans]
115:
Which one of the following will define a function that CANNOT be called from another source file?
void function() { ... }
extern void function() { ... }
const void function() { ... }
private void function() { ... }
static void function() { ... }
116.
Which of the following are essential statement types for describing algorithms?
1.sequence
2.selection
3.repetition
117.
What is a condition?
1.A value which is true or false
2.A numerical value
118.
Which of the following are valid C++ identifiers?
1.const
2.y=z
.xyz123
Bill
4.ThisIsALongOne
5.Sue's
6.two-way
7. int
8.so_is_this_one
9. _amount
10.2ndclass
119.
Values can be input to variables in the program using the stream
1.cin
2.cout
120.
Comments in C++ are started with
1.//
2. {
3. ;
121.
Evaluate 4+5*3
1.19
2.27
122.
Evaluate 7*3+2
1.23
2.35
123.
Evaluate (4+2)*3
1.18
2.10
124.
Evaluate 17/3
1.5
2.6
125.
Evaluate 17%3
1.0
2.1
3. 2
4. 3
126.
Evaluate 1/2
0
1
127.
Evaluate 2*8/2*4
32
2
128.
Consider the following section of C++ program, in which i and n are int variables
n = 7;
i = 4;
i = n++;
What are the values of i and n ?
i=7 n=8
i=7 n=7
i=8 n=8
i=4 n=7
129.
Consider the following section of C++ program, in which i and n are int variables
n = 5;
i = 9;
i = --n;
What are the values of i and n ?
i=9 n=5
i=4 n=4
i=4 n=5
i=5 n=4
130.
What values can a relational expression take?
true and false
Any numerical value
131.
What values does C++ use to represent true and false ?
1 and 0
Any numerical value
132.
If a is 5, b is 10, c is 15 and d is 0 what are the truth values of the following expressions?
0 c == a+b
0 a != 7
1 b <=a
0 a > 5
0 a+d >= c-b
0 d/a < c*b
133.
If a is 5, b is 10, c is 15 and d is 0 what are the truth values of the following expressions?
0 c == a+b || c == d
0 a != 7 && c >= 6 || a+c <= 20
0 !(b <= 12) && a % 2 == 0
0 !(a >5) || c < a+b
134.
If y has the value 5 what will be the value of the variable y after the following piece of C++ is executed?
if (y > 0)
y += 2;
5
7
2
135.
If p has the value 3 and max has the value 5, what will be the value of the variable max after the following piece of C++ is executed?
if (p < max)
max = p;
5
3
136.
If x has the value 5.0 what will be the value of the variable countneg after the following piece of C++ is executed?
countneg = 0;
if (x < 0.0)
negsum = negsum + x;
countneg = countneg + 1;
0
1
5
137.
If this is changed as follows what would be the value of countneg after execution of the code?
countneg = 0;
if (x < 0.0)
{
negsum = negsum + x;
countneg = countneg + 1;
}
0
1
5
138.
If y has the value 5 what will be the value of the variable y after the following piece of C++ is executed?
if (y > 0)
y += 2;
else
y = 3;
5
7
3
2
139.
Consider the following section of code:
if ( base < height )
cout << "Top heavy structure" << endl;
else
cout << "Stable structure" << endl;
Which of the following possible values for base and height cause the message Stable structure to be printed?
0 base = 5, height = 3
0 base = 8, height = 8
0 base = 2, height = 7
140.
What is the final value of x if initially x has the value 1?
if (x >= 0)
x += 5;
else if (x >=5)
x += 2;
8
6
1
141.
What is the final value of x if initially x has the value 0?
if (x >= 0)
x += 5;
if (x >= 5)
x += 2;
7
5
0
142.
What is the value of the variable c after the switch statement below?
x = 3;
switch ( x ) {
case 1: c = ‘A’; break;
case 2: c = ‘B’; break;
case 3: c = ‘C’; break;
default: c = ‘F’; break;
}
A
B
C
F
143.
What is the value of i after the while statement below?
n = 10;
i = 0;
while ( i < n ) {
...
i++;
}
9
10
11
144.
What are the first and last values of i output by this loop?
n = 10;
i = 0;
while ( ++i < n ) {
cout << i << endl;
}
1 and 9
0 and 9
0 and 10
1 and 10
145.
What are the first and last values of i output by this loop?
n = 10;
i = 0;
while ( i++ < n ) {
cout << i << endl;
}
1 and 10
0 and 10
1 and 9
0 and 9
146.
What is the value of i after the do-while statement below?
n = 10;
i = 0;
do {
...
i++;
} while ( i < n );
9
10
11
147.
What are the first and last values of i output by this loop?
n = 10;
i = 0;
do {
cout << i << endl;
} while ( ++i < n );
1 and 9
0 and 9
0 and 10
1 and 10
148.
What are the first and last values of i output by this loop?
n = 10;
i = 0;
do {
cout << i << endl;
} while ( i++ < n );
1 and 10
0 and 10
1 and 9
0 and 9
149.
What is the value of i after the for statement below?
n = 100;
for ( i = 0; i < n; i++ ) {
...
}
99 100 101
150.
What are the first and last values of i output by this loop?
n = 20;
for ( i = 0; i < n; i++ ) {
cout << i << endl;
}
0 and 19
1 and 19
0 and 20
1 and 20
151.
What are the first and last values of i output by this loop?
n = 15;
i = 0;
for( i = 0; i <= n; i++ ) {
cout << i << endl;
}
0 and 15
1 and 14
1 and 15
152.
What is the last value of i output by this loop?
n = 27;
i = 0;
for( i = 0; i <= n; i+= 2 ) {
cout << i << endl;
}
25
28
27
26
153.
What is the result of this function
int foo(int j) {
int x = 5 ;
if( j == 0 ) {
int x ;
x = 3 ; }
return x ;
}
1. 3
2. 5
3. 8
4. 3 if j==0 and 0 otherwise
154.
What value is returned from a call to subroutine f?
int fred = 0;
void g(int fred) {
fred = 13 ;
}
int f() {
int fred = 42 ;
g( fred ) ;
return fred ;
}
1. 0
2. 13
3. 42
4. 5
155.
What value is returned from a call to subroutine f?
int fred = 0;
void g(int &frieda) { // This line is different from last time.
frieda = 13 ; // So is this one
}
int f() {
int fred = 42 ;
g( fred ) ;
return fred ;
}
1. 0
2. 13
3. 42
4. 5
156.
What values are printed by this program?
#include
using namespace std ;
int nutsy(bool flag) {
int fred ;
if( flag ) fred = 42 ;
return fred ; }
int main() {
cout << nutsy( true ) << endl ;
cout << nutsy( false ) << endl ; }
1. 42 and 0
2. 42 and 42
3. 42 and who knows what
4. 0 and 0
157.
What is printed by this program
#include
#include
using namespace std ;
int main() {
int *p = new(nothrow) int ;
if( p!=0 ) {
*p = 99 ;
int *q = p ;
delete q ;
cout << *p << endl ; }
}
1. 99
2. 0
3. Unknown
4. Unknown and the program may crash.
158.
What is printed by this program
#include
#include
using namespace std ;
int main() {
int *p = new(nothrow) int ;
if( p!=0 ) {
delete p ;
*p = 99 ;
cout << 12 << endl ; }
}
1. 99
2. 12
3. Unknown and the program may crash
4. The program will crash.
159.
What is printed by this program
#include
#include
using namespace std ;
int main() {
int *p = new(nothrow) int ;
if( p!=0 ) {
*p = 99 ;
int *q = p ;
delete q ;
cout << *p << endl ; }
}
1. 99
2. 0
3. Unknown
4. Unknown and the program may crash.
160.
What is printed by this program
#include
#include
using namespace std ;
int main() {
int *p = new(nothrow) int ;
if( p!=0 ) {
delete p ;
*p = 99 ;
cout << 12 << endl ; }
}
1. 99
2. 12
3. Unknown and the program may crash
4. The program will crash.
161.
What is wrong with this subroutine:
Matrix *strange( Matrix input ) {
Matrix *workMatrixP = new(nothrow) Matrix ;
*workMatrixP = input ;
for( int i = 0 ; i < workMatrixP->rows() ; ++i )
for( int j = 0 ; j < workMatrixP->cols() ; ++j )
if( i != j ) workMatrixP->set(i,j, 0.0) ;
return workMatrixP ; }
1. Variable “workMatrixP” is declared incorrectly.
2. “new” should not be followed by “(nothrow)”
3. A null pointer may be dereferenced.
4. “strange” returns a pointer to a variable that will be destroyed when “strange” is returned from.
5. An uninitialized pointer will be dereferenced.
162.
What is wrong with this code?
for( int i = 0 ; i < N ; ++i ) {
double *p = new(nothrow) double ;
if( p== 0 ) { return false ; }
*p = A[i] ; A[i] = B[i] ; B[i] = *p ; }
1. A null pointer may be referenced
2. The is no need to use a heap variable in this case.
3. An uninitialized pointer may be dereferenced.
4. It contains a space leak.
163.
What is wrong with the following snippet of code? (Assume sourceP is a pointer to char variable.)
// Copy a C-style string
char *targetP ;
while( *sourceP ) {
*targetP = *sourceP ;
++targetP; ++sourceP ;
}
*targetP = '\0' ;
1. It dereferences an uninitialized pointer.
2. The expression *sourceP is not boolean
3. The loop condition should be "sourceP" rather than "*sourceP"
4. "sourceP" should be incremented before "targetP"
164.
What is wrong with this subroutine?
double *weird( double a, double b ) {
double c ;
c = pow( a*a + b*b, 0.5 ) ;
return &c ;
}
1. It may dereference a null pointer
2. The type of the return expression is wrong
3. The parameters should be passed by reference
4. The result will be an invalid pointer.
165.
Suppose we execute the following code
int a[10], *p ;
for(int i=0 ; i<10 ; ++i) a[i] = i*i ;
p = &a[3] ;
At this point, which of the following pairs of expressions are NOT equivalent
1. "a[1]" and "p[-2]"
2. "&a[0]" and "p-3"
3. "*a" and "*(p-3)"
4. "&a[10]" and "&p[7]"
5. "a" and "(p-3)"
166.
What is the type of "pp" ?
char **pp, *p, c ;
1. No type. This is an ill-formed declaration
2. "pp" is a pointer to a char variable.
3. "pp" is a pointer to a pointer to a char variable.
167.
What values will 'c', 'p', and 'pp' have after this code is executed?
char **pp, *p, c ;
pp = & p ;
*pp = & c ;
**pp = 'z' ;
1. "pp" holds the address of variable "p"
"p" was not initialized (and so may hold anything)
"c" was not initialized (and so may hold anything)
2. "pp" holds the address of variable "p"
"p" holds the address of variable "c"
"c" holds the character 'z'
3. The code is illegal and will not compile
4. The code dereferences an uninitialized pointer.
168.
What is the difference between the following two subroutines:
void f( int &r ) {
r = r / 2 ;
}
void g(int *p) {
*p = *p / 2 ;
}
1. Only the name.
2. "f" has no net effect. "g" assigns an integer the floor of half its original value.
3. "g" has no net effect. "f" assigns an integer the floor of half its original value.
4. Calling "g" requires an address to be passed in.
169.
Is there anything wrong with this subroutine?
int &odd( int &r, int i ) {
r = i*i ;
return r ; }
1. Yes. It may return a reference to a location that no longer exists.
2. No. There is nothing wrong with it.
170.
Is there anything wrong with this subroutine?
int &peculiar( int &r, int i ) {
r = i*i ;
return i ; /* This line has changed */ }
1. Yes. It returns a reference to a variable that will no longer exist.
2. No. It is fine.
171.
Is there a difference between these two subroutines:
Student top( ClassList &cl ) {
...subroutine body...
}
Student top( const ClassList cl ) {
...subroutine body...
}
1. No. They are interchangeable.
2. Yes. They are similar, but there are some differences
172.
What is the type of variable "x" in the following declaration.
ClassList *(x[N]) ;
1. "x" is an array of pointers to objects of type ClassList
2. "x" is a pointer to an array of ClassList objects
3. The declaration is ill-formed and will cause a compile time error.
173.
What is the type of variable "y" declared thus:
ClassList ( *y )[N] ;
1. "y" is an array of pointers to objects of type ClassList.
2. "y" is a pointer to an array of objects of type ClassList.
3. The declaration is ill-formed and will cause a compile time error.
174.
Which of the following pairs of declarations are NOT identical in meaning.
1. int *a[10]; int *(a[10]) ;
2. int *( *q ); int **q ;
3. int *&a = b; int &*a = b;
4. int *f() ; int *( f() ) ;
175.
What is the type of "x" in the following declaration?
const char* w, x, y ;
1. "x" is a constant pointer to a character
2. "x" is a pointer to a constant character
3. "x" is a constant character
4. "x" is a plain old character
176.
Suppose the following declarations are made
const int *p = x ;
int *const q = y ;
For each of the following statements, is the statement sensible or not.
1. *p = 10 ;
2. *q = 10
3. p = x;
4. q = y ;
177.
When will this line fail to compile:
new myObj[100];
a) Never
b) When myObj is too large to fit into memory
c) When myObj has no default constructor
178.
Assuming that myObj is less than 1000 bytes, is there anything wrong with this code?
char x[1000];
myObj *obj = reinterpret_cast(x);
new (obj) myObj;]
a) Nope, it works fine
b) Yes, there could be byte alignment issues
c) Yes, the syntax for calling new is incorrect
179.
What is the functional difference between
myObj *x = new myObj[100];
delete x;
and
myObj *x = new myObj[100];
delete [] x;
a) There is none; they both work as expected
b) They both do nothing.
c) The first will not invoke all myObj destructors
180.
What is wrong with the following code?
int * x = (int *) malloc(100 * sizeof(int));
x = realloc(x, sizeof(int) * 200);
a) If realloc fails, then the original memory is lost
b) Nothing, realloc is guaranteed to succeed (by returning the original pointer)
c) Nothing, realloc frees the original memory passed to it
181.
What is one possible symptom of having called free on the same block of memory twice?
a) Nothing, calling free twice on one block of memory always works fine
b) Malloc might always return the same block of memory
c) You cannot free any memory in the future
182 What's wrong with this line of code?
delete NULL;
a) It causes a segmentation fault when delete tries to access NULL
b) Nothing
c) It is undefined behavior
183.
Assuming this code were being compiled using a standards-conforming C++, what is wrong with it?
int * x = malloc(100 * sizeof(int));
x = realloc(x, sizeof(int) * 200);
a) malloc is undefined in C++, you can only allocate memory using new
b) Invalid cast from a void* to an int*
c) Nothing is wrong with this code
184.
What happens if the normal new operator fails?
a) This is left up to the implementation to decide
b) It returns NULL
c) It throws an exception
185.
What is a difficulty that arises with handling out of memory errors?
a) Many cleanup operations require extra memory
b) Running out of memory rarely happens in systems that interact with users
c) Once you've run out of memory, your program cannot continue
186.
What is the result of this code?
myObj *foo = operator new(sizeof(foo));
a) That's not legal syntax!
b) foo has memory allocated for it, but is not constructed
c) foo has memory allocated for it, and is fully constructed
187.
Which of the following is illegal:
a) template
b) template
c) template
188.
What problems can a templated member function cause?
a) They’re just not legal
b) They’re hard to use
c) They allow violations of encapsulation
189.
When must template functions have explicit template parameters?
a) Always
b) When the template types cannot be inferred
c) Never, the template types can always be inferred
190. Are templates conceptually related to polymorphism?
a) Nope
b) Only when the template types are objects
c) Yes, but compile-time polymorphism
191.
In general, is it possible to completely hide the source code of a library written using templates?
a) Yes, but using export feature
b) No, pretty much never
c) Yes, all the time
192.
When are templates usually instantiated?
a) At runtime
b) At compile time
c) At link time
193.
Which of the following describes a potentially surprising result of using templates?
a) Slower programs
b) Poor variable naming in the debugger
c) Increased executable size in comparison to the code base
194.
Which of the following is an invalid template declaration:
a) template
b) template
c) template
195.
What is the result of trying to run this program:
#include
template
struct X
{
enum val {v = T * X
};
template <>
struct X<0>
{
enum val {v = 1 };
};
int main() { std::cout<
a) Compilation error
b) Link error
c) 120
196.
Given the below code, what happens when a method invokes callFunc on an object of type obj?
template
template <> func
class obj
{
public:
callFunc() { func(4.5); }
private:
func(int val) {}
};
a) func(int val)
b) template
c) template <> func
197.
Choose the correct statement.
a) A compiler translates one high-level program instruction at a time into machine code and immediately executes that code.
b) Compilation is the process of translating a source program into an object program.
c) A program written in a high-level language is called an object program.
d) A program that consists of machine language instructions is called a source program.
198.
Computers are very systematic and can never guess. (T/F)
199.
Planning out an algorithm to solve the required problem is the first step in programming. (T/F)
200.
What kind of error causes a program to not be able to finish compiling?
a) logic error
b) operational error
c) semantic error
d) syntax error
e) computational error
201.
The following shows what kind of error?
#include
using namespace std;
int MAIN()
{
cout << "This is my first test.\n";
return 0;
}
a) compile-time error
b) run-time error
c) linking error
d) object code error
e) No errors
202.
Excessive comments add time to the execution of your program. (T/F)
203.
Which of the following is NOT a valid identifier (i.e. CANNOT be used as a name for a variable).
a) phone_number
b) EXTRACREDIT
c) DOUBLE
d) my course
e) All are invalid
204.
The following is an example of what type of error?
int num1;
int num2;
cout << "Enter two numbers: ";
cin >> num1 >> num1;
a) syntax error
b) semantic error
c) logic error
d) compile-time error
e) no errors
205.
Which statement has a syntax error?
a) double a = 3.5 + 4;
b) cout << b + c;
c) x + y = total;
d) double Main;
e) All of the above.
206.
What is the output of the following statement?
int num = 26;
cout << "Here it is." << num << "\nThere it is."
a) Here it is.There it is.
b) Here it is.26nThere it is.
c) Here it is.26
There it is.
d) Here it is.
There it is.
e) Here it is.26\nThere it is.
207.
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;
a) 0
b) 14
c) 14.92
d) 15
e) None of the above
208. What is the result of the following code:
int a = 53;
int b = 6;
cout << a / b;
a) 8.833
b) 9
c) 8
d) 5
e) None of the above
209.
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
a) 1 / c + 1 - a / 1 + b
b) 1 / c + (1 - a / 1 + b)
c) (1 / c) + (1 - a / 1 + b)
d) 1 / c + (1 - a) / (1 + b)
e) None of the above
210.
What is the result of the following arithmetic expression? 7.0 * 3 % 2
a) 7.0
b) 0
c) 1.0
d) 10.5
e) invalid expression
211.
All three statements below have the same effects.
a = a + 1;
a = (a+1);
a = 1 + a;
(T/F)
212.
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;
a) 1.5
b) 1
c) 0.25
d) 0
e) None of the above
213.
What is the output of the following code?
int a = 9;
int b = 6;
double c = 2.2;
cout << 4.4 + c * (a / b);
a) 7.7
b) 6.6
c) 9.9
d) 11
e) None of the above
214.
What is the output of the following code?
string fun = "I love C++";
cout << fun.length();
a) 1
b) 9
c) 10
d) 8
e) None of the above
215.
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);
a) Sun
b) nun
c) nnd
d) S u
e) None of the above
216.
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";
a) ----1.00
777.78
b) -------1
777.78
c) ----1.00
--777.78
d) ----1.00
777.777
e) None of the above
217.
The Point type is built into the C++ language. (T/F)
218.
If the condition in an if structure is FALSE, the program will end. (T/F)
219.
Using an assignment operator in the condition of an if structure will result in a syntax error. (T/F)
220.
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
{
stmt set 2;
}
(T/F)
221.
Given the following code, what will be the output, assuming the user enters 14.25.
int num;
cout << "Enter an integer: ";
cin >> num;
if (cin.fail())
{
cout << "Output 1\n";
}
else
{
cout << "Output 2\n";
}
a) Output 1
b) Output 2
222.
Given the code from the above question, what will be the output, assuming the user enters .11.
a) Output 1
b) Output 2
223.
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;
{
str = word;
}
cout << str;
a) Hello
b) Goodbye
c) None of the above
224.
Given the following code, what will be the output?
int x = 1;
int y = 2;
if (x < y)
{
cout << x;
}
cout << y;
a) 1
b) 2
c) x
d) y
e) 2
225.
What will be the output of the following code segment?
int x = 0;
int y = 3;
if (x < 1)
{
cout << "stmt 1 ";
x = y;
}
if (x > 1)
{
cout << "stmt 2 ";
}
else
{
cout << "stmt 3 ";
}
a) stmt 1
b) stmt 2
c) stmt 1 stmt 2
d) stmt 1 stmt 2 stmt 3
e) stmt 1 stmt 3
226.
Parameters and return types can only be fundamental types. (T/F)
227.
A return statement always terminates the program. (T/F)
228.
Assuming that x is a double, the following statement is a valid statement.
cout << sqrt(pow(x, 4));
(T/F)
For the next 3 questions, assume there is a the following function header (prototype):
void func(int, double)
229.
Given the function func above, the following statement is valid.
cout << func(4, 13.9);
(T/F)
230.
Given the function func above, the following statement is valid.
func(rand(), 2.2);
(T/F)
231.
Given the function func above, the following statement is valid.
func(7.7, 11);
(T/F)
232.
The expression rand() % 50 + 10 would result in a range of
a) 0-49
b) 1-50
c) 10-50
d) 10-59
e) None of the above
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 3
return 0;
}
233.
What will OUTPUT 1 print?
a) 3
b) 9
c) 27
d) 32
e) None of the above
234.
What will OUTPUT 2 print?
a) 3
b) 20
c) 27
d) 100
e) None of the above
235.
What will OUTPUT 3 print?
a) 8
b) 41
c) 127
d) 132
e) None of the above
Questions 236 to 244 are based on code below:
void foo (int x, int & y)
{
int c;
c = x + y;
y = 2 * c;
x = 2 * x;
}
236.
"x" and "y" are called:
a)actual parameters b) local variables
c) formal parameters d) local parameters
237.
"c" is called:
a)actual parameter b) local variable
c) formal parameter d) local parameter
238.
The declaration "void foo (int x, int & y);" is called a
function body b) function type
c) function stereotype d) function prototype
239.
The function "foo" returns the following type as result:
integer b) no result is returned
c) float d) none of the above
240.
An example of a value parameter in the code above is:
a) x b) y
c) c d) foo
241.
An example of a variable parameter in the code above is:
a) x b) y
c) c d) foo
242.
In the main program, if you have the following code:
int a = 3, b=3;
foo(a, b);
the identifiers "a" and "b" are called:
actual parameters b) local variables
c) formal parameters d) local parameters
243.
The value of the variable "a" after "foo" is called in the main program as above:
6 b) 12
c) 8 d) 3
244.
The value of the variable "b" after "foo" is called in the main program as above:
6 b) 12
c) 8 d) 3
Consider the following array declaration and code (for questions 245 to 246):
int A[5];
245.
The number of cells declared in this array is:
a) 4 b) 5
c) 6 d) None of the above
246.
The array cells are indexed from:
a) 0..5 b) 1..5
c) 0..4 d) 1..6
In the context of the following declaration (assuming
string x = “Bob”, y = “Jane”, z;
247.
A call “z.empty( )” would yield what value?
true b) false
unknown d) depends
248.
A call “x.size( )” would yield what value?
a) 4 b) 5
c) 2 d) 3
249.
A call “y[0].isdigit( )” would yield what value?
a)true b) false
c) unknown d) depends on circumstances
250.
When you refer to an identifier inside a function, the C++ compiler prefers:
a)Most local identifier b) Any that looks available
c) Most global identifier d) Identifier inside a comment
251.
The default parameter passing mechanism in C++ is:
a) Call by reference b) Call by name
c) Call by value d) Call by global variable
252.
Parameters are preferable to global variables because (mark ALL that apply):
? Function is considerably shorter
? Function is considerably longer
? Function can be reused in other programs
? Function is easier to understand
Consider the following code: (for questions 253 to 255)
int scores[10][20];
253.
The number of elements in the scores array is:
a) 200 b) 232
c) 201 d) 199
254.
The number of rows in the scores array is:
a) 21 b) 20
c) 10 d) 11
255.
The number of columns in the scores array is:
a) 21 b) 20
c) 10 d) 11
The function is_even is supposed to take a single integer as parameter and return true if the parameter is divisible by 2 and false otherwise. (questions 256 to 258)
int val = 19;
256.
is_even(val) would return what value?
a) unknown b) true
c) false d) depends on circumstances
257.
Declare the prototype for the is_even function below: (2 points)
258.
Define the entire is_even function below including its body: (3 points)
Questions 318 and 319use the following:
enum student_status { INACTIVE, ACTIVE, GRADUATED };
259.
The above declares an enumerated type. The benefit of this type is that:
a) Program is shorter b) Program executes faster
c) Program is longer d) Program is easier to understand
260.
For all practical purposes, enumerated types come very close to being:
a) Better than structures b) Mnemonic constants
c) Worse than constants d) Mnemonic functions
261.
Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived object; calling of that virtual method will result in which method being called?
a. Base method b. Derived method.
c. Error d. None of these
262.
void main()
{
extern int a;
a=10;
printf(”%d”,a);
}
will
1. give linker error- a not defined ans
2. print 10
263.
int a[10];
printf(”%d,%d”,a[0],a[12]);
will compiler show any error?
264.
main( )
{
unsigned int i=3;
while( i >=0)
printf( “%d”, i–);
}
how many times will the printf stmt be executed?
a)0 b)3 c)4 d)infinite
265.
main( )
{
int x,y, z;
x=2;
y=5;
z= x+++y;
printf(”%d %d %d”, x, y z);
}
a)3 5 7 b)option 2 c)option 3 d)option 4
266.
Given the following statement
enum day = { jan = 1 ,feb=4, april, may}
What is the value of may?
(a) 4
(b) 5
(c) 6
(d) 11
(e) None of the above
267.
Find the output for the following C program
main
{int x,j,k;
j=k=6;x=2;
x=j*k;
printf(”%d”, x);
268.
Find the output for the following C program
fn f(x)
{ if(x<=0)
return;
else f(x-1)+x;
}
269.
Find the output for the following C program
i=20,k=0;
for(j=1;j
{k+=j<10?4:3;
}
printf("%d", k);
270.
Find the output for the following C program
int i =10
main()
{int i =20,n;
for(n=0;n<=i;)
{int i=10;
i++;
}
printf("%d", i);
271.
Find the output for the following C program
int x=5;
y= x&y
272.
Find the output for the following C program
Y=10;
if( Y++>9 && Y++!=10 && Y++>10)
{printf(”%d”, Y);
else
printf(”%d”, Y);
}
273.
Find the output for the following C program
f=(x>y)?x:y
a) f points to max of x and y
b) f points to min of x and y
c)error
274.
What is the sizeof(long int)
(a) 4 bytes
(b) 2 bytes
(c) compiler dependent
(d) 8 bytes
275.
Which of the function operator cannot be over loaded
(a) <=
(b) ?:
(c) ==
(d) *
276.
Find the output for the following C program
main()
{intx=2,y=6,z=6;
x=y==z;
printf(%d",x)
}
277.
What is the output of the following program
main()
{ int var=25,varp;
varp=&var;
varp p = 10;
fnc(varp)
printf("%d%d,var,varp);
}
(a) 20,55
(b) 35,35
(c) 25,25
(d)55,55
278.
To add a value 1 to variable x, you write
A. 1 + x = x;
B. x += 1;
C. x := 1;
D. x = x + 1;
E. x = 1 + x;
279.
To add number to sum, you write (Note: C++ is case-sensitive)
A. number += sum;
B. number = sum + number;
C. sum = Number + sum;
D. sum += number;
E. sum = sum + number;
280.
Analyze the following code.
#include
using namespace std;
int main()
{
int month = 09;
cout << "month is " << month;
}
A. The program displays month is 09
B. The program displays month is 9
C. The program displays month is 9.0
D. The program has a syntax error, because 09 is an incorrect literal value.
281.
what is y displayed in the following code?
#include
using namespace std;
int main()
{
int x = 1;
int y = x = x + 1;
cout << "y is " << y;
}
A. y is 0.
B. y is 1 because x is assigned to y first.
C. y is 2 because x + 1 is assigned to x and then x is assigned to y.
D. The program has a syntax error since x is redeclared in the statement int y = x = x + 1.
282.
To assign a double variable d to an float variable x, you write
A. x = (long)d
B. x = (int)d;
C. x = d;
D. x = (float)d;
283.
What is the printout of the following code:
double x = 5.5;
int y = (int)x;
cout << "x is " << x << " and y is " << y;
A. x is 5 and y is 6
B. x is 6.0 and y is 6.0
C. x is 6 and y is 6
D. x is 5.5 and y is 5
E. x is 5.5 and y is 5.0
284.
Which of the following assignment statements is legal?
A. float f = -34;
B. int t = 23;
C. short s = 10;
D. int t = (int)false;
E. int t = 4.5;
285.
What is the value of (double)5/2?
A. 2;
B. 2.5;
C. 3;
D. 2.0;
E. 3.0;
286.
What is the value of (double)(5/2)?
A. 2;
B. 2.5;
C. 3;
D. 2.0;
E. 3.0;
287.
If you attempt to add an int, a long, and a double, the result will be a __________ value.
A. float
B. int
C. long
D. double
288.
Which of the following is the correct expression of character 4?
A. 4
B. "4"
C. '\0004'
D. '4'
289.
A character is stored in __________.
A. one byte
B. two bytes
C. three bytes
D. four bytes
290.
Suppose x is a char variable with a value 'b'. What is the printout of the statement cout << ++x?
A. a
B. b
C. c
D. d
291.
Which of the following statement prints smith\exam1\test.txt?
A. cout << "smith\exam1\test.txt";
B. cout << "smith\\exam1\\test.txt";
C. cout << "smith\"exam1\"test.txt";
D. cout << "smith"\exam1"\test.txt";
292.
Suppose i is an int type variable. Which of the following statements display the character whose ASCII is stored in variable i?
A. cout << i;
B. cout << (char)i;
C. cout << (int)i;
D. cout << i;
293.
The ASCII of 'a' is 97. What is the ASCII for 'c'?
A. 96
B. 97
C. 98
D. 99
294.
What is the printout of cout << 'z' - 'a'?
A. 25
B. 26
C. a
D. z
295.
An int variable can hold __________.
A. 'x'
B. 120
C. 120.0
D. "x"
E. "120"
296.
Which of the following assignment statements is correct?
A. char c = 'd';
B. char c = 100;
C. char c = "d";
D. char c = "100";
297.
The expression "C++" + 1 + 2 + 3 evaluates to ________.
A. C++123
B. C++6
C. C++15
D. C++33
E. Illegal expression
298.
Note that the ASCII for character A is 65. The expression "A" + 1 evaluates to ________.
A. 66
B. B
C. A1
D. Illegal expression
299.
Note that the ASCII for character A is 65. The expression 'A' + 1 evaluates to ________.
A. 66
B. B
C. A1
D. Illegal expression
300.
Analyze the following code.
#include
using namespace std;
int main()
{
int i;
int j;
cout << "Enter an integer: ";
cin >> j;
i = (i + 4);
}
A. The program cannot compile because j is not initialized.
B. The program cannot compile because i does not have an initial value when it is used in i = i + 4;
C. The program compiles but has a runtime error because i does not have an initial value when it is used in i = i + 4;
D. The program compiles and runs fine.
301.
The __________ function returns a raised to the power of b.
A. power(a, b)
B. exponent(a, b)
C. pow(a, b)
D. pow(b, a)
302.
The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
A. 76.02
B. 76
C. 76.0252175
D. 76.03
303.
The function time(0) returns ________________ .
A. the current time.
B. the current time in milliseconds.
C. the current time in milliseconds since midnight.
D. the current time in milliseconds since midnight, January 1, 1970.
E. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).
304.
Programming style is important, because ______________.
A. a program may not compile if it has a bad style
B. good programming style can make a program run faster
C. good programming style makes a program more readable
D. good programming style helps reduce programming errors
305.
According to the C++ naming convention adopted by this book, which of the following names can be variables?
A. FindArea
B. findArea
C. totalLength
D. TOTAL_LENGTH
E. class
306.
Which of these is a postfix expression?
a. -5 b. a + b c. ++c d. c++ e. b – -(–c)
307.
Which of these us a unary expression?
a. -5 b. a + b c. num = a + b d. a+(b / c) e. 0
308.
Which is NOT a valid assignment operaton?
a. a = 4 b. 4 = a c. x = x d. a = a++ e. x = –(-4)
309.
Which of these operators has highest precedence?
a. = b. ( ) c. % d. ++ e. /
310.
Which of these operators has lowest precedence?
a. = b. — c. % d. ( ) e. /
311.
What is the value of the expression: 23 / 5?
a. 4 b. 4.6 c. 0 d. 5 e. 3
312.
What is the value of the expression: 23 % 5
a. 4 b. 4.6 c. 0 d. 5 e. 3
313.
Which of these is right to left associative?
a. % b. * c. + d. ( ) e. /=
314.
If a = 5 before this statement what is the value of c afterward? c = a++;
a. 0 b. 5 c. 6 d. 4 e. none of these
315.
What is the value of the expression: 4 * 5 – 3 / 2 % 4
a. 1 b. 0 c. 18.5 d. 14 e. 19
In all program, assume that the required header file/files has /have been included
Consider the data type
char is 1 byte
int is 2 byte
long int 4 byte
float is 4 byet
double is 8 byte
long double is 10 byte
pointer is 2 byte
316.
Consider the following program:
main()
{
int a, b,c, d;
a=3;
b=5;
c=a,b;
d=(a,b);
printf(”c=%d” ,c);
printf(”d=%d” ,d);
}
The output for this program is:
(a) c=3 d=3
(b) c=5 d=3
(c) c=3 d=5
(d) c=5 d=5
317.
Consider the following code segment:
int foo ( int x , int n)
{
int val;
val =1;
if (n>0)
{
if (n%2 == 1) val = val *x;
val = val * foo(x*x , n/2);
}
return val;
}
What function of x and n is compute by this code segment?
(a) xn
(b) x*n
(c) nx
(d) None of the above
318.
Consider the following program:
main()
{
int i=3;
int j;
j = sizeof(++i+ ++i);
printf(”i=%d j=%d”, i ,j);
}
The output for this program is:
(a) i=4 j=2
(b) i=3 j=2
(c) i=3 j=4
(d) i=3 j=6
319.
Consider the following program:
void e(int );
main()
{
int a;
a=3;
e(a);
}
void e(int n)
{
if(n>0)
{
e(–n);
printf(”%d” , n);
e(–n);
}
}
The output for this program is:
(a) 0 1 2 0
(b) 0 1 2 1
(c) 1 2 0 1
(d) 0 2 1 1
320.
Consider the following program:
int counter (int i)
{
static int count =0;
count = count +i;
return (count );
}
main()
{
int i , j;
for (i=0; i <=5; i++)
j = counter(i);
}
The value of j at the end of the execution of the this program is:
(a) 10
(b) 15
(c) 6
(d) 7
321.
Consider the following program:
#include
static jmp_buf buf;
main()
{
volatile int b;
b =3;
if(setjmp(buf)!=0)
{
printf(”%d “, b);
exit(0);
}
b=5;
longjmp(buf , 1);
}
The output for this program is:
(a) 3
(b) 5
(c) 0
(d) None of the above
322.
Consider the following program:
void e(int );
main()
{
int a;
a=3;
e(a);
}
void e(int n)
{
if(n>0)
{
e(–n);
printf(”%d” , n);
e(–n);
}
}
The output for this program is:
(a) 0 1 2 0
(b) 0 1 2 1
(c) 1 2 0 1
(d) 0 2 1 1
323.
Consider the following program:
main()
{
int a[5] = {1,2,3,4,5};
int *ptr = (int*)(&a+1);
printf(”%d %d” , *(a+1), *(ptr-1) );
}
The output for this program is:
(a) 2 2
(b) 2 1
(c) 2 5
(d) None of the above
324.
Consider the following program:
void foo(int [][3] );
main()
{
int a [3][3]= { { 1,2,3} , { 4,5,6},{7,8,9}};
foo(a);
printf(”%d” , a[2][1]);
}
void foo( int b[][3])
{
++ b;
b[1][1] =9;
}
The output for this program is:
(a) 8
(b) 9
(c) 7
(d) None of the above
325.
Consider the following program:
main()
{
int a[][3] = { 1,2,3 ,4,5,6};
int (*ptr)[3] =a;
printf(”%d %d ” ,(*ptr)[1], (*ptr)[2] );
++ptr;
printf(”%d %d” ,(*ptr)[1], (*ptr)[2] );
}
The output for this program is:
(a) 2 3 5 6
(b) 2 3 4 5
(c) 4 5 0 0
(d) None of the above
326.
Consider following function
int *f1(void)
{
int x =10;
return(&x);
}
int *f2(void)
{
int*ptr;
*ptr =10;
return ptr;
}
int *f3(void)
{
int *ptr;
ptr=(int*) malloc(sizeof(int));
return ptr;
}
Which of the above three functions are likely to cause problem with pointers
(a) Only f3
(b) Only f1 and f3
(c) Only f1 and f2
(d) f1 , f2 ,f3
327.
Consider the following program:
void f1(int *, int);
void f2(int *, int);
void(*p[2]) ( int *, int);
main()
{
int a;
int b;
p[0] = f1;
p[1] = f2;
a=3;
b=5;
p[0](&a , b);
printf(”%d\t %d\t” , a ,b);
p[1](&a , b);
printf(”%d\t %d\t” , a ,b);
}
void f1( int* p , int q)
{
int tmp;
tmp =*p;
*p = q;
q= tmp;
}
void f2( int* p , int q)
{
int tmp;
tmp =*p;
*p = q;
q= tmp;
}
The output for this program is:
(a) 5 5 5 5
(b) 3 5 3 5
(c) 5 3 5 3
(d) 3 3 3 3
328.
Consider the following program:
main()
{
struct node
{
int a;
int b;
int c;
};
struct node s= { 3, 5,6 };
struct node *pt = &s;
printf(”%d” , *(int*)pt);
}
The output for this program is:
(a) 3
(b) 5
(c) 6
(d) 7
329.
Consider following declaration
typedef int (*test) ( float * , float*)
test tmp;
type of tmp is
(a) Pointer to function of having two arguments that is pointer to float
(b) int
(c) Pointer to function having two argument that is pointer to float and return int
(d) None of the above
330.
Consider the following program:
main()
{
char *p;
char buf[10] ={ 1,2,3,4,5,6,9,8};
p = (buf+1)[5];
printf(”%d” , p);
}
The output for this program is:
(a) 5
(b) 6
(c) 9
(d) None of the above
331.
Consider the following program:
Void f(char**);
main()
{
char * argv[] = { “ab” ,”cd” , “ef” ,”gh”, “ij” ,”kl” };
f( argv );
}
void f( char **p )
{
char* t;
t= (p+= sizeof(int))[-1];
printf( “%s” , t);
}
The output for this program is:
(a) ab
(b) cd
(c) ef
(d) gh
332.
What variables stores the number of arguments to a program?
A. argc
B. argv
C count
D. arglen
333.
What is argv[0]?
A. The number of arguments to the program
B. The name of the program
C. The first argument to the program
D. This syntax is illegal
334.
What type is argv?
A. char *
B. int
C. char **
D. It’s not a variable
335.
In what order do the two command line variables appear in the definition of main?
A. Count then argument array
B. Argument array then count
C. They don’t appear in the definition of main
D. There is only one argument.
336.
What does the argument count variable store?
A. the number of arguments
B. the number of arguments plus one
C. the number of arguments minus one
D. The total size of the argument array.
337.
Which of the following is the boolean operator for logical-and?
A. &
B. &&
C. |
D. |&
338.
Evaluate !(1 && !(0 || 1)).
A. True
B. False
C. Unevaluatable
339.
Which of the following shows the correct syntax for an if statement?
A. if expression
B. if{ expression
C. if( expression)
D. expression if
340.
What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?
A. 10
B. 9
C. 0
D. 1
341.
When does the code block following while(x<100) execute?
A. When x is less than one hundred
B. When x is greater than one hundred
C. When x is equal to one hundred
D. While it wishes
342.
Which is not a loop structure?
A. For
B. Do while
C. While
D. Repeat Until
343.
How many times is a do while loop guaranteed to loop?
A. 0
B. Infinitely
C. 1
D. Variable
344.
Which follows the case statement?
A. :
B. ;
C. -
D. A newline
345.
What is required to avoid falling through from one case to the next?
A. end;
B. break;
C. Stop;
D. A semicolon.
346.
What keyword covers unhandled possibilities?
A. all
B. contingency
C. default
D. other
347.
What is the result of the following code?
x=0;
switch(x)
{
case 1: printf( "One" );
case 0: printf( "Zero" );
case 2: printf( "Hello World" );
}
A. One
B. Zero
C. Hello World
D. ZeroHello World
348.
Which is not a proper prototype?
A. int funct(char x, char y);
B. double funct(char x)
C. void funct();
D. char x();
349.
What is the return type of the function with prototype: "int func(char x, float v, double t);"
A. char
B. int
C. float
D. double
350.
Which of the following is a valid function call (assuming the function exists)?
A. funct;
B. funct x, y;
C. funct();
D. int funct();
351.
Which of the following is a complete function?
A. int funct();
B. int funct(int x) {return x=x+1;}
C. void funct(int) { printf( "Hello");
D. void funct(x) { printf( "Hello"); }
352.
What does the inline keyword do?
A. Indicates a function declaration
B. Tells the compiler to use the function only within the same source code file
C. Causes all function calls to be replaced by the code from the function
D. Allows one-line function declarations
353.
Why would you want to use inline functions?
A. To decrease the size of the resulting program
B. To increase the speed of the resulting program
C. To simplify the source code file
D. To remove unnecessary functions
354.
Which of the following is a limit on inline functions?
A. Inline functions cannot return a value.
B. Inline functions must resturn a value.
C. Inline functions must be less than ten lines.
D. The compiler may choose to ignore an inline directive.
355.
Which of the following is a valid inline for function foo?
A. inline void foo() {}
B. void foo() inline {}
C. inline:void foo() {}
D. None of the above
356.
How can you ensure that an inline function isn't inlined for a particular function call for function foo?
A. unline x();
B. noexpand x();
C. x();
D. This is not possible on a case-by-case basis
357.
Which of the following is the proper declaration of a pointer?
A. int x;
B. int &x;
C. ptr x;
D. int *x;
358.
Which of the following gives the memory address of integer variable a?
A. *a;
B. a;
C. &a;
D. address(a);
359.
Which of the following gives the memory address of a pointer a?
A. a;
B. *a;
C. &a;
D. address(a);
360.
Which of the following gives the value stored in pointer a?
A. a;
B. val(a);
C. *a;
D. &a;
361.
Which of the following is the proper keyword to allocate memory in C?
A. new
B. malloc
C. create
D. value
362.
Which of the following is the proper keyword to deallocate memory?
A. free
B. delete
C. clear
D. remove
363.
Which of the following accesses a variable in structure b?
A. b->var;
B. b.var;
C. b-var;
D. b>var;
364.
Which of the following accesses a variable in structure *b?
A. b->var;
B. b.var;
C. b-var;
D. b>var;
365.
Which of the following is a properly defined struct?
A. struct {int a;}
B. struct a_struct {int a;}
C. struct a_struct int a;
D. struct a_struct {int a;};
366.
Which properly declares a variable of struct foo?
A. struct foo;
B. struct foo var;
C. foo;
D. int foo;
367.
Which of the following correctly declares an array?
A. int anarray[10];
B. int anarray;
C. anarray{10};
D. array anarray[10];
368.
What is the index number of the last element of an array with 29 elements?
A. 29
B. 28
C. 0
D. Programmer-defined
369.
Which of the following is a two-dimensional array?
A. array anarray[20][20];
B. int anarray[20][20];
C. int array[20, 20];
D. char array[20];
370.
Which of the following correctly accesses the seventh element stored in foo, an array with 100 elements?
A. foo[6];
B. foo[7];
C. foo(7);
D. foo;
371.
Which of the following gives the memory address of the first element in array foo, an array with 100 elements?
A. foo[0];
B. foo;
C. &foo;
D. foo[1];
372.
Which of the following is a static string?
A. Static String
B. “Static String”
C. ‘Static String’
D. char string[100];
373.
What character ends all strings?
A. ‘.’
B. ‘ ‘
C. ‘\0′
D. ‘\n’
374.
Which of the following reads in a string named x with one hundred characters?
A fgets(x, 101, stdin);
B. fgets(x, 100, stdin);
C. readline(x, 100, ‘\n’);
D. read(x);
375.
Which of the following functions compares two strings?
A. compare();
B. stringcompare();
C. cmp();
D. strcmp();
376.
Which of the following adds one string to the end of another?
A. append();
B. stringadd();
C. strcat();
D. stradd();
377.
Which of the following classes handlers file input?
A. ofstream
B. ifstream
C. instream
D. inputfile
378.
Which of the following is not a valid ofstream argument?
A. ios::app
B. ios::trunc
C. ios::noreplace
D. ios::create
379.
What does ios::ate mean as an argument to ofstream?
A. Open file, but do not create.
B. Open file, create.
C. Open file, allow additions to the end.
D. Open file, allow additions anywhere.
380.
How would you output to an open file named a_file?
A. a_file.out(”Output”);
B. a_file=”Output”;
C. a_file<<"Output";
D. a_file.printf("Output");
381.
What header file contains C++ file I/O instructions?
A. iostream.h
B. fstream.h
C. infstream.h
D. outstream.h
382.
Which header file do you need to include to use typecasting?
A. iostream.h
B. ctype.h
C. math.h
D. None
383.
Which is a valid typecast?
A. a(char);
B. char:a;
C. (char)a;
D. to(char, a);
384.
Why can typecasting be dangerous?
A. Some conversions are not defined, such as char to int.
B. You might permanently change the value of the variable.
C. You might temporarily lose part of the data - such as truncating a float when typecasting to an int.
D. There are no dangers.
385.
Which is a good use for typecasting?
A. To allow division of two integers to return a decimal value.
B. To allow your program to use nothing but integers.
C. To change the return type of a function.
D. To swap variables rapidly.
386.
Which conversion is not possible?
A. int to float
B. float to int
C. char to float
D. All are possible
387.
What purpose do classes serve?
A. data encapsulation
B. providing a convenient way of modeling real-world objects
C. simplifying code reuse
D. all of the above
388.
Which is not a protection level provided by classes in C++?
A. protected
B. hidden
C. private
D. public
389.
What value must a destructor return?
A. A pointer to the class.
B. An object of the class.
C. A status code determining whether the class was destructed correctly
D. Destructors do not return a value.
390.
Which of the following is a valid class declaration?
A. class A { int x; };
B. class B { }
C. public class A { }
D. object A { int x; };
391.
Which functions will every class contain?
A. None
B. Constructor
C. Destructor
D. Both a constructor and a destructor
392.
int a=10,b;
b=a++ + ++a;
printf("%d,%d,%d,%d",b,a++,a,++a);
What will be the output when following code is executed
12,10,11,13
22,10,11,13
22,11,11,11
12,11,11,11
22,13,13,13
393.
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?
1
2
3
4
5
394.
Which one of the following will read a character from the keyboard and will
store it in the variable c?
c = getc();
getc( &c );
c = getchar( stdin );
getchar( &c )
c = getchar();
395.
#include
int i;
void increment( int i )
{
i++;
}
int main()
{
for( i = 0; i < 10; increment( i ) )
{
}
printf("i=%d\n", i);
return 0;
}
What will happen when the program above is compiled and executed?
It will not compile.
It will print out: i=9.
It will print out: i=10.
It will print out: i=11.
It will loop indefinitely.
396.
int i = 4;
switch (i)
{
default:
;
case 3:
i += 5;
if ( i == ![]()
{
i++;
if (i == 9) break;
i *= 2;
}
i -= 4;
break;
case 8:
i += 5;
break;
}
printf("i = %d\n", i);
What will the output of the sample code above be?
i = 5[Ans]
i = 8
i = 9
i = 10
i = 18
397.
int x = 0;
for ( ; ; )
{
if (x++ == 4)
break;
continue;
}
printf("x=%d\n", x);
What will be printed when the sample code above is executed?
x=0
x=1
x=4
x=5
x=6
398.
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?
if (x == 0) return 0;
return 1;
if (x >= 2) return 2;
if (x == 0) return 1;
if (x <= 1) return 1;
399.
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?
400.
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable "ptr"?
ptr = ptr + sizeof(myStruct);
++(int*)ptr;
ptr = ptr + sizeof(myArray);
increment(ptr);
ptr = ptr + sizeof(ptr);
401.
Which of the following statements are true?
A. Every recursive function must have a base case or a stopping condition.
B. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case.
C. Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case.
D. Every recursive function must have a return value.
E. A recursive function is invoked differently from a non-recursive function.
402.
Fill in the code to complete the following function for computing factorial.
/** Return the factorial for a specified index */
long factorial(int n)
{
if (n == 0) // Base case
return 1;
else
return _____________; // Recursive call
}
A. n * (n - 1)
B. n
C. n * factorial(n - 1)
D. factorial(n - 1) * n
403.
Analyze the following recursive function.
long factorial(int n)
{
return n * factorial(n - 1);
}
A. Invoking factorial(0) returns 0.
B. Invoking factorial(1) returns 1.
C. Invoking factorial(2) returns 2.
D. Invoking factorial(3) returns 6.
E. The function runs infinitely and runs out of memory.
404.
Which of the following statements are true?
A. The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series.
B. The Fibonacci series begins with 1 and 1, and each subsequent number is the sum of the preceding two numbers in the series.
C. The Fibonacci series begins with 1 and 2, and each subsequent number is the sum of the preceding two numbers in the series.
D. The Fibonacci series begins with 2 and 3, and each subsequent number is the sum of the preceding two numbers in the series.
405.
Fill in the code to complete the following function for computing a Fibonnaci number.
long fib(long index)
{
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return __________________;
}
A. fib(index - 1)
B. fib(index - 2)
C. fib(index - 1) + fib(index - 2)
D. fib(index - 2) + fib(index - 1)
406.
In the following function, what is the base case?
int xFunction(int n)
{
if (n == 1)
return 1;
else
return n + xFunction(n - 1);
}
A. n is 1.
B. n is greater than 1.
C. n is less than 1.
D. no base case.
407.
What is the return value for xFunction(4) after calling the following function?
int xFunction(int n)
{
if (n == 1)
return 1;
else
return n + xFunction(n - 1);
}
A. 12
B. 11
C. 10
D. 9
408.
Fill in the code to complete the following function for checking whether a string is a palindrome.
bool isPalindrome(const char * const s)
{
if (strlen(s) <= 1) // Base case
return true;
else if _____________________________ // Base case
return false;
else
return isPalindrome(substring(s, 1, strlen(s) - 2));
}
A. (s[0] != s[strlen(s) - 1])
B. (s[0] == s[strlen(s) - 1])
C. (s[0] <> s[strlen(s) - 1])
D. (s[0] = s[strlen(s) - 1])
409.
Analyze the following code:
#include
using namespace std;
void xFunction(int x[], int length)
{
cout << " " << x[length - 1];
xFunction(x, length - 1);
}
int main()
{
int x[] = {1, 2, 3, 4, 5};
xFunction(x, 5);
}
A. The program displays 1 2 3 4 6.
B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.
C. The program displays 5 4 3 2 1.
D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
410.
Fill in the code to complete the following function for checking whether a string is a palindrome.
bool isPalindrome(const char * const s, int low, int high)
{
if (high <= low) // Base case
return true;
else if (s[low] != s[high]) // Base case
return false;
else
return ____________________________;
}
bool isPalindrome(const char * const s)
{
return isPalindrome(s, 0, strlen(s) - 1);
}
A. isPalindrome(s)
B. isPalindrome(s, low, high)
C. isPalindrome(s, low + 1, high)
D. isPalindrome(s, low, high - 1)
E. isPalindrome(s, low + 1, high - 1)
411.
Fill in the code to complete the following function for sorting a list.
void sort(char list[], int high)
{
if (high > 1)
{
// Find the largest element and its index
int indexOfMax = 0;
char max = list[0];
for (int i = 1; i <= high; i++)
{
if (list[i] > max)
{
max = list[i];
indexOfMax = i;
}
}
// Swap the largest with the last element in the list
list[indexOfMax] = list[high];
list[high] = max;
// Sort the remaining list
sort(list, high – 1);
}
}
void sort(char list[])
{
____________________________;
}
void sort(double[] list) {
___________________________;
}
A. sort(list)
B. sort(list, strlen(list))
C. sort(list, strlen(list) – 1)
D. sort(list, strlen(list) – 2)
412.
Fill in the code to complete the following function for binary search.
int binarySearch(const int list[], int key, int low, int high)
{
if (low > high) // The list has been exhausted without a match
return -low – 1; // Return -insertion point – 1
int mid = (low + high) / 2;
if (key < list[mid])
return binarySearch(list, key, low, mid - 1);
else if (key == list[mid])
return mid;
else
return binarySearch(list, key, mid + 1, high);
}
int binarySearch(const int list[], int key, int size)
{
int low = 0;
int high = size - 1;
return __________________________;
}
A. recursiveBinarySearch(list, key)
B. recursiveBinarySearch(list, key, low + 1, high - 1)
C. recursiveBinarySearch(list, key, low - 1, high + 1)
D. recursiveBinarySearch(list, key, low, high)
Recursion versus Iteration
413.
Which of the following statements are true?
A. Recursive functions run faster than non-recursive functions.
B. Recursive functions usually takes more memory space than non-recursive functions.
C. A recursive function can always be replaced by a non-recursive function.
D. In some cases, however, using recursion enables you to give a natural, straightforward, simple solution to a program that would otherwise be difficult to solve
414.
__________ represents an entity in the real world that can be distinctly identified.
A. A class
B. An object
C. A function
D. A data field
415.
_______ is a construct that defines objects of the same type.
A. A class
B. An object
C. A function
D. A data field
416.
An object is an instance of a __________.
A. program
B. class
C. function
D. data
417.
The keyword __________ is required to declare a class.
A. public
B. private
C. class
D. All of the above.
418.
________ is invoked to create an object.
A. A constructor
B. The main function
C. A function with a return type
D. A function with the void return type
419.
Which of the following statements are true?
A. A default no-arg constructor is provided automatically if no constructors are explicitly declared in the class.
B. At least one constructor must always be defined explicitly.
C. Constructors do not have a return type, not even void.
D. Constructors must have the same name as the class itself.
E. Constructors are invoked when an object is created.
420.
Analyze the following code:
#include
using namespace std;
class A
{
public:
int s;
A(int newS)
{
s = newS;
}
void print()
{
cout << s;
}
};
int main()
{
A a;
a.print();
}
A. The program has a compilation error because class A is not a public class.
B. The program has a compilation error because class A does not have a default constructor.
C. The program compiles and runs fine and prints nothing.
D. The program would compile and run if you change A a to A a(5).
421.
What is wrong in the following code?
#include
using namespace std;
class TempClass
{
public:
int i;
TempClass()
{
int i = 5;
}
};
int main()
{
TempClass temp(2);
}
A. The program has a compilation error because TempClass does not have a default constructor.
B. The program has a compilation error because TempClass does not have a constructor with an int argument.
C. The program compiles fine, but it does not run because class C is not public.
D. The program compiles and runs fine.
422.
Given the declaration Circle x, which of the following statement is most accurate.
A. x contains an int value.
B. x refers to an object of the Circle type.
C. *x refers to an object of the Circle type.
D. You can assign an int value to x.
423.
Analyze the following code.
#include
using namespace std;
class Test
{
public:
int x;
Test()
{
cout << "Test";
}
};
int main()
{
Test test;
cout << test.x;
}
A. The program has a syntax error because test is not initialized.
B. The program has a syntax error because x has not been initialized.
C. The program runs fine, but test.x is unpredictable.
D. The program has a syntax error because Test does not have a default constructor.
424.
There are no default value for data fields in a class.
A. true
B. false
425.
Which of the following statements are true?
A. local variables do not have default values.
B. data fields have no default values.
C. A variable of a primitive type holds a value of the primitive type.
D. An object name is like a constant, which cannot be reassigned with a new object.
426.
Suppose circle1 and circle2 are two Circle objects. What does the following statement do?
circle2 = circle1;
A. It copies the contents of circle1 to circle2.
B. It makes circle2 and circle1 the same object.
C. It copies the contents of circle2 to circle1.
D. This statement is illegal.
427.
Which of the following statements are true?
A. Object names are like array names. Once an object name is declared, it references to an object.
B. Object names cannot be reassigned to reference another object.
C. An object name is a constant, though the contents of the object may change.
D. An object is associated with only one object name.
428.
Which of the following statements are true?
A. The statement Circle circle = Circle() creates a Circle object using the no-arg constructor and copies its contents to circle.
B. The statement Circle circle = Circle(5) creates a Circle object with radius 5 and copies its contents to circle.
C. Circle circle = Circle() should be replaced by Circle circle.
D. Circle circle = Circle(5) should be replaced by Circle circle(5).
429.
Analyze the following code.
#include
using namespace std;
class B
{
public:
B() { };
int k;
};
int main()
{
B b;
cout << b.k << endl;
return 0;
}
A. The program has a compile error because b.k cannot be accessed.
B. The program displays 0.
C. The program displays 1.
D. The program displays unpredictable number.
E. The program has a runtime error because b.k does not have a value.
430.
Which of the following statements are true?
A. C++ allows you to separate class declaration from implementation.
B. The class declaration describes the contract of the class and the class implementation implements the contract.
C. The class declaration simply lists all the data fields, constructor prototypes, and the function prototypes. The class implementation implements the constructors and functions.
D. The class declaration and implementation are in two separate files. Both files should have the same name, but with different extension names.
E. The class declaration file has an extension name .h and the class implementation file has an extension name .cpp.
431.
Which of the following statements are true?
A. The :: symbol is called the scope operator.
B. The binary scope operator can be used as ClassName::member to tell the compiler that member belongs to a class.
C. The unary scope operator can be used as ::var to tell the compiler that the variable is a global variable.
432.
Show the output of the following code:
#include
using namespace std;
class A
{
public:
int x;
int y;
int z;
A(): x(1), y(2), z(3)
{
}
};
int main()
{
A a;
cout << a.x << " " << a.y << " " << a.z;
return 0;
}
A. 1 1 1
B. 1 1 2
C. 1 2 3
D. 2 2 2
E. 3 3 3
433.
Show the output of the following code:
#include
using namespace std;
class A
{
public:
int x;
int y;
int z;
A(): x(1), y(2), z(3)
{
}
};
int main()
{
A a;
A *p1 = &a;
a.x = 2;
A a1;
p1 = &a1;
cout << p1->x << " " << (p1*).y << " " << p1->z;
return 0;
}
A. 1 1 1
B. 1 1 2
C. 1 2 3
D. 2 2 2
E. 3 3 3
434.
Which of the following statements are correct?
Which of the following statements are correct to delete a dynamic object from a pointer p?
A. delete *p;
B. delete p;
C. delete [] *p;
D. delete [] p;
435.
What is the output of the following code?
string s(”abc”);
s.append(”welcome”);
cout << s;
A. abcwelcome
B. abc
C. welcome
D. welcomeabc
436.
What is the output of the following code?
string s("abc");
s.append("welcome", 0, 3);
cout << s;
A. abcwelcome
B. abc
C. abcwel
D. welcomeabc
437.
What is the output of the following code?
string s("abc");
s.append("welcome", 3);
cout << s;
A. abcwelcome
B. abc
C. abcwel
D. welcomeabc
438.
What is the output of the following code?
string s("abc");
s.append(3, 'w');
cout << s;
A. abcwelcome
B. abc
C. abcwel
D. abcwww
439.
What is the output of the following code?
string s("abc");
s.assign("welcome");
cout << s;
A. abcwelcome
B. abc
C. welcome
D. abcwww
440.
What is the output of the following code?
string s("abc");
s.assign("welcome", 0, 3);
cout << s;
A. abcwelcome
B. abc
C. welcome
D. abcwww
E. wel
441.
What is the output of the following code?
string s("abc");
s.assign("welcome", 3);
cout << s;
A. abcwelcome
B. abc
C. welcome
D. abcwww
E. wel
442.
What is the output of the following code?
string s("abc");
s.assign(3, 'w');
cout << s;
A. abcwww
B. abc
C. www
D. abcwww
E. wel
443.
What is the output of the following code?
string s("abcd");
cout << s.at(1);
A. a
B. b
C. c
D. d
444.
What is the output of the following code?
string s("abcd");
cout << s.length();
A. 1
B. 2
C. 3
D. 4
E. 5
445.
What is the output of the following code?
string s("abcd");
cout << s.size();
A. 1
B. 2
C. 3
D. 4
E. 5
446.
What is the output of the following code?
string s("abcd");
s.clear();
cout << s.length();
A. 1
B. 2
C. 3
D. 4
E. 0
447.
What is the output of the following code?
string s("abcdefg");
cout << s.compare("abb");
A. 1
B. 0
C. -1
D. -2
448.
What is the output of the following code?
string s("abcdefg");
s.erase(2, 3);
cout << s;
A. abcd
B. abfg
C. abcg
D. aefg
449.
What is the output of the following code?
string s("abcdefg");
char s1[15] = "welcome";
s.copy(s1, 3, 0);
cout << s1;
A. welcome
B. abccome
C. welcomeabc
D. abcwelcome
450.
What is the output of the following code?
string s("abcdefg");
cout << s.substr(1, 3);
A. abc
B. bcd
C. a
D. c
451.
What is the output of the following code?
string s("abcdefg");
cout << s.substr(3);
A. abc
B. bcd
C. defg
D. efg
452.
What is the output of the following code?
string s("abcdefg");
string s1("welcome");
s.swap(s1);
cout << s;
A. abcdefg
B. welcome
453.
What is the output of the following code?
string s("abcdefag");
cout << s.find("def") << " " << s.find("a", 3);
A. 3 0
B. 3 6
C. 2 4
D. 0 0
454.
What is the output of the following code?
string s("abcdefg");
s.replace(1, 2, "wel");
cout << s;
A. abcdefg
B. aweldefg
C. welabcdefg
D. awelbcdefg
455.
What is the output of the following code?
string s("abcdefg");
s.insert(1, "wel");
cout << s;
A. abcdefg
B. aweldefg
C. welabcdefg
D. awelbcdefg
456.
What is the output of the following code?
string s("abcdefg");
s.insert(1, 3, 'w');
cout << s;
A. abcdefg
B. aweldefg
C. awwwbcdefg
D. awelbcdefg
457.
Which of the following statements are true?
A. Use the private keyword to encapsulate data fields.
B. Encapsulating data fields makes the program easy to maintain.
C. Encapsulating data fields makes the program short.
D. Encapsulating data fields helps prevent programming errors.
E. If you don't use the public keyword, the visibility is private by default.
458.
Suppose you wish to provide an accessor function for a boolean property finished, what signature of the function should be?
A. void getFinished()
B. bool getFinished()
C. bool isFinished()
D. void isFinished()
459.
What is the output of the following code?
#include
using namespace std;
class Foo
{
public:
int x; // data field
int y; // data field
Foo()
{
x = 10;
y = 10;
}
void p()
{
int x = 20; // local variable
cout << "x is " << x << " ";
cout << "y is " << y << endl;
}
};
int main()
{
Foo foo;
foo.p();
return 0;
}
A. x is 10 y is 10
B. x is 20 y is 20
C. x is 20 y is 10
D. x is 10 y is 20
460.
Analyze the following code.
#include
using namespace std;
class B
{
public:
B() { };
private:
int k;
};
int main()
{
B b;
cout << b.k << endl;
return 0;
}
A. The program displays 0.
B. The program displays 1.
C. The program displays unpredictable number.
D. The program has a compile error because b.k cannot be accessed.
E. The program has a runtime error because b.k does not have a value.
461.
Analyze the following code:
class Circle
{
public:
Circle(double radius)
{
radius = radius;
}
private:
double radius;
};
A. The program has a compilation error because it does not have a main function.
B. The program will compile, but you cannot create an object of Circle with a specified radius.
The object will always have radius 0.
C. The program will compile, but you cannot create an object of Circle with a specified radius. The object will have an unpredictable value for radius.
D. The program has a compilation error because you cannot assign radius to radius.
E. The program does not compile because Circle does not have a default constructor.
462.
Which of the following statements are correct?
A. C++ allows you to pass a parameter of object type in a function by value.
B. C++ allows you to pass a parameter of object type in a function by reference.
C. C++ allows you to pass a parameter of object type in a function by pointer.
D. Passing objects by reference is commonly used because it saves memory.
463.
When invoking a function with a reference object parameter, ___________ is passed.
A. the contents of the object
B. a copy of the object
C. the reference of the object
D. the object is copied, then the reference of the copied object
464.
What is the printout of the following code?
#include
using namespace std;
class Count
{
public:
int count;
Count(int c)
{
count = c;
}
Count()
{
count = 0;
}
};
void increment(Count c, int times)
{
c.count++;
times++;
}
int main()
{
Count myCount;
int times = 0;
for (int i = 0; i < 100; i++)
increment(myCount, times);
cout << "myCount.count is " << myCount.count;
cout << " times is " << times;
return 0;
}
A. myCount.count is 0 times is 0
B. myCount.count is 100 times is 100
C. myCount.count is 0 times is 100
D. myCount.count is 100 times is 0
465.
What is the printout of the following code?
#include
using namespace std;
class Count
{
public:
int count;
Count(int c)
{
count = c;
}
Count()
{
count = 0;
}
};
void increment(Count &c, int & times)
{
c.count++;
times++;
}
int main()
{
Count myCount;
int times = 0;
for (int i = 0; i < 100; i++)
increment(myCount, times);
cout << "myCount.count is " << myCount.count;
cout << " times is " << times;
return 0;
}
A. myCount.count is 0 times is 0
B. myCount.count is 100 times is 100
C. myCount.count is 0 times is 100
D. myCount.count is 100 times is 0
E. myCount.count is 100 times is 100
466.
What is the printout of the following code?
#include
using namespace std;
class Count
{
public:
int count;
Count(int c)
{
count = c;
}
Count()
{
count = 0;
}
};
void increment(Count c, int & times)
{
c.count++;
times++;
}
int main()
{
Count myCount;
int times = 0;
for (int i = 0; i < 100; i++)
increment(myCount, times);
cout << "myCount.count is " << myCount.count;
cout << " times is " << times;
return 0;
}
A. myCount.count is 0 times is 0
B. myCount.count is 100 times is 100
C. myCount.count is 0 times is 100
D. myCount.count is 100 times is 0
E. myCount.count is 100 times is 100
467.
Given the declaration Circle x[10], which of the following statement is most accurate.
A. x contains an array of ten int values.
B. x contains an array of ten objects of the Circle type.
C. Each element in the array is a Circle object.
D. You cannot assign a new object to the elements in the array, but you can change the contents in each object element.
468.
Show the output of the following code:
#include
#include
using namespace std;
class A
{
public:
A(): i(5), s(”abc”)
{
};
int i; // Declare a data field of the int type
string s; //Declare a data field of the string type
};
int main()
{
A a;
cout << "s is " << a.s.data() << endl;
cout << "i is " << a.i << endl;
return 0;
}
A. The program displays s is abc followed by i is 5.
B. The program displays s is abc followed by i is 0.
C. The program displays s is followed by i is 5.
D. The program displays s is followed by i is 0.
E. The program displays s is followed by i is.
469.
Analyze the following code:
class A
{
public:
A();
private:
int i = 5;
string s("abc");
};
A. The program has a syntax error because int data field i cannot be initialized when it isdeclared.
B. The program has a syntax error because object data field s cannot be created in its declaration.
D. The program has no syntax error.
470.
Which of the following statements are true about an immutable object?
A. The contents of an immutable object cannot be modified.
B. All properties of an immutable object must be private.
C. All properties of an immutable object must be of primitive types.
D. An object type property in an immutable object must also be immutable.
E. An immutable object contains no mutator functions.
471.
Variables that are shared by every instances of a class are __________.
A. public variables
B. private variables
C. instance variables
D. static variables
472.
You should add the static keyword in the place of ? in which of the following function:
#include
using namespace std;
class Test
{
public:
? int square(int n)
{
return n * n;
}
? int getAge()
{
return age;
}
private:
int age;
};
A. in the square function
B. in the getAge function
C. in both lthe square function and the getAge function
D. none
473.
What is wrong in the following code?
#include
using namespace std;
class Test
{
public:
static int square(int n)
{
return n * n;
}
int getAge()
{
return age;
}
private:
int age;
};
int main()
{
cout << Test.square(4);
}
A. It is a syntax error to invoke Test.square(4).
B. You should replace Test.square(4) with square(4).
C. You should replace Test.square(4) with Test->square(4).
D. You should replace Test.square(4) with Test::square(4).
474.
What is wrong in the following code?
#include
using namespace std;
class Test
{
public:
static int square(int n)
{
return n * n;
}
int getAge()
{
return age;
}
static int k = 5;
private:
int age;
};
int main()
{
cout << Test::square(4);
}
A. The static variable k cannot be initialized inside the class.
B. You should replace static int k = 5 with static int k and declare int Test::k = 5 outside the Test class.
C. You should replace static int k = 5 with static int k and declare int Test.k = 5 outside the Test class.
D. You should replace static int k = 5 with static int k and declare int Test->k = 5 outside the Test class.
475.
A function that is associated with an individual object is called __________.
A. a static function
B. a class function
C. an instance function
D. an object function
476.
Which of the following statements are true?
A. Every class has a default constructor if no constructors are defined explicitly.
B. Every class has a default destructor if no destructors are defined explicitly.
C. A class can have only one destructor.
D. The destructor does not have any arguments.
477.
Which of the following statements are true?
A. Every class has a copy constructor with the signature ClassName(ClassName &).
B. The copy constructor can be used to create an object initialized with another object?s data.
C. By default, the copy constructor simply copies each data field in one object to its counterpart in the other object.
D. By default, the copy constructor performs a shallow copy.
478.
Which of the following statements are true?
A. Private members of a class cannot be accessed from outside of the class.
B. C++ enables you to use the friend keyword to declare friend functions and friend classes for
a class so these functions and classes can access the class?s private members.
479.
Analyze the following code:
#include
using namespace std;
class Date
{
friend void p();
private:
int year;
int month;
int day;
};
void p()
{
Date date;
date.year = 2000;
cout << date.year;
}
int main()
{
p();
return 0;
}
A. The program has a syntax error because year is a private data field in Date.
B. The program compiles and runs fine and display 2000.
C. The program will have a syntax error if the line friend void p() is deleted.
D. Since year is private, you cannot access it using date.year in function p().
480.
Which of the following statements are true?
A. An object can contain another object. The relationship between the two is called composition.
B. Composition is actually a special case of the aggregation relationship.
C. Aggregation models has-a relationships and represents an ownership relationship between two objects.
D. The owner object is called an aggregating object and its class an aggregating class.
E. The subject object is called an aggregated object and its class an aggregated class.
481.
Object-oriented programming allows you to derive new classes from existing classes. This is called ____________.
A. encapsulation
B. inheritance
C. abstraction
D. generalization
482.
Which of the following statements are true?
A. A derived class is a subset of a base class.
B. A derived class is usually extended to contain more functions and more detailed information than its base class.
C. "class A: public B" means A is a derived class of B.
D. "class A: public B" means B is a derived class of A.
483.
Suppose Circle and Rectangle classes are derived from GeometricObject and you declared
void displayGeometricObject(GeometricObject shape)
{
cout << shape.toString() << endl;
}
Which of the following function call is correct?
A. displayGeometricObject(GeometricObject("black", true));
B. displayGeometricObject(Circle(5));
C. displayGeometricObject(Rectangle(2, 3));
D. displayGeometricObject(string());
484.
Are the constructors inherited by the derived class?
A. Yes
B. No
485.
Are the destructors inherited by the derived class?
A. Yes
B. No
486.
Suppose class A is derived from B and both A and B have no-arg constructors. To invoke B's constructor from A, use ________.
A. A(): B() { ... }
B. A(): { B(); ... }
C. B(): A() { ... }
D. B(): { A(); ... }
487.
What is the output of the following code?
#include
using namespace std;
class B
{
public:
~B()
{
cout << "B";
}
};
class A: public B
{
public:
~A()
{
cout << "A";
}
};
int main()
{
A a;
return 0;
}
A. AB
B. BA
C. A
D. B
E. AA
488.
What is wrong in the following code?
class Fruit
{
public:
Fruit(int id)
{
}
};
class Apple: public Fruit
{
public:
Apple()
{
}
};
A. The program will compile if you add a no-arg constructor for Fruit.
B. The program has a syntax error because Fruit does not have a no-arg constructor.
C. The program will compile if you delete the constructor in Fruit.
D. The program will compile if you replace Apple() by Apple(): Fruit(4).
489.
Which of the following statements are true?
A. To redefine a function, the function must be defined in the derived class using the same signature and return type as in its base class.
B. Overloading a function is to provide more than one function with the same name but with different signatures to distinguish them.
C. It is a compilation error if two functions differ only in return type.
D. A private function cannot be redefined. If a function defined in a derived class is private in its base class, the two functions are completely unrelated.
490.
Which of the following statements are true?
A. A function can be overloaded in the same class.
B. A function can be redefined in the same class.
C. If a function overloads another function, these two functions must have the same signature.
D. If a function redefines another function, these two functions must have the same signature.
491.
To invoke the toString() function defined in GeometricObject from a Circle object c, use __________.
A. super.toString()
B. c.super.toString()
C. c.GeometricObject::toString()
D. c->GeometricObject::toString()
492.
What is the printout of the following code?
#include
using namespace std;
class C
{
public:
string toString()
{
return “C”;
}
};
class B: public C
{
string toString()
{
return “B”;
}
};
class A: public B
{
string toString()
{
return “A”;
}
};
void displayObject(C *p)
{
cout << p->toString();
}
int main()
{
displayObject(&A());
displayObject(&B());
displayObject(&C());
return 0;
}
A. ABC
B. CBA
C. AAA
D. BBB
E. CCC
493.
What is the printout of the following code?
#include
using namespace std;
class C
{
public:
virtual string toString()
{
return “C”;
}
};
class B: public C
{
string toString()
{
return “B”;
}
};
class A: public B
{
string toString()
{
return “A”;
}
};
void displayObject(C *p)
{
cout << p->toString();
}
int main()
{
displayObject(&A());
displayObject(&B());
displayObject(&C());
return 0;
}
A. ABC
B. CBA
C. AAA
D. BBB
E. CCC
494.
What is the printout of the following code?
#include
using namespace std;
class C
{
public:
string toString()
{
return “C”;
}
};
class B: public C
{
string toString()
{
return “B”;
}
};
class A: public B
{
virtual string toString()
{
return “A”;
}
};
void displayObject(C *p)
{
cout << p->toString();
}
int main()
{
displayObject(&A());
displayObject(&B());
displayObject(&C());
return 0;
}
A. ABC
B. CBA
C. AAA
D. BBB
E. CCC
495.
What is the printout of the following code?
#include
using namespace std;
class C
{
public:
virtual string toString()
{
return “C”;
}
};
class B: public C
{
string toString()
{
return “B”;
}
};
class A: public B
{
string toString()
{
return “A”;
}
};
void displayObject(C p)
{
cout << p.toString();
}
int main()
{
displayObject(A());
displayObject(B());
displayObject(C());
return 0;
}
A. ABC
B. CBA
C. AAA
D. BBB
E. CCC
496.
If A is derived from B, and B is derived from C, B has a virtual function, will this function be dynamically binded?
A. Yes
B. No
497.
If the variable that references the object for the function is not the address of the object, will this function be dynamically binded?
A. Yes
B. No
498.
Which of the following statements are true?
A. If a function is defined virtual in a base class, it is automatically virtual in all its derived classes. It is not necessary to add the keyword virtual in the function declaration in the derived class.
B. If a function will not be redefined, it is more efficient without declaring it virtual, because it takes more time and system resource to bind virtual functions dynamically at runtime.
C. A virtual function may be implemented in several derived classes. C++ dynamically binds the implementation of the function at runtime, decided by the actual class of the object referenced by the variable.
D. The compiler finds a matching function according to parameter type, number of parameters, and order of the parameters at compile time.
499.
Which of the following statements are true?
A. Private members can only be accessed from the inside of the class and public members can be accessed from any other classes.
B. A protected data field or a protected function in a base class can be accessed by name in its derived classes.
C. A public data field or function in a class can be accessed by name by any other program.
500.
Which of the following is an abstract function?
A. virtual double getArea();
B. virtual double getArea() = 0;
C. double getArea() = 0;
D. virtual double getArea();
501.
Which of the following statements are true?
A. An abstract class is declared using a keyword abstract.
B. A class is abstract if it contains a pure virtual function.
C. An abstract class is like a regular class except that you cannot create objects from it.
D. You can declare a class abstract even though it does not contain abstract functions.
502.
Which of the following statements are true?
A. Assigning a pointer of a derived class type to a pointer of its base class type is called upcasting.
B. Assigning a pointer of a base class type to a pointer of its derived class type is called downcasting.
C. Upcasting can be performed implicitly without using the dynamic_cast operator.
D. downcasting must be performed explicitly using the dynamic_cast operator.
503.
Suppose you declared GeometricObject *p = &object. To cast p to Circle, use ___________.
A. Circle *p1 = dynamic_cast
B. Circle *p1 = dynamic_cast
C. Circle p1 = dynamic_cast
D. Circle p1 = dynamic_cast


