CS 1621

Midterm 2 Practice Questions

 

These questions should help you get an idea of the types of questions that will be on the 2nd midterm exam.  Remember that you should study ALL of the material stated for the exam.

 

After you have given all of the problems a good try, see the answers below.

 

Fill in the Blanks

 

1)      Old C-style strings are considered to be ____________________________ [static, limited dynamic, dynamic] in length.

 

2)      Java references are addresses just like C++ pointers, but with (at least) 2 important restrictions:  _________________________________ and __________________________________

 

3)      In C++, many type conversions within expressions are done ____________________, as opposed to Ada, in which all conversions must be done _____________________.

 

4)      Given nested if statements where the number of else clauses does not match the number of if clauses, the determination of correct associations can be handled via _________________________ (as with C++ and Java) or via _______________________________ (as with Ada  and BASIC).

 

5)      Implementing local variables within a procedure as ________________________ [stack dynamic, static] variables better supports recursion.

 

T/F (correct false answers)

 

1)      Ada '83 array variables that are passed as IN OUT parameters use the value-result parameter passing mechanism.

 

2)      Since the for loop in C++ is a counting loop, we can pre-calculate the number of iterations for any given application, thereby allowing it to execute more efficiently.

 

3)      A post-condition for the assignment:

A = B = C;

for assignable variables A, B and C in C++ will be that A, B and C all have the same value.

 

Short Answers

 

1)      We discussed in lecture two problems associated with allocation and deallocation of heap-dynamic variables.  Identify, explain and give an example of each of these problems.

 

2)      Consider a C++ two-dimensional array that is implemented in row-major order:  float A[10][5].  Assume A has a base address of B and that a float uses 4 bytes.  Give the byte address of location A[4][3]. Show your work.

 

3)      Addition is mathematically a commutative operation.  However, it is not always commutative when used in a programming language.  Explain and give an example to support these statements.

 

4)      Consider the C++ while loop below.  Rewrite the code so that it is logically equivalent to the code below in all cases, but it uses a do…while loop rather than the loop below.

 

sum = 0;

ctr = 0;

cin >> num;

while (num > 0)

{

     sum += num;

     ctr++;

     cin >> num;

}

 

5)      Consider the pseudocode below.  Give the ALL output (produced by the output statements) for each of the following situations:

a)         Pass-by-reference parameters are used

b)         Pass-by-name parameters are used

 

main()

{

  int i = 0;

     int arr[3] = {0, 0, 0};

     add(i, arr[i]);

     output(i);

     for (int j = 0; j < 3; j++)

          output(arr[j]);

}

 

subprogram add(int A, int B)

{

     B++; A++; B++; A++; B++;

}

 

 

 



 

 

ANSWERS:

 

Fill in the Blanks

 

1)      Old C-style strings are considered to be _____LIMITED DYNAMIC________ [static, limited dynamic, dynamic] in length.

 

2)      Java references are addresses just like C++ pointers, but with (at least) 2 important restrictions:  __NO EXPLICIT DEREFERENCING______ and ____NO POINTER ARITHMETIC_______

 

3)      In C++, many type conversions within expressions are done ___IMPLICITLY______, as opposed to Ada, in which all conversions must be done _____EXPLICITLY______.

 

4)      Given nested if statements where the number of else clauses does not match the number of if clauses, the determination of correct associations can be handled via ______A RULE_____________ (as with C++ and Java) or via ______SYNTAX__________________ (as with Ada  and BASIC).

 

5)      Implementing local variables within a procedure as ____STACK DYNAMIC___ [stack dynamic, static] variables better supports recursion.

 

 

T/F (correct false answers)

 

1)      Ada '83 array variables that are passed as IN OUT parameters use the value-result parameter passing mechanism.       FALSE – Ada '83 does not specify how these parameters should be passed.

2)      Since the for loop in C++ is a counting loop, we can pre-calculate the number of iterations for any given application, thereby allowing it to execute more efficiently.

FALSE – the for loop in C++ is very general – it can be used as a counting loop but is not necessarily so.

3)      A post-condition for the assignment:

A = B = C; 

for assignable variables A, B and C in C++ will be that A, B and C all have the same value;

FALSE – if the = operator is overloaded the result can be arbitrary, since each call could modify the value being assigned

 

Short Answers

 

1)      We discussed in lecture two problems associated with allocation and deallocation of heap-dynamic variables.  Identify, explain and give an example of each of these problems.

ANSWER: The problems are dangling references and garbage accumulation.  Dangling references occur when memory is de-allocated but a pointer variable still points to it – thereby enabling access into invalid locations in the heap.  For example:

Foo * F = new Foo();

delete(F); // free memory

// possible statements in here

F->mutateSomething(X); // mutate data

Garbage accumulates when memory in the heap becomes inaccessible without being de-allocated.  Thus it cannot be used but also cannot be reclaimed.  For example:

Foo * F = new Foo(X);

// possible statements in here

F = new Foo(Y); // old object is now garbage

 

2)      Consider a C++ two-dimensional array that is implemented in row-major order:  float A[10][5].  Assume A has a base address of B and that a float uses 4 bytes.  Give the byte address of location A[4][3].  Show your work.

ANSWER: Since each float is 4 bytes, and each row in the array is 5 floats long, so we know each row is 4(5) = 20 bytes.  Thus, proceeding from the base address, B, we have

lvalue(A[4][3]) =     B + (rows down)*(size of row) + (columns over)*(size of float) =

                              B + (4 * 20) + (3 * 4) =

                              B + 92      

 

3)      Addition is mathematically a commutative operation.  However, it is not always commutative when used in a programming language.  Explain and give an example to support these statements.

ANSWER: If a function used in an expression produces a side-effect, this may prevent an addition from being commutative.  For example, consider the expressions X + f(X) and f(X) + X.  If the function f(X) modifies the value of X, and if the expressions are evaluated left to right, then the value X has when it is added may be different in the two expressions, thereby producing different results. 

 

 

4)      Consider the C++ while loop below.  Rewrite the code so that it is LOGICALLY EQUIVALENT to the code below in all cases, but it uses a do…while loop rather than the loop below.

 

sum = 0;

ctr = 0;

cin >> num;

while (num > 0)

{

     sum += num;

     ctr++;

     cin >> num;

}

 

ANSWER:

 

sum = 0;

ctr = 0;

cin >> num;

if (num > 0)

{

    do

    {

        sum += num;

        ctr++;

        cin >> num;

    } while (num > 0);

}

 

5)      Consider the pseudocode below.  Give the ALL output (produced by the output statements) for each of the following situations:

a)         Pass-by-reference parameters are used

b)         Pass-by-name parameters are used

 

main()

{

  int i = 0;

     int arr[3] = {0, 0, 0};

     add(i, arr[i]);

     output(i);

     for (int j = 0; j < 3; j++)

          output(arr[j]);

}

 

subprogram add(int A, int B)

{

     B++; A++; B++; A++; B++;

}

 

ANSWERS:

a)      2

        3 0 0

b)      2

        1 1 1