Which of the following statements stores the value 3 in x ?
Responses:
A) int x = 4 / 7;
B) int x = 7 / 3;
C) int x = 7 / 4;
D) int x = 5 % 8;
E) int x = 8 % 5;
E) int x = 8 % 5;
Consider the following code segment.
double a = 1.1;
double b = 1.2;
if ((a + b) * (a - b) != (a * a) - (b * b))
{
System.out.println("Mathematical error!");
}
Which of the following best describes why the phrase "Mathematical error!" would be printed?
(Remember that mathematically (a + b) * (a - b) = a2 - b2 .)
Responses
A) Precedence rules make the if condition true.
B) Associativity rules make the if condition true.
C) Roundoff error makes the if condition true.
D) Overflow makes the if condition true.
E) A compiler bug or hardware error has occurred.
C) Roundoff error makes the if condition true.
Consider the following code segment.
String str = "0";
str += str + 0 + 8;
System.out.println(str);
What is printed as a result of executing the code segment?
Responses
A) 8
B) 08
C) 008
D) 0008
E) Nothing is printed, because numerical values cannot be added to a String object.
D) 0008
Consider the following method, between, which is intended to return true if x is between lower and upper, inclusive, and false otherwise.
// precondition: lower <= upper
// postcondition: returns true if x is between lower and upper,
// inclusive; otherwise, returns false
public boolean between(int x, int lower, int upper)
{
/* missing code */
}
Which of the following can be used to replace /* missing code */ so that between will work as intended?
Responses
A) return (x <= lower) && (x >= upper);
B) return (x <= lower) || (x >= upper);
C) return lower <= x <= upper;
D) return (x >= lower) && (x <= upper);
E) return (x >= lower) || (x <= upper);
D) return (x >= lower) && (x <= upper);
Consider the following code segment, which is intended to print the sum of all the odd integers from 0 up to and including 101.
int r = 0;
int sum = 0;
/* missing loop header */
{
if (r % 2 == 1)
{
sum += r;
}
r++;
}
System.out.println(sum);
Which of the following could replace /* missing loop header */ to ensure that the code segment will work as intended?
Responses
A) while (r <= 100)
B) while (sum <= 100)
C) while (r < 101)
D) while (r <= 101)
E) while (sum <= 101)
D) while (r <= 101)
Consider the following method. Method allEven is intended to return true if all elements in array arr are even numbers; otherwise, it should return false.
public boolean allEven(int[] arr)
{
boolean isEven = /* expression */ ;
for (int k = 0; k < arr.length; k++)
{
/* loop body */
}
return isEven;
}
Which of the following replacements for /* expression */ and /* loop body */ should be used so that method allEven will work as intended?
Responses
A)
/* expression *//* loop body */false
if ((arr[k] % 2) == 0)
isEven = true;
B) /* expression *//* loop body */false
if ((arr[k] % 2) != 0)
isEven = false;
else
isEven = true;
C) /* expression *//* loop body */true
if ((arr[k] % 2) != 0)
isEven = false;
D) /* expression *//* loop body */true
if ((arr[k] % 2) != 0)
isEven = false;
else
isEven = true;
E) /* expression *//* loop body */true
if ((arr[k] % 2) == 0)
isEven = false;
else
isEven = true;
C
Consider the following code segment.
int x = 5;
x += 6 * 2;
x -= 3 / 2;
What value is stored in x after the code segment executes?
Responses
A) -1.5
B) 1
C) 9
D) 15.5
E) 16
E) 16
Consider the following method, biggest, which is intended to return the greatest of three integers. It does not always work as intended.
Question
Which of the following best describes the error in the method?
Responses
A) biggest always returns the value of a.
B) biggest may not work correctly when c has the greatest value.
C) biggest may not work correctly when a and b have equal values.
D) biggest may not work correctly when a and c have equal values.
E) biggest may not work correctly when b and c have equal values.
C) biggest may not work correctly when a and b have equal values.
Consider the following code segment.
String oldStr = "ABCDEF";
String newStr = oldStr.substring(1, 3) + oldStr.substring(4);
System.out.println(newStr);
What is printed as a result of executing the code segment?
Responses
A) ABCD
B) BCDE
C) BCEF
D) BCDEF
E) ABCDEF
C) BCEF
Consider the following class definitions.
public class Person
{
private String name;
public String getName()
{ return name; }
}
public class Book
{
private String author;
private String title;
private Person borrower;
public Book(String a, String t)
{
author = a;
title = t;
borrower = null;
}
public void printDetails()
{
System.out.print("Author: " + author + " Title: " + title);
if ( /* missing condition */ )
{
System.out.println(" Borrower: " + borrower.getName());
}
}
public void setBorrower(Person b)
{ borrower = b; }
}
Question
Which of the following can replace /* missing condition */ so that the printDetails method CANNOT cause a run-time error?
Responses
A) I only
B) II only
C) III only
D) I and II
E) II and III
B) II only
Consider the following code segment.
int j = 1;
while (j < 5)
{
int k = 1;
while (k < 5)
{
System.out.println(k);
k++;
}
j++;
}
Which of the following best explains the effect, if any, of changing the first line of code to int j = 0; ?
Responses
A) There will be one more value printed because the outer loop will iterate one additional time.
B) There will be four more values printed because the outer loop will iterate one additional time.
C) There will be one less value printed because the outer loop will iterate one fewer time.
D) There will be four fewer values printed because the outer loop will iterate one fewer time.
E) There will be no change to the output of the code segment.
B) There will be four more values printed because the outer loop will iterate one additional time.
n the following code segment, assume that the string str has been properly declared and initialized. The code segment is intended to print the number of strings in the array animals that have str as a substring.
String[] animals = {"horse", "cow", "goat", "dog", "cat", "mouse"};
int count = 0;
for (int i = 0; i <= animals.length; i++)
{
if (animals[i].indexOf(str) >= 0)
{
count++;
}
}
System.out.println(count);
The code segment does not work as intended. Which of the following changes should be made so the code segment works as intended?
Responses
A) The Boolean expression in the for loop header should be changed to i < animals.length.
B) The Boolean expression in the for loop header should be changed to i < animals.length - 1.
C) The Boolean expression in the for loop header should be changed to i < animals[i].length.
D) The condition in the if statement should be changed to animals[i].equals(str).
E) The condition in the if statement should be changed to animals[i].substring(str).
Which of the following expressions evaluate to 7 ?
II and III
Consider the following code segment.
System.out.print(I do not fear computers. ); // Line 1
System.out.println(I fear the lack of them.); // Line 2
System.out.println(--Isaac Asimov); // Line 3
The code segment is intended to produce the following output but may not work as intended.
I do not fear computers. I fear the lack of them.
--Isaac Asimov
Which change, if any, can be made so that the code segment produces the intended output?
In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.
Consider the following code segment.
String temp = "comp";
System.out.print(temp.substring(0) + " " +
temp.substring(1) + " " +
temp.substring(2) + " " +
temp.substring(3));
What is printed when the code segment is executed?
comp omp mp p
Consider the following method.
public void conditionalTest(int a, int b)
{
if ((a > 0) && (b > 0))
{
if (a > b)
System.out.println("A");
else
System.out.println("B");
}
else if ((b < 0) || (a < 0))
System.out.println("C");
else
System.out.println("D");
}
What is printed as a result of the call conditionalTest(3, -2)?
C
Consider the following method.
Assume that doSome is called and executes without error. Which of the following are possible combinations for the value of lim, the number of times Statement S is executed, and the number of times Statement T is executed?
II only
Consider the following two methods that appear within a single class.
public void changeIt(int[] list, int num)
{
list = new int[5];
num = 0;
for (int x = 0; x < list.length; x++)
list[x] = 0;
}
public void start()
{
int[] nums = {1, 2, 3, 4, 5};
int value = 6;
changeIt(nums, value);
for (int k = 0; k < nums.length; k++)
System.out.print(nums[k] + " ");
System.out.print(value);
}
What is printed as a result of the call start()?
1 2 3 4 5 6
Consider the following code segment.
int a = 4;
int b = 5;
a++;
b++;
int c = a + b;
a -= 1;
System.out.println(a + c);
What is printed when the code segment is executed?
15
Consider the following method, which is intended to return true if at least one of the three strings s1, s2, or s3 contains the substring "art". Otherwise, the method should return false.
Question
Which of the following method calls demonstrates that the method does not work as intended?Responses
A) containsArt ("rattrap", "similar", "today")
B) containsArt ("start", "article", "Bart")
C) containsArt ("harm", "chortle", "crowbar")
D) containsArt ("matriculate", "carat", "arbitrary")
E) containsArt ("darkroom", "cartoon", "articulate")
A) containsArt ("rattrap", "similar", "today")
Consider the following class declaration
public class SomeClass
{
private int num;
public SomeClass(int n)
{
num = n;
}
public void increment(int more)
{
num = num + more;
}
public int getNum()
{
return num;
}
}
The following code segment appears in another class.
SomeClass one = new SomeClass(100);
SomeClass two = new SomeClass(100);
SomeClass three = one;
one.increment(200);
System.out.println(one.getNum() + " " + two.getNum() + " " +
three.getNum());
What is printed as a result of executing the code segment?
300 100 300
At a certain high school students receive letter grades based on the following scale.
Which of the following code segments will assign the correct string to grade
for a given integer score ?
I and III
Which of the following code segments will print all multiples of 5 that are greater than 0 and less than 100 ?
I. for (int k = 1; k < 100; k++){ if (k % 5 == 0) { System.out.print(k + " "); }}
II. for (int k = 1; k < 100; k++){ if (k / 5 == 0) { System.out.print(k + " "); }}
III. int k = 5;while (k < 100){ System.out.print(k + " "); k = k + 5;}
I and III
Consider the following code segment from an insertion sort program.
Question
Assume that array arr has been defined and initialized with the values {5, 4, 3, 2, 1}. What are the values in array arr after two passes of the for loop (i.e., when j = 2 at the point indicated by / * end of for loop * / ) ?
{3, 4, 5, 2, 1}
Consider the following code segment.
double firstDouble = 2.5;
int firstInt = 30;
int secondInt = 5;
double secondDouble = firstInt - secondInt / firstDouble + 2.5;
What value will be assigned to secondDouble when the code segment is executed?
30.5
Consider the following code segment.
The following conditions have been proposed to replace /* condition */ in the code segment.
x < 0
x <= 1
x < 10
Question
For which of the conditions will nothing be printed?
I, II, and III
Consider the following class declaration.
public class Sample
{
private int a;
private double b;
public Sample(int x, double y)
{
a = x;
b = y;
}
// No other constructors
}
The following method appears in a class other than Sample.
public static void test()
{
Sample object = new /* missing constructor call */ ;
}
Which of the following could be used to replace /* missing constructor call */ so that the method will compile without error?
Sample(10, 6.2)
Consider the following method that is intended to determine if the double values d1 and d2 are close enough to be considered equal. For example, given a tolerance of 0.001, the values 54.32271 and 54.32294 would be considered equal.
Question
What should replace / * missing code * / so that almostEqual will work as intended?
return Math.abs(d1 - d2) <= tolerance;
Consider the following recursive method.
public static void stars(int num)
{
if (num == 1)
{
return;
}
stars(num - 1);
for (int i = 0; i < num; i++)
{
System.out.print("*");
}
System.out.println();
}
Question
What is printed as a result of the method call stars(5) ?
**
***
****
*****
Consider the following data field and method.
private int[] seq;
// precondition: seq.length > 0
public int lenIncreasing()
{
int k = 1;
while ((k < seq.length) && (seq[k - 1] < seq[k]))
{
k++;
}
// assertion
return k;
}
Which of the following assertions is true when execution reaches the line // assertion in lenIncreasing?
Responses
I. (k == seq.length) && (seq[k - 1] >= seq[k])
II. (k == seq.length) || (seq[k - 1] >= seq[k])
III. (k < seq.length) && (seq[k - 1] < seq[k])
IV. (k < seq.length) || (seq[k - 1] < seq[k])
V. (k == seq.length) || (seq[k - 1] == seq[k])
II