Quiz on Chapter 3
This page contains questions on Chapter 3 of Introduction to Programming Using Java. You should be able to answer these questions after studying that chapter. Sample answers to these questions can be found here.
Question 1:
What is an algorithm?
Question 2:
Explain briefly what is meant by "pseudocode" and how is it useful in the development of algorithms.
Question 3:
What is a block statement? How are block statements used in Java programs?
Question 4:
What is the main difference between a while loop and a do..while loop?
Question 5:
What does it mean to prime a loop?
Question 6:
Explain what is meant by an animation and how a computer displays an animation.
Question 7:
Write a for loop that will print out all the multiples of 3 from 3 to 36, that is: 3 6 9 12 15 18 21 24 27 30 33 36.
Question 8:
Fill in the following main() routine so that it will ask the user to enter an integer, read the user's response, and tell the user whether the number entered is even or odd. (You can use TextIO.getInt() to read the integer. Recall that an integer n is even if n % 2 == 0.)
public static void main(String[] args) { // Fill in the body of this subroutine! }
Question 9:
Write a code segment that will print out two different random integers selected from the range 1 to 10. All possible outputs should have the same probability. Hint: You can easily select two random numbers, but you have to account for the fact that the two numbers that you pick might be the same.
Question 10:
Suppose that s1 and s2 are variables of type String, whose values are expected to be string representations of values of type int. Write a code segment that will compute and print the integer sum of those values, or will print an error message if the values cannot successfully be converted into integers. (Use a try..catch statement.)
Question 11:
Show the exact output that would be produced by the following main() routine:
public static void main(String[] args) { int N; N = 1; while (N <= 32) { N = 2 * N; System.out.println(N); } }
Question 12:
Show the exact output produced by the following main() routine:
public static void main(String[] args) { int x,y; x = 5; y = 1; while (x > 0) { x = x - 1; y = y * x; System.out.println(y); } }
Question 13:
What output is produced by the following program segment? Why? (Recall that name.charAt(i) is the i-th character in the string, name.)
String name; int i; boolean startWord; name = "Richard M. Nixon"; startWord = true; for (i = 0; i < name.length(); i++) { if (startWord) System.out.println(name.charAt(i)); if (name.charAt(i) == ' ') startWord = true; else startWord = false; }
Question 14:
Suppose that numbers is an array of type int[]. Write a code segment that will count and output the number of times that the number 42 occurs in the array.
Question 15:
Define the range of an array of numbers to be the maximum value in the array minus the minimum value. Suppose that raceTimes is an array of type double[]. Write a code segment that will find and print the range of raceTimes.