21. Consider the following class definitions. public class Data { private int x;
ID: 3718325 • Letter: 2
Question
21. Consider the following class definitions.
public class Data
{
private int x;
public void setX(int n)
{
x = n;
}
//... other methods not shown
}
public class EnhancedData extends Data
{
private int y;
public void setY(int n)
{
y = n;
}
//... other methods not shown
}
Assume that the following declaration appears in a client program.
EnhancedData item = new EnhancedData();
Which of the following statements would be valid?
I. item.y = 16;
II. item.setY(16);
III.item.setX(25);
Question 21 options:
II only
I only
II and III only
I, II and III
I and II only
22. Consider the following class definitions.
public class A
{
private int a1;
public void methodA()
{
methodB(); //Statement 1
}
}
public class B extends A
{
public void methodB()
{
methodA(); //Statement II
a1 = 0; //Statement III
}
}
Which of the labeled statements in the methods shown above will cause a compile-time error?
Question 22 options:
I only
III only
I and III
II and III
I and II
23. Assume that a, b, and c are variables of type int. Consider the following three conditions.
I. (a == b) && (a == c) && (b == c)
II. (a == b) || (a == c) || (b == c)
III. ((a - b) *(a - c) * (b - c) == 0
Assume that subraction and multiplication never overflow. Which of the conditions above is (are) always tuure if at least two of a, b, and c are equal?
Question 23 options:
I only
I and II
III only
II only
II and III
24. Consider the following two static methods, where f2 is intended to be the iterative version of f1.
public static int f1(int n)
{
if (n < 0)
{
return 0;
}
else
{
return (f1 (n -1) + n * 10);
}
}
public static int f2(int n)
{
int answer = 0;
while (n > 0)
{
answer = answer + n * 10;
n--;
}
return answer;
}
The method f2 will always produce the same results as f1 under which of the following conditions?
I. n < 0
II. n = 0
III. n > 0
Question 24 options:
II and III only
I, II and III
II only
III only
I only
25. Consider the following code segment.
int k = 1;
while (k < 20)
{
if ((k % 3) == 1)
System.out.println (k + " ");
k++;
}
What is printed as a result of executing this code segment?
Question 25 options:
2 4 6 8 10 12 14 16 18 20
1 3 5 7 9 11 13 15 17 19
3 6 9 12 15 18
1 4 7 10 13 16 19
2 5 8 11 14 17
II only
I only
II and III only
I, II and III
I and II only
Explanation / Answer
Q21. Ans: II and III only
Q22. Ans: III only, a1 is not visible in class B
Q23. Ans: II and III
Q24. Ans: I, II and III
Q25. Ans: 1 4 7 10 13 16 19
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.