2 C++
2 C++
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?
1. 5 2. 6 3. 10 4. 11 5. 12
2. With every use of a memory allocation function, what function should be used to
release allocated memory which is no longer needed?
1. unalloc() 2. dropmem() 3. dealloc() 4. release() 5. free()
3.
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable "ptr"?
1. ptr = ptr + sizeof(myStruct); 2. ++(int*)ptr;
3. ptr = ptr + sizeof(myArray); 4. increment(ptr);
5. 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?
1. y = HELLO 2. y = ELLO 3. y = LLO
4. y = LO 5. 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?
1. It will work correctly since the for loop covers the entire list.
2. It may fail since each node "nPtr" is freed before its next address can be accessed.
3. In the for loop, the assignment "nPtr=nPtr->next" should be changed to
"nPtr=nPtr.next".
4. This is invalid syntax for freeing memory.
5. The loop will never end.
6. What function will read a specified number of elements from a file?
1. fileread() 2. getline() 3. readfile()
4. fread() 5. gets()
7.
"My salary was increased by 15%!"
Select the statement which will EXACTLY reproduce the line of text above.
1. printf("\"My salary was increased by 15/%\!\"\n");
2. printf("My salary was increased by 15%!\n");
3. printf("My salary was increased by 15'%'!\n");
4. printf("\"My salary was increased by 15%%!\"\n");
5. printf("\"My salary was increased by 15'%'!\"\n");
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?
1. 3
2. 5
3. 7
4. 9
5. 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
1. 12,10,11,13 2. 22,10,11,13 3. 22,11,11,11
4. 12,11,11,11 5. 22,13,13,13
11. What number of digits that can be accurately stored in a float (based on the IEEE
Standard 754)?
a) 6 b) 38 c) An unlimited number
a) && b) || c) !
a) 1 b) 0.08642 c) 0
14. Which is not valid in C?
a) -> b) >> c) *
20. True or false, if you keep incrementing a variable, it will become negative?
a) \0 b) . c) \END
22. If you push the numbers (in order) 1, 3, and 5 onto a stack, which pops out first?
a) 5 b) 1 c) 3
24. Evaluate:
int fn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return fn(v/2)+2;
else
return fn(v-1)+3;
}
for fn(7);
a) 10 b) 11 c) 1
26. Which of the following data structures is on average the fastest for retrieving data?
a) Binary Tree b) Hash Table c) Stack
a) 1 b) 0 c) Program is illegal
a) 0 b) 1 c) void
34. What does the following code do: int c=0; cout<<c++<<c;
a) Undefined b) 01 c) 00
36. In int main(int argc, char *argv[]) what is argv[0] usually going to be?
42. Of the numbers 12 23 9 28 which would be at the top of a properly implemented maxheap?
a) 100 b) 99 c) 101
46. Would you rather wait for quicksort, linear search, or bubble sort on a 200000
element array? (Or go to lunch...)
47. Which of the following data structures uses the least memory?
a) struct astruct
{
int x;
float y;
int v;
};
b) union aunion
{
int x;
float v;
};
c) char array[10];
56. Will a recursive function without an end condition ever quit, in practice?
a) Compiler-Specific b) No c) Yes
57. How long does this loop run: for(int x=0; x=3; x++)
Question #2
Assuming that myObj is less than 1000 bytes, is there anything wrong with this code?
char x[1000];
myObj *obj = reinterpret_cast(x);
Question #3
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
Question #4
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
Question #5
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
Question #6
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
Question #7
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 conversion from a void* to an int*
c) Nothing is wrong with this code
Question #8
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
Question #9
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
Question #10
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
Question #1
Which of the following is illegal:
a)
template <class T> func(T x) {}
template <class T> func<T*>(T* x) {}
b)
template <class T>
class myObject {};
c)
template <class T>
class myObj { template <class R> memFunc() {} };
Question #2
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
Question #3
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
Question #4
Are templates conceptually related to polymorphism?
a) Nope
b) Only when the template types are objects
c) Yes, but compile-time polymorphism
Question #5
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
Question #6
When are templates usually instantiated?
a) At runtime
b) At compile time
c) At link time
Question #7
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
Question #8
Which of the following is an invalid template declaration:
a) template <int x> int func() {return x;}
b) template <double x> double func() {return x;}
c) template <typename x> void func(x t) {}
Question #9
What is the result of trying to run this program:
#include <iostream>
template <int T>
struct X
{
enum val {v = T * X<T-1>::v };
};
template <>
struct X<0>
{
enum val {v = 1 };
};
Question #10
Given the below code, what happens when a method invokes callFunc on an object of
type obj?
template <class X> func(X val) {}
template <> func<double>(double val) {}
class obj
{
public:
callFunc() { func(4.5); }
private:
func(int val) {}
};
a) func(int val)
b) template <class X> func(X val)
c) template <> func<double>(double val)