1.
Consider the following declaration:
final double FEE = 0.50;
The value of the variable FEE cannot subsequently be altered in the program, and any attempt to do so will be caught by the compiler.
Correct Answer
A. True
Explanation
The given declaration states that the variable FEE is declared as final, which means its value cannot be changed once it is assigned. This is indicated by the keyword "final" before the data type. Since the value of FEE is set to 0.50, it cannot be altered later in the program. If there is any attempt to modify the value of FEE, the compiler will catch it and throw an error. Therefore, the statement "The value of the variable FEE cannot subsequently be altered in the program, and any attempt to do so will be caught by the compiler" is true.
2.
The following lines of code will swap the contents of the integer variables x and y:
int z;
x=y;
y=z;
z=x;
Correct Answer
B. False
Explanation
The given code will not swap the contents of the integer variables x and y. This is because the variable z is not assigned any value before it is assigned to x. Therefore, when z is assigned to y, and then x is assigned to z, the original values of x and y will not be preserved and the swap will not occur.
3.
The expression !(a||b) is equivalent to (!a)&&(!b)
Correct Answer
A. True
Explanation
The expression !(a||b) represents the negation of the logical OR operation between variables a and b. This means that the expression will be true only if both a and b are false. On the other hand, the expression (!a)&&(!b) represents the logical AND operation between the negations of variables a and b. This expression will also be true only if both a and b are false. Since both expressions have the same conditions for being true, they are equivalent. Therefore, the answer is true.
4.
When Java evaluates the boolean expression
(num >= 0 && num <= 100)
the final value of the expression is determined only after both sub-expressions (num >= 0, num <= 100) are evaluated.
Correct Answer
B. False
Explanation
The given statement is false. In Java, when evaluating a boolean expression with the "&&" operator, the second sub-expression is only evaluated if the first sub-expression is true. This is known as short-circuit evaluation. Therefore, if the first sub-expression (num >= 0) is false, the second sub-expression (num
5.
String word = “computer”;
System.out.println(word.substring(3,6));
The output of the code above would be: mpu
Correct Answer
B. False
Explanation
The output of the code above would be "put". The substring method in Java returns a new string that is a substring of the original string. In this case, the substring starts at index 3 (inclusive) and ends at index 6 (exclusive), so it includes the characters at indices 3, 4, and 5, which are "p", "u", and "t" respectively. Therefore, the correct answer is False.
6.
After the following code is executed, the value of x is 4
int x = 7;
x += 4;
Correct Answer
B. False
Explanation
The value is actually 11
7.
The ________ of a variable is that section of the program in which the variable exists.
Correct Answer
C. Scope
Explanation
The scope of a variable refers to the section of the program in which the variable exists. It determines the visibility and accessibility of the variable within the program. The scope defines where the variable can be used and accessed, and it helps in avoiding conflicts and maintaining the integrity of the program.
8.
A(n) ________ is a class method that is automatically called whenever an object of that class is created.
Correct Answer
D. Constructor
Explanation
A constructor is a class method that is automatically called whenever an object of that class is created. It is responsible for initializing the object's state and allocating memory for it. The constructor typically takes parameters to set initial values for the object's attributes. By calling the constructor, the object is created and ready for use.
9.
Consider the following code segment:
String S = “ILoveJava”;
System.out.print(S.substring(1,5));
What is output when this code segment is executed?
Correct Answer
D. Love
Explanation
The code segment uses the substring() method to extract a portion of the string S. The method takes two parameters, the starting index (inclusive) and the ending index (exclusive). In this case, the starting index is 1 and the ending index is 5. Therefore, the substring that is extracted from S is "Love". The print statement then outputs this substring.
10.
Assume x and y are String variables with x = “Smile” and y = null. The result of (x == y) is
Correct Answer
B. False
Explanation
The result of (x == y) is false because the == operator checks for reference equality. In this case, x and y are two different objects in memory. x is referencing the string "Smile" while y is referencing null. Since they are not referencing the same object, the comparison evaluates to false.
11.
Assume x and y are String variables with x = “Smile” and y = null. The result of x.length( )+ y.length( ) is
Correct Answer
E. Exception
Explanation
The given code will throw an exception because we are trying to access the length of a null object (y). Since y is null, it does not have a length property, resulting in a NullPointerException.
12.
The following method determines whether any character occurs more than once in the given String. However, the method has a bug.
boolean ContainsDouble(String S ) {
int n;
for ( n=1; n < S.length( ); n++ ) {
if (S.charAt(0) == S.charAt(n))
return true;
}
return false;
}
For which of the following String parameters would method ContainsDouble correctly return the value true?
I. “dttd” II. “xxxx” III. “look”
Correct Answer
C. I and II
Explanation
The given method checks if the first character of the string is equal to any other character in the string. If it is, then it returns true. If not, it continues checking until the end of the string and then returns false.
For the first string "dttd", the first character 'd' occurs again at index 3, so the method correctly returns true.
For the second string "xxxx", the first character 'x' occurs again at index 1, so the method correctly returns true.
For the third string "look", the first character 'l' does not occur again in the string, so the method incorrectly returns false.
Therefore, the method correctly returns true for the strings I and II.
13.
Assume that x and y are int variables with x = 8, y = 3, and a and d are char variables with a =‘c’ and d =‘D’, and examine the following conditions:
Condition 1: !(true && false)
Condition 2: (a != d || x != 8)
Condition 3: (x < y && x > 0)
Condition 4: (x > y || a == ‘D’ || d != ‘D’)
Correct Answer
D. Only condition 3 is false
Explanation
Condition 3 states that x is less than y and greater than 0. Since x is 8 and y is 3, this condition is false because 8 is not less than 3. Therefore, the correct answer is that only condition 3 is false.
14.
Consider the following method:
int boo (int x, int y) {
x -= 2; ++y;
return x * y;
}
Consider the following code:
int x = 7, y = -3, z;
z = x + y + boo ( y, x );
What is z?
Correct Answer
A. -36
Explanation
The method "boo" takes two integers, subtracts 2 from the first integer (x) and increments the second integer (y) by 1. It then returns the product of the updated x and y values. In the given code, x is 7 and y is -3.
First, y is added to x, resulting in 7 + (-3) = 4.
Then, the method "boo" is called with y as the first argument and x as the second argument.
Inside the "boo" method, x becomes -5 (subtracting 2 from y) and y becomes 0 (incrementing y by 1).
The return value of the "boo" method is -5 * 0 = 0.
Finally, 4 + 0 = 4, so the value of z is 4.
15.
Consider the following code:
int b = 6;
int c = 11;
int a = b * (-c + 2) / 7;
The value stored in a is:
Correct Answer
C. -7
Explanation
The given code calculates the value of variable "a" using the expression "b * (-c + 2) / 7". Here, b is 6 and c is 11. So, substituting the values, the expression becomes 6 * (-11 + 2) / 7. Simplifying further, we have 6 * (-9) / 7, which is -54 / 7. Dividing -54 by 7 gives us approximately -7.71428. Therefore, the value stored in variable "a" is -7.71428.
16.
Consider the following swap method.
public void sillyStrings(String a, String b) {
a = a + “One”;
b = b + a;
}
If String x = “Hello” and String y = “Goodbye”, then sillyStrings(x, y); results in which of the following?
Correct Answer
C. X and y remain unchanged
Explanation
The swap method does not modify the original strings x and y because strings are immutable in Java. Therefore, any modifications made inside the method do not affect the original variables. As a result, x and y remain unchanged after calling the sillyStrings method.
17.
Consider a Point class with the following constructors and methods
instance data
description
private int myX
the current x coordinate
private int myY
the current y coordinate
Methods
description
Point ()
Default constructor:
Initialize the point to (0,0)
Point(int x, int y)
Another constructor:
Initialize the point to (x,y)
void SetX(int x)
Set the x coordinate to the given value
void SetY (int y)
Set the y coordinate to the given value
int GetX()
return the x coordinate
int GetY()
return the y coordinate
Which of the following code segments correctly defines a Point variable that represents the point 3,5?
Segment I
Point P();
P.myX = 3;
P.myY = 5;
Segment II
Point P();
P.SetX(3);
P.SetY(5);
Segment III
Point P(3,5);
Correct Answer
E. II and III
Explanation
Segment II and III correctly define a Point variable that represents the point (3,5). In Segment II, a Point object is created using the constructor Point(int x, int y), and then the SetX and SetY methods are used to set the x and y coordinates to 3 and 5 respectively. In Segment III, a Point object is directly created using the constructor Point(int x, int y) with the values 3 and 5 passed as arguments.
18.
Consider a Point class with the following constructors and methods
instance data
description
private int myX
the current x coordinate
private int myY
the current y coordinate
Methods
description
Point ()
Default constructor:
Initialize the point to (0,0)
Point(int x, int y)
Another constructor:
Initialize the point to (x,y)
void SetX(int x)
Set the x coordinate to the given value
void SetY (int y)
Set the y coordinate to the given value
int GetX()
return the x coordinate
int GetY()
return the y coordinate
Assume that P is a Point object that represents the point x,y. Which code segment correctly changes P to represent y,x?
A)
P.SetX (P.GetY( ));
P.SetY (P.GetX( ));
B)
P.GetX( ) = P.GetY( );
P.GetY( ) = P.GetX( );
C
int tmp = P.myX;
P.myX = P.myY;
P.myY = tmp;
D)
int tmp = P.GetX( );
P.SetX(P.GetY( ) );
P.SetY(tmp);
Correct Answer
C. C
Explanation
In option C, the code segment correctly changes P to represent y,x. It creates a temporary variable tmp and assigns it the value of P.myX. Then, it assigns P.myY to P.myX and finally assigns tmp to P.myY. This effectively swaps the values of P.myX and P.myY, resulting in P representing y,x.
19.
For questions 16 and 17, use the following class definition:
public class StaticExample {
private static int x;
public StaticExample (int y) {
x = y;
}
public int incr( ) {
x++;
return x;
}
}
16) What is the value of z after the third statement executes below?
StaticExample a = new StaticExample(12);
StaticExample b = new StaticExample(5);
int z = a.incr( );
A) 5
B) 6
C) 12
D) 13
E) The code is syntactically invalid
Correct Answer
C. 12
Explanation
An instance of StaticExample is created with a and initialized with a value of 12. At this point, the static variable x is set to 12.
Another instance b is created and initialized with a value of 5. The static variable x is updated to 5, which affects all instances.
The incr() method is called on instance a (which has x set to 5 now), incrementing x by 1. Therefore, the value of z after the third statement is executed is 6.
20.
Consider the following class
public class StaticExample {
private static int x;
public StaticExample (int y) {
x = y;
}
public int incr( ) {
x++;
return x;
}
}
If there are 4 objects of type StaticExample, how many different instances of x are there?
Correct Answer
D. 0
Explanation
The variable "x" in the class StaticExample is declared as a static variable. Static variables are shared among all instances of a class. Therefore, regardless of the number of objects created (in this case, 4 objects), there will only be one instance of the static variable "x". So, the answer is 0.