Computer Science : Program Analysis

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #11 : Program Analysis

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 compiles and runs to completion without printing anything

The program does not compile.

The program compiles and runs, printing "method2"

The program compiles and crashes when it runs.

The program compiles and runs, printing "method1"

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 : Debugging

Given:

    const int x = 10;

Which of the following will compile?

Possible Answers:

const int * r = &x

None of the above

int * p = &x

All of the above

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 #11 : Program Analysis

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 not compile

Line B will not compile

None of these

Line A will not compile

Line B will compile

Line A will compile

Line B will not compile

Line A will compile

Line B will 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.

Example Question #1 : Debugging

Consider the following code:

Object[] objects = new Object[20];

for(int i = 0; i < objects.length; i++) {

     switch(i % 4) {

     case 0:

          objects[i] = new Integer(i + 3);

          break;

     case 1:

          objects[i] = "This val: " + i;

          break;

     case 2:

          objects[i] = new Double(i * 4.4);

          break;

     case 3:

          objects[i] = "That val: " + (i*12);

          break;

     }

}

String s = (String)objects[8];

System.out.println(s);

What is the error in the code above?

Possible Answers:

There is an array overrun.

There will be a ClassCastException thrown.

There are no errors.

You cannot assign various types to the array in that manner.

There will be a NullPointerException.

Correct answer:

There will be a ClassCastException thrown.

Explanation:

In order to understand the error in this code, you must understand what the loop is doing. It is assigning variable types to the array of Object objects based upon the remainder of dividing the loop control variable i by 4. You thus get a repeating pattern:

Integer, String, Double, String, Integer, String, Double, String, . . .

Now, index 8 will be an Integer (for it has a remainder of 0). This means that when you do the type cast to a String, you will receive a TypeCastException for the line reading:

String s = (String)objects[8];

Example Question #11 : Debugging

public static int[][] doWork(int[][] a, int[][] b) {

       int[][] ret = new int[a.length][a[0].length];

       for(int i = 0; i < a.length; i++) {

              for(int j = 0; j < a[i].length; j++) {

                     ret[i][j] = a[i][j] + b[i][j];

              }

       }

       return ret;

}

In the code above, what is the potential error that will not be caught on the following line?

ret[i][j] = a[i][j] + b[i][j];

Possible Answers:

The array could go out of bounds

The arrays may not be initialized

The code is fine as it is written

The array a may be set to null

The arrays may contain null values

Correct answer:

The array could go out of bounds

Explanation:

At this point of the code, it is not possible for a or b to be null. Furthermore, these arrays cannot contain null values, for they are made up of primitive types (int types). These cannot be set to null, as they are not objects. The potential error here is a little bit abstruse, but it is important to note. There is one possible error, namely that the 2D arrays are "ragged"; however we don't need to worry about this for the exam. Still, it is also possible that the array b (presuming that it is not null) is not the same size as the array a. Since we are using a to set our loop counts, this could potentially mean that we overrun the array b by using index values that are too large. (Note, it is also possible that b could be null, causing an error; however, there are no answer choices for that possibility.)

Example Question #1 : Debugging

Consider the following code:

Object[] objects = new Object[20];

for(int i = 0; i < objects.length; i++) {

     switch(i % 4) {

     case 0:

          objects[i] = new Integer(i + 3);

          break;

     case 1:

          objects[i] = "This val: " + i;

          break;

     case 2:

          objects[i] = new Double(i * 4.4);

          break;

     case 3:

          objects[i] = "That val: " + (i*12);

          break;

     }

}

String s = (String)objects[8];

System.out.println(s);

What is the error in the code above?

Possible Answers:

There is an array overrun.

There will be a ClassCastException thrown.

There are no errors.

You cannot assign various types to the array in that manner.

There will be a NullPointerException.

Correct answer:

There will be a ClassCastException thrown.

Explanation:

In order to understand the error in this code, you must understand what the loop is doing. It is assigning variable types to the array of Object objects based upon the remainder of dividing the loop control variable i by 4. You thus get a repeating pattern:

Integer, String, Double, String, Integer, String, Double, String, . . .

Now, index 8 will be an Integer (for it has a remainder of 0). This means that when you do the type cast to a String, you will receive a TypeCastException for the line reading:

String s = (String)objects[8];

Example Question #11 : Debugging

public static int[][] doWork(int[][] a, int[][] b) {

       int[][] ret = new int[a.length][a[0].length];

       for(int i = 0; i < a.length; i++) {

              for(int j = 0; j < a[i].length; j++) {

                     ret[i][j] = a[i][j] + b[i][j];

              }

       }

       return ret;

}

In the code above, what is the potential error that will not be caught on the following line?

ret[i][j] = a[i][j] + b[i][j];

Possible Answers:

The array could go out of bounds

The arrays may not be initialized

The code is fine as it is written

The array a may be set to null

The arrays may contain null values

Correct answer:

The array could go out of bounds

Explanation:

At this point of the code, it is not possible for a or b to be null. Furthermore, these arrays cannot contain null values, for they are made up of primitive types (int types). These cannot be set to null, as they are not objects. The potential error here is a little bit abstruse, but it is important to note. There is one possible error, namely that the 2D arrays are "ragged"; however we don't need to worry about this for the exam. Still, it is also possible that the array b (presuming that it is not null) is not the same size as the array a. Since we are using a to set our loop counts, this could potentially mean that we overrun the array b by using index values that are too large. (Note, it is also possible that b could be null, causing an error; however, there are no answer choices for that possibility.)

Example Question #1 : Logic Errors

Consider the following code:

public static int[] del(int[] a,int delIndex) {

     if(delIndex < 0 || delIndex >= a.length) {

          return null;

     }

     int[] ret = new int[a.length - 1];

     for(int i = 0; i < a.length; i++) {

          if(i != delIndex) {

               ret[i] = a[i];

          }

     }

     return ret;

}

What is the error in the code above?

Possible Answers:

There is a null pointer exception.

You need to implement a swap for the values.

The use of the array index i is incorrect.

The loop is infinite.

The intial conditional (i.e. the if statement) has incorrect logic.

Correct answer:

The use of the array index i is incorrect.

Explanation:

The problematic line in the code above is the one that reads:

ret[i] = a[i];

This is going to work well until the end of the array.  The variable i is going to go for the length of the array a.  However, the array ret is one less in length.  This means that you will overrun your array, getting an ArrayIndexOutOfBoundsException at the very end of the looping.  You would need to implement two indices—one of which will not be incremented on that one time when you skip the element that is removed from the array.

Example Question #11 : Program Analysis

What is wrong with this loop?

int i = 0;

int count = 20;

int[] arr = [1, 3, 4, 6, 8, 768, 78, 9]

while (i < count) {

  if (i > 0) {

    arr[i] = count;

  }

  if (i < 15) {

      System.out.println("HEY");

  }

}

Possible Answers:

The loop is infinite

it prints Hey too many times

count never gets decremented

if statement is incorrect

Correct answer:

The loop is infinite

Explanation:

The loop is infinite because i never gets incremented to become greater than count. 

The loop in this case is,

while (i < count) {

  if (i > 0) {

    arr[i] = count;

  }

and the error occurs because there is no terminating factor in the while loop. A terminating factor would be,

while(i<count){

   if(i>0, i<10, i++){

    arr[i] = count;

  }

 

Example Question #1 : Identifying & Correcting Errors

Consider the following code:

int num = 1;

for(int i = 0; i < 10; i++) {

     num *= i;

}

The code above is intended to provide the continuous product from num to 10.  (That is, it provides the factorial value of 10!.)  What is the error in the code?

Possible Answers:

Both the i intialization and the loop control value are incorrect.

You cannot use *= in a loop.

The i initialization is wrong.

The loop control, the initialization, and the initialization for num are all wrong.

The loop control is incorrect.

Correct answer:

Both the i intialization and the loop control value are incorrect.

Explanation:

This loop will indeed run 10 times.  However, notice that it begins on 0 and ends on 10.  Thus, it will begin by executing:

num *= 0

Which is the same as:

num = num * 0 = 0

Thus, you need to start on 1 for i.  However, notice also that you need to go from 1 to 10 inclusive.  Therefore, you need to change i < 10 to i <= 10.

 

Learning Tools by Varsity Tutors