Which of the following is used to indicate a new line?
\n
What is output?
System.out.println("The answer is: " + 5 + 19);
The answer is: 519
What are if statements used for in programs?
Making decisions
Consider the following code, intended to search an array for a value and print the position where that value was found:
int [] array = / Assume array is correctly initialized /;
int num = / Input from the keyboard /;
int position = -1;
for (int i = 0; i < array.length; i++) {
if ( array[i] == num )
/ Missing Code /
}
if (position == -1)
System.out.println("Value not found");
else
System.out.println("Value found at position " + position);
What could replace / Missing Code / so that the code works as intended?
position = i;
Assuming that scan is a properly initialized Scanner variable, which of the following correctly inputs a String?
String val = scan.nextLine();
Which of the following is a legal variable name in Java?
ans
Consider the following code:
int c = 3 - 37 % 2;
System.out.println ( c );
What is the output?
2
Which if statement below tests if letter holds R? (letter is a char variable)
if (letter == 'R')
The following is intended to count the number of times the number 87 is found in an array of test scores:
int [] d = / Assume array is initialized /;
int scoreCount = 0;
for (int i = 0; i < d.length; i++) {
if (/ Missing Code /)
scoreCount++;
}
System.out.println("Number of 87's: " + scoreCount);
Which of the following could replace / Missing Code / so that the code works as intended?
d[i] == 87
Which character below is not allowed in a variable name?
&
Which of the following is NOT a primitive data type?
String
Consider the following code:
double x = -97.6;
System.out.println(Math.abs(x));
What is output?
97.6
Which if statement below tests if the variable letter holds the char value w?
if ( letter == 'w')
Consider the following code, intended to count the number of words in the array with a length less than or equal to 6.
String vocabulary [] = / Array initialized with Strings /;
int c = 0;
for (int i = 0; i < vocabulary.length; i++) {
if ( / Missing Code /)
c++;
}
System.out.println("Number of words with length less than or equal to 6: " + c);
What could be used to replace / Missing Code / so that the code works as intended?
vocabulary[i].length() <= 6
Which of the following would properly create A and B as integer variables?
int A, B;
Which of the following variable assignments is legal in Java?
double n = 3;
What is output by the following code?
int x = 36 % 8;
if (x >= 10)
System.out.println(1);
else if (x >= 8)
System.out.println(2);
else if (x >= 6)
System.out.println(3);
else if ( x >= 4)
System.out.println(4);
else
System.out.println(5);
4
What does short circuit evaluation mean in the following code?
if ( a < b || c != d )
if a < b is true it doesn't evaluate c != d
The following is intended to return the location of the first instance of the String the user enters from the keyboard, -1 if not found.
String names [] = new String[20];
//assume array is initialized
System.out.println("Enter a name to search for: ");
String lookingFor = scan.nextLine();
int found = -1;
for (int i = 0; i < names.length; i++) {
if ( / Missing Code / ) {
found = i;
break;
}
}
Which of the following could replace / Missing Code / so that it works as intended?
lookingFor.equals(names[i])
What is the result of compiling and running the following code?
int s = Math.sqrt(26);
Error: possible loss of precision
Which of the following correctly stores the word umbrella in a variable called stuff?
String stuff = "umbrella";
Consider the following code:
String words [] = {"avalanche", "budget", "cannot", "center", "meaning", "clear", "furniture", "deep", "piccolo", "friendly", "potatoes"};
int c = 0;
for(int i = 0; i < words.length; i++) {
if (words[i].substring(0,3).indexOf('o') >= 0)
c++;
}
System.out.println( c );
What is output?
1
The following if statement tests the rainfall in New York's Central Park during the months of June, July and August.
if (low <= rain && rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
It could be replaced with:
II only
if (rain >= low) {
if (rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
} else
System.out.println("Rainfall amount is abnormal.");
Consider the following method checking whether a student has passing grades:
public boolean isPassing(int finalExam, int cumulativeAverage) {
/ Missing Code /
}
A student can pass a class if one of the following is true:
The final exam is at least 98
The cumulative average is over 60
Which of the following correctly replaces / Missing Code / so that the method works as intended?
I and II only
I.
if ((finalExam >= 98) || (cumulativeAverage > 60))
return true;
return false;
II.
boolean pass = false;
if (finalExam >= 98)
pass = true;
if (cumulativeAverage > 60)
pass = true;
return pass;
Consider the following code:
double list [] = new double [50];
The index of the first value is _______ and the last index is ______.
0, 49