Computer Science : Compile Time Errors

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 : Debugging

class Base{};

class Derived : public Base{

    public:

        void method(){ cout<< "method1\n"; }

};

class Derived2 : public Base{

    public:

        void method() { cout<< "method2\n"; }

};

int main(){

    Base* bp = new Derived();

    Derived2* d2p = bp;

    d2p -> method();

}

What is the result of compiling and running the program in C++?

 

Possible Answers:

The program does not compile.

The program compiles and runs, printing "method1"

The program compiles and runs to completion without printing anything

The program compiles and runs, printing "method2"

The program compiles and crashes when it runs.

Correct answer:

The program does not compile.

Explanation:

In this problem, Derived1 and Derived2 are children of the Base class. If we take a look at this line:

Base* bp = new Derived();

We are assigning a new Derived class to a base pointer. This will compile. Think of the Base as a larger object because it is the parent, so copying a smaller object into a larger one is acceptable.

Now let's look at this one:

Derived2* d2p = bp;

This line will cause the program to not compile. Since the Base class is considered the "bigger" object, copying a bigger object into a "smaller" one will result in a failure to copy everything over, this is known as a Slicing Problem

We don't even have to look at the next line because we know that the program wil crash.

Example Question #1 : Compile Time Errors

Given:

    const int x = 10;

Which of the following will compile?

Possible Answers:

None of the above

All of the above

const int * r = &x

int * p = &x

int * const q = &x

Correct answer:

const int * r = &x

Explanation:

First we take a look at the given statement:

const int x = 10;

the const in front of "int" means that x will always hold the value of 10 and it will not change.

Let's observe all the choices.

 

int *p =&x

This line says to assign the address of x (in memory) to the pointer p. This however, will not compile because int * p is not marked as const. x is marked as a const so this forces int * p to be a const as well.

 

int * const q = &x

There is a const in this case but it is in the wrong place

 

const int * r = &x

The const is in the correct place and this is the correct answer

 

Example Question #2 : Compile Time Errors

class Base{

protected:

    void method();

};

 

class Derived : public Base{


};

 

int main(){

    Base b;

    b.method(); //Line A

    Derived d;

    d.method(); //Line B

}

 

Which of the following is true?

Possible Answers:

Line A will compile

Line B will compile

Line A will compile

Line B will not compile

Line A will not compile

Line B will compile

None of these

Line A will not compile

Line B will not compile

Correct answer:

Line A will not compile

Line B will not compile

Explanation:

To understand this question, we have to understand what protected method means. A protected method is a method that is accessible to methods inside it's own class as well as it's children. This means that a protected method can be called in the child class.

We can see that method() is called inside main. This should already raise a red flag. A protected class is being called outside of the child class so it will not compile. Even those it's being called on the Base and Derived objects, the calls are not made inside Base and Derived class so neither line will compile.

Learning Tools by Varsity Tutors