Practice Test – Fill in the blanks with Code
1. The following Java code that uses a for statement to sum the numbers from 1 through 50 and display the total sum to the console.
Answer:
int sum = 0;
//Two lines of code are missing here
System.out.println(“The total is: ” + sum); Ans: for(int i=1; i <= 50; i++)
sum += i;
1. Write code in the blank which would produce the required output.
public void drawTriangle()
{
int row // the row position
int column ; // the column position
____________________________________
//enter code here
{
for ( column = 1; column <= row; column++ )
System.out.print( "*" );
System.out.println();
} // end for loop
System.out.println();
}
Output: ********** ********* ******** ******* ****** *****
**** ***
**
*
Ans: 38
for ( row = 10; row >= 1; row– )
1
2. Write code in the blank which would produce the required output.
public void drawTriangle()
{
int row; // the row position
int column; // the column position
int space; // number of spaces to print
for ( row = 10; row >= 1; row– )
{
for ( space = 10; space > row; space– )
System.out.print( ” ” );
//Fill code here
System.out.print( “*” );
System.out.println();
} // end for loop
System.out.println();
}
Output:
**********
********* ******** ******* ****** ***** **** *** **
Answer 2: for ( column = 1; column <= row; column++ )
2
3. Identify and correct the error in the following piece of code.
while ( x <= 100 )
total += x;
++x;
ANS: The two statements should be enclosed in curly braces to
properly group them into the body of the while; otherwise the loop
will be an infinite loop.
4. The output produced by the following program is shown as it would
appear on the console. Fill in the 2 lines of code that would
produce the required output.
public class Mystery {
public static void main(String[] args) {
String i = "CS";
String j = "teacher";
String k = "tough";
String teacher = "face";
String course = "tasty";
compute(k, i, j);
compute(i, j, k);
// Fill in the two lines of code here.
compute(j, "k", j);
}
public static void compute(String k, String i, String j) {
System.out.println("The " + j + " of " + k + " is " + i);
}
}
Answer: compute(teacher, j, course); compute("course", k, "marty");
The teacher of tough is CS
The tough of CS is teacher
The tasty of face is teacher
The marty of course is tough
The teacher of teacher is k
3
4 6. Write ONE Java statement that computes and displays a random
number between 1 and 25.
Answer:
System.out.println("Random number between 1 and 25 " +
Math.round((Math.random() * 25)));
7. Declare and create a multidimensional array to hold the first and
last names of 10 people.
Answer: String[][] Names = new String[10][2];
8. Write 3 lines of code to initialize the following 2-d array to
-1.0.
Double[][] myDoubles = new Double[10][10];
Answer:
for(int row = 0; row < myDoubles.length; row++)
for(int column = 0; column < myDoubles[row].length; column++)
myDoubles[row][column] = -1.0;
9. Write a decision statement to determine if an object should be
downcast and then downcast it.
Answer:
if(someObject instance of MyObject)
MyObject m = (MyObject) someObject;
10. Define the signature for an abstract method which returns a
String and takes in 3 parameters – a Double object, an integer
primitive and a variable argument list of type objects.
Answer:
abstract String format(Double d, int s, Object... arguments);