Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Programming Concepts (COP 2510) Summer 2015

Date : June 24, 2015. Time: 3:30 PM ~ 4:45 PM


Full Marks 100

NAME : SOLUTIONS

UID:

Set A: Select the best possible answer.

20 X 4 = 80

1) What happens if you attempt to use a variable before it has been initialized?
A) A syntax error may be generated by the compiler
B) A runtime error may occur during execution
C) A "garbage" or "uninitialized" value will be used in the computation
D) A value of zero is used if a variable has not been initialized
E) Answers A and B are correct
Answer: E
Explanation: E) Many times the compiler is able to detect the attempted use of an uninitialized
variable. it will generate a syntax error in this case. If such as use escapes detection by the
compiler then a runtime error occurs upon usage. Java is a very "safe" language, so it does not
allow "garbage" or zero to be used if an uninitialized variable is used in a computation.
2) Say you write a program that makes use of the Random class, but you fail to include an import
statement for java.util.Random (or java.util.*). What will happen when you attempt to compile
and run your program.
A) The program won't run, but it will compile with a warning about the missing class.
B) The program won't compile-you'll receive a syntax error about the missing class.
C) The program will compile, but you'll receive a warning about the missing class.
D) The program will encounter a runtime error when it attempts to access any member of the
Random class
E) none of the above
Answer: B
Explanation: B) The missing class means that there will be undefined variables and/or methods.
The compiler will detect these and will issue error messages. Your program won't be executable.
3) Which of the following will yield a pseudorandom number in the range [ -5, +5 ) given the
following:
Random gen = new Random( );
A) gen.nextFloat( ) * 5
B) gen.nextFloat( ) * 10 - 5
C) gen.nextFloat( ) * 5 - 10
D) gen.nextInt( ) * 10 - 5
E) gen.nextInt(10) - 5
Answer: B

Explanation: B) nextFloat yields a pseudorandom number in the range [ 0, 1 ); multiplying by 10


yields numbers in the range [ 0, 10 ); subtracting 5 yields numbers in the range [ -5, 5 ).
4) Consider the following two lines of code. What can you say about s1 and s2?
String s1 = "testing" + "123";
String s2 = new String("testing 123");
A) s1 and s2 are both references to the same String object
B) the line declaring s2 is legal Java; the line declaring s1 will produce a syntax error
C) s1 and s2 are both references to different String objects
D) s1 and s2 will compare "equal"
E) none of the above
Answer: C
Explanation: C) Both declarations are legal Java. s1 is a String reference, and it is initialized to
the String "testing123". s2 is a String reference, and it is initialized to the String "testing 123".
Note the space between the "testing" and the "123". So the two Strings are not equal.
5) The relationship between a class and an object is best described as
A) classes are instances of objects
B) objects are instances of classes
C) objects and classes are the same thing
D) classes are programs while objects are variables
E) objects are the instance data of classes
Answer: B

6) The idea that program instructions execute in order (linearly) unless otherwise specified
through a conditional statement is known as
A) boolean execution
B) conditional statements
C) try and catch
D) sequentiality
E) flow of control
Answer: E
7) Of the following if statements, which one correctly executes three instructions if the condition
is true?
A) if (x < 0)
a = b * 2;
y = x;
z = a - y;

B)
{
if (x < 0)
a = b * 2;
y = x;
z = a - y;
}
C)
if { (x < 0)
a = b * 2;
y = x;
z=a-y;
}
D)
if (x < 0)
{
a = b * 2;
y = x;
z = a - y;
}
E) B, C and D are all correct, but not A
Answer: D
Explanation: D) In order to have three instructions execute in the if clause when the condition is
true, the three statements must be placed in a block following the condition. This is the syntax
used in D. In A, there is no block. In B, the block is placed around the entire if statement such
that the if clause is only a = b * 2; and the other two statements are not part of the if statement,
but follow it. The syntax in C is illegal resulting in a syntax error. Don't forget that the structure
of your code (how it lines up) is immaterial to the compiler.

8) Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x
is negative but leave x alone if x is 0?
A) if (x > 0) x++;
else x--;
B) if (x > 0) x++;
else if (x < 0) x--;
C) if (x > 0) x++;
if (x < 0) x--;
else x = 0;
D) if (x == 0) x = 0;
else x++;
x--;
E) x++;
x--;
Answer: B
Explanation: B) if x is positive, x++ is performed else if x is negative x-- is performed and

otherwise, nothing happens, or x is unaffected. In A, C, D and E, the logic is incorrect. In A, x-is done if x is not positive, thus if x is 0, x becomes -1 which is the wrong answer. In C, if x is
positive, then x++ is performed. In either case, the next statement is executed and if x is not
negative, the else clause is performed setting x to 0. So if x is positive, it becomes 0 after this set
of code. In D, x++ and x-- are both performed if x is not 0. And in E, this code does not attempt
to determine if x is positive or negative, it just adds one and then subtracts 1 from x leaving x the
same.
Given the nested if-else structure below, answer questions below.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
9) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
A) 0
B) 2
C) 3
D) 4
E) 5
Answer: C
Explanation: C) Since (a > 0) is true, the next condition is checked. Since (b < 0) is false, the
else clause for this condition is executed. Since (a > 5) is false the else clause for this condition
is executed, which is x = x + 3. Therefore, 3 is added to x, so it is now 3.

10) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
A) 0
B) 2
C) 3
D) 4
E) 5
Answer: C

11) Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending
on a student's test score.
if (score >= 90) grade = 'A';
if (score >= 80) grade = 'B';
if (score >= 70) grade = 'C';
if (score >= 60) grade = 'D';
else grade = 'F';
A) This code will work correctly in all cases
B) This code will work correctly only if grade >= 60
C) This code will work correctly only if grade < 60
D) This code will work correctly only if grade < 70
E) This code will not work correctly under any circumstances
Answer: D

Explanation: D) The problem with this code is that it consists of three if statements followed by
one if-else statement, rather than a nested if-else statement. So the logic is improper. If the
student's grade falls into the 'A' category, then all 4 conditions are true and the student winds up
with a 'D'. If the student's grade falls into the 'B' category, then the first condition is false, but the
next three are true and the student winds up with a 'D'. If the student's grade falls into the 'D'
category, then the only condition that is true is the one that tests for 'D' and so the grade is
assigned correctly. If the student's grade falls into the 'F' category, then none of the conditions
are true and an 'F' is assigned correctly. So, only if the grade < 70 does the code work correctly.

12) You might choose to use a switch statement instead of nested if-else statements if
A) the variable being tested might equal one of several hundred int values
B) the variable being tested might equal one of only a few int values
C) there are two or more int variables being tested, each of which could be one of several
hundred values
D) there are two or more int variables being tested, each of which could be one of only a few
values
E) none of the above, you would never choose to use a switch statement in place of nested if-else
statements under any circumstance
Answer: B
Explanation: B) The switch statement can only be used if there is a single variable being tested
and it is an integral type (an int or a char in Java). Further, because you have to enumerate each
possible value being tested, the switch statement only makes sense if the number of values being
tested is a small number.

13) If a switch statement is written that contains no break statements whatsoever,


A) this is a syntax error and an appropriate error message will be generated
B) each of the case clauses will be executed every time the switch statement is encountered
C) this is equivalent to having the switch statement always take the default clause, if one is
present
D) this is not an error, but nothing within the switch statement ever will be executed
E) none of the above
Answer: E
Explanation: E) Although writing such a switch statement is unusual, it is entirely legal. The
after the switch expression is evaluated. Following that, all subsequent clauses are executed, in
order, since there are no break statements to terminate the switch/case execution.

Given the following switch statement where x is an int, answer the questions below
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
14) If x is currently equal to 5, what will the value of x be after the switch statement executes?
A) 8
B) 6
C) 11
D) 5
E) 10
Answer: C
Explanation: C) Since there are no break statements, the flow falls through any cases following
the one that is true. The first case to be true is 5 since x is 5 prior to the switch statement. This
changes x to 8, then to 9, then to 11, then to 10, and then finally back to 11.
15) The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as
A) y = (x < 0) ? x : 0;
B) x = (x < 0) ? y : 0;
C) (x < 0) ? y = x : y = 0;
D) y = (x < 0);

E) y = if (x < 0) x : 0;
Answer: A

Explanation: A) The conditional operator of Java tests a condition, (x < 0) in this case, and if
true, returns the value after the ? (x in this case) and if false, returns the value after the : (0 in this
case). The original if statement is to assign y to x if (x < 0) and 0 otherwise. This will be
accomplished by assigning y to be x or 0 based on (x < 0), as shown in A. In B, x is assigned the
value of y or 0 which is backward. In C, the conditional operator is syntactically invalid. In D, y
would be set to either true or false depending on (x < 0), and the statement in E is syntactically
invalid.

16) Given the following code, where x = 0, what is the resulting value of x after the for-loop
terminates?
for (int i=0;i<5;i++)
x += i;
A) 0
B) 4
C) 5
D) 10
E) 15
Answer: D
Explanation: D) Since the condition is tested before the loop body executes, the loop will not
execute once x == 0. So, the loop iterates for x = 10, x = 9, ..., x = 1, or 10 times.
17) Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a
= 'a' and d = 'A', and examine the following conditions:
Condition 1: (x < y && x > 0)
Condition 2: (a != d || x != 5)
Condition 3: !(true && false)
Condition 4: (x > y || a == 'A' || d != 'A')
A) All 4 Conditions are true
B) Only Condition 2 is true
C) Condition 2 and Condition 4 are true only
D) Conditions 2, 3 and 4 are all true, Condition 1 is not
E) All 4 Conditions are false
Explanation: D) Condition 1 is not true because (x < y) is false, making the entire condition
false. Since (c != d) is true, Condition 2 is true even though (x != 5) is false. Condition 3 is true
because (true && false) is false, but !false is true. Condition 4 is true because (x > y) is true,
making the entire Condition true. Therefore, Conditions 2, 3 and 4 are true while Condition 1 is
false.

18) The do loop differs from the while loop in that


A) the while loop will always execute the body of the loop at least once
B) the do loop will always execute the body of the loop at least once
C) the do loop will continue to loop while condition in the while statement is false and the while
loop will continue to loop while the condition in the while statement is true
D) the while loop will continue to loop while condition in the while statement is false and the do
loop will continue to loop while the condition in the while statement is true
E) none of the above, there is absolutely no difference between the two types of loops
Answer: B
Explanation: B) Because the do loop does not test the condition until after the body of the loop
executes, the body will always execute at least one time, but the while loop tests the condition
first, and so if the condition is false the first time, the body does not execute even one time.

19) How many times will the following loop iterate?


int x = 10;
do {
System.out.println(x);
x--;
} while (x > 0);
A) 0 times
B) 1 time
C) 9 times
D) 10 times
E) 11 times
Answer: E
Explanation: E) The variable x starts at 10. Each pass through the loop, x is decremented and
the loop finally exits once x is not longer greater than 0, which in this case means once x
becomes 0. So the loop body executes for x = 10, x = 9, x = 8, and so forth down to x = 0. This
is 11 times.

20) The following nested loop structure will execute the inner most statement (x++) how many
times?
for (int j = 0; j < 100; j++)
for (int k = 100; k > 0; k--)

x++;
A) 100
B) 200
C) 10,000
D) 20,000
E) 1,000,000
Answer: C
Explanation: C) The outer loop iterates 100 times. Each time it iterates, the inner loop, and the
x++ statement, execute 100 times. Therefore, the statement x++ executes 100*100 = 10,000
times.
Set B: 20
2. Write a program to produce the following output exactly. For full marks, write the loops correctly
such that it produces exactly what is requested.
20
1*
2*
3*
4*
5*
6*

1
2
3
4
5
6

2
4
6
8
10
12

3
6
9
12
15
18

4
8
12
16
20
24

5
10
15
20
25
30

6
12
18
24
30
36

Solutions
1 public class multiplicationTable{
2
public static void main (String args[]) {
3
for (int i = 1; i <7; i++)
4
System.out.println(i + "*" + "\t " + i*1 + "\t " +
i*2 + "\t " + i*3 + "\t" + i*4 + "\t" + i*5 + "\t" + i*6);
5
6
}
7 }

You might also like