AP Computer Science A : Run Time Exceptions

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 : Run Time Exceptions

BITWISE XOR OPERATION

Given the following binary values

a = 0100 1110

b = 0011 0101

perform an XOR operation (c = a^b). What is the result?

Possible Answers:

c = 0111 1111

c = 0000 0100

c = 0111 1011

c = 0111 0011

Correct answer:

c = 0111 1011

Explanation:

Performing a bitwise excludive OR constitutes in taking both binary values and evaluating as follows: either one or the other (1) never both (0). This is the truth table for a bitwise XOR operation:

 Blank flowchart   new page  2

Taking both a and b and performing the operation bit by bit we get the following result:

 Result

Example Question #2 : Run Time Exceptions

True or False.

 

There is a runtime exception in this code snippet. 

 

int wait_time = 0;

int wait_time = 5;

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

System.out.println(wait_time);

}

Possible Answers:

True

False

Correct answer:

True

Explanation:

Yes, there is a runtime exception in the code snippet. The int wait_time is defined twice which will give a runtime exception. This can be fixed by not declaring int before the second assignment of the variable wait_time. 

Learning Tools by Varsity Tutors