Variables

UP

(C) Theodore Norvell 1999

Question 0

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

No

There is an assignment of 3 to a variable named x, but that variable is different from the one whose value is consulted on the return from the function.

Yes

The assignment of 3 to the "inner" variable named "x" has no effect on the value of the "outer" variable also named "x". It is an entirely different variable. It is possible to have more than one variable in existence at the same time with the same name.

No

No

There is an assignment of 3 to a variable named x, but that variable is different from the one whose value is consulted on the return from the function.


Question 1

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

No

The global variable named "fred" is a red-herring.

No

The variable named "fred" in subroutine "g" is a different variable from the variable named "fred" in subroutine "f". So the assignment within "g" has no effect on the variable declared in "f"

Yes

The variable named "fred" in subroutine "g" is a different variable from the variable named "fred" in subroutine "f". So the assignment within "g" has no effect on the variable declared in "f"

Huh?

Try again.


 

Question 2

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

No

The global variable "fred" is a red-herring.

Yes

This time the parameter to "g" is a reference parameter. This means that it is like another name for the argument variable. In this case the argument variable is "fred" in subroutine "f". The assignment to "frieda" does not change "frieda" (it remains a reference to the same variable), but rather changes "fred" to 13.

No

Note the & in the declaration of "frieda". It indicates a reference parameter.

Huh

Try again.


Question 3

What values are printed by this program?

#include <iostream>
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

No

You can not rely on stack variables being initialized to 0.

No

The second time that "nutsy" is called, the variable "fred" is not initialized. It is important to understand that every call to a subroutine gets different variables. The lifetime of the first "fred" variable ended when the first subroutine call ended.

Yes

The variable "fred" is not initialized, the second time "nutsy" is called. There is no telling what value it has. It may have the value 42, but it may not.

No

It's fairly clear the first statement in main will output a 42..


Question 4

What is printed by this program

#include <iostream>
#include <new>
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.

No.

It is possible that it prints 99. But you can not count on it.

No

Try again.

Almost

Yes

The line cout << *p dereferences a pointer p. But the variable p used to point to was destroyed by the delete statement. If you try to use a variable that has been destroyed, there is no telling what will happen. In some cases your program will crash; in others it will behave oddly; sometimes nothing strange happens at all.


Question 5

What is printed by this program

#include <iostream>
#include <new>
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.

Huh

Try again

No

It is possible that it prints 12. But you can not count on it.

Yes

As in the last question, there is an attempt to use a variable that no longer exists.

No

The program may crash, but you can not count on it. More often when you assign to a variable that has been destroyed already, the assignment is made to some other variable. Bugs like that are very hard to track down.


UP