Computer Science : Console Output

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 : Console Output

What will the following code result in?

int main(){

    int i = 7;

    const int * ip = &i;

    cout<< *ip << endl;

}

Possible Answers:

Compilation error

8

None of the above.

Runtime Error

7

Correct answer:

7

Explanation:

Let's take a look at the code.

int i = 7;

This line assigns 7 to the variable "i".

 

const int * ip = &i;

This line creates a constant int pointer and assigns the address of "i" to the pointer.

 

cout << *ip << endl;

This line prints the dereference of the pointer, which holds the value 7.

Example Question #31 : Program Implementation

class Base{

    public:

        void foo(int n) { cout << "Base::foo(int)\n"; }

};

class Derive: public Base{

    public:

        void foo(double n) { cout << "Derived::foo(double)\n";}

};

int main(){

    Derived der;

    der.foo(42);

}

 

The above code is written in C++, what is the output of the program?

 

Possible Answers:

The program does not generate any output.

Base::foo(int).

None of the above.

The program does not compile.

Derived::foo(double).

Correct answer:

Derived::foo(double).

Explanation:

This is an example of inheritance. The child class (Derived) is an inherited class of the parent class (Base). When the Derived object is created and the "foo" method is called, foo in the Derived class will be called. If there is the same method in the parent class and child class, the child's method will be called.

Example Question #1 : Console Output

In Swift (iOS), give a description of what this method does. 

 

func getDivisors(num: Int, divisor: Int) -> Bool {

var result: Bool = False

if (num % divisor == 0) {

result = True

}

println(result)

}

Possible Answers:

This function returns true or false.

This function doesn't do anything. 

This function returns false if the number is evenly divisible by the divisor. It returns true otherwise. 

This method returns true if the number is evenly divisible by the divisor. It returns false otherwise. 

Correct answer:

This method returns true if the number is evenly divisible by the divisor. It returns false otherwise. 

Explanation:

The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise. 

Example Question #41 : Program Implementation

Does the code compile and if yes, what is the the output?

public class Test

{

       public static void main ( String [] args )

       {

             int x = 2;

             if (x = 2)

                     System.out.println("Good job.");

              else

                     System.out.println("Bad job);

       }

}

Possible Answers:

Yes it compiles and it prints good job.

No, it doesn't compile.

Yes it compiles and it prints bad job.

Correct answer:

No, it doesn't compile.

Explanation:

It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.

Learning Tools by Varsity Tutors