Programming Final Review
Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates?
10
How many times will the following loop iterate?
10
How many times will the following loop iterate?
10 times
If x is currently equal to 5, what will the value of x be after the switch statement executes?
11
If x is an int where x = 1, what will x be after the following loop terminates?
128
What output is produced by the following code fragment? int num = 1, max = 20; while (num < max) { System.out.println (num); num += 4; }
159131721
If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
2
What output is created by the following code fragment? int num = 1, max = 20; while (num < max) { if (num%2 ==0) System.out.println (num); num++; }
2 4 6 8 10 12 14 16 18
If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
5
Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score.
This code will work correctly only if score < 70
An if statement may or may not have an else clause, but an else clause must be part of an if statement.
True
Assume that the class Bird has a static method fly( ). If b is a Bird, then to invoke fly, you could do Bird.fly( );.
True
Unlike the String class where you must pass a message to an object (instance) of the class, as in x.length( ), in order to use the Math class, you pass messages directly to the class name, as in Math.abs( ) or Math.sqrt( ).
True
Control in a switch statement jumps to the first matching case.
Ture
The statement { } is a legal block.
Ture
Write a method called sum100 that returns the sum of the integers from 1 to 100 inclusive. (you must use an appropriate looping construct).
public int sum100() { int sum = 0; for (int count = 1; count <= 100; count++) sum += count; return sum; }
Given that s is a String, what does the following loop do?
it prints s out backwards
Having multiple class methods of the same name where each method has a different number of or type of parameters is known as
method overloading
Write a method called multiConcat that takes a String and an integer as parameters. Return a String that consists of the string parameter concatentated with itself count times, where count is the integer parameter. For example if the parameter values are "hi" and 4, then return value is "hihihihi". Return the original string if the integer parameter is less than 2.
public String multiConcat (String text, int repeats) { String result = text; if (repeats > 1) for (int count = 2; count <= repeats; count++) result += text; return result; }
Write a method called evenlyDivisible that accepts two integer parameters and returns true if the first parameter is evenly divisible by the second, or vice versa, and false otherwise. Return false if either parameter is zero.
public boolean evenlyDivisible (int num1, int num2) { boolean result = false; if (num1 != 0 && num2 != 0) if (num1 % num2 ==0 || num2 % num1 ==0) result = true; return result; }
Write a method called average that accepts two integer parameters and returns their average as a floating point value.
public double average (int num1, int num2) { return (num1 + num2) / 2.0; }
For the questions below, consider a class called ChessPiece. This class has two instance data, String type and int player. The variable type will store "King", "Queen", "Bishop", etc and the int player will store 0 or 1 depending on whose piece it is. We wish to implement Comparable for the ChessPiece class. Assume that, the current ChessPiece is compared to a ChessPiece passed as a parameter. Pieces are ordered as follows: "Pawn" is a lesser piece to a "Knight" and a "Bishop", "Knight" and "Bishop" are equivalent for this example, both are lesser pieces to a "Rook" which is a lesser piece to a "Queen" which is a lesser piece to a "King." Which of the following method headers would properly define the method needed to make this class Comparable?
public int compareTo(Object cp)
Which of the following methods is a static method? The class in which the method is defined is given in parentheses following the method name.
sqrt (Math)
All objects implement Comparable.
False
Any class can implement an interface, but no classes can implement more than a single interface.
False
In Java, it is possible to create an infinite loop out of while and do loops, but not for-loops.
False
Which of the following statements are true about Java loops?
all three loop statements are functionally equivalent
A switch statement must have a default clause.
False
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
False
It is not possible to test out any single method or class of a system until the entire system has been developed, and so all testing is postponed until after the implementation phase.
False
The expression (s.concat(t).length( ) < y) is true.
False
When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
Flase
What output is produced by the following code frament? int limit = 100, num1 = 15, num2 = 40; if (limit <= limit) { if (num1 == num2) System.out.println ("lemon"); System.out.println ("lime"); } System.out.println ("grape");
Lime grape
Write a code fragment that reads and prints integer values entered by a user until a particular sentinel value (stored in SENTINEL) is entered. Do not print the sentinel value. You need to declare variables, etc.
Scanner scan = new Scanner (System.in); System.out.println ("Enter some integers (" + SENTINEL + " to quit):"); number = scan.nextInt(); while number (!= SENTINEL) { System.out.println (number); number = scan.nextInt(); }
What is wrong, logically, with the following code?
There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
What output is produced by the following code fragment? for (int val = 200; val >= 0; val -=1) if (val % 4 != 0) System.out.println (val);
The output produced is all values from 200 down to 0, except those that are evenly divisible by 4: 199 198 197 195 and so on until... 5 3 2 1
What output is produced by the following code fragment? int num = 87, max = 25; if (num > max * 2) System.out.println ("apple"); System.out.println ("orange"); System.out.println ("pear");
apple ogrange pear
In order to implement Comparable in a class, what method(s) must be defined in that class?
compareTo
In order to create a constant, you would use which of the following Java reserved words?
final
The goal of testing is to
find logical and run-time errors
Write a for loop that prints the odd numbers from 1 to 99 (inclusive)
for(int i=1; i<=99;i++) { if(i%2==0) { System.out.println(); } else { System.out.println(i); } }
Write a code fragment that reads 10 integer values from the user and prints the highest value entered. You must use an appropriate looping construct.
for(int i=1; int i=10;i++) { System.out.println("Please enter an integer value") number=scan.nextInt(); } pnumber=str.charAt(number);
Write a code fragment that prints the characters stored in a String object called str, backwards.
for(int i=str.length(); i>=0; i--) { pnumber=str.charAt(str); System.out.println(str.charAt(i)); }
Use nested for loops to produce the following output. You must use constants for the loop limits. 1 12 123 1234 12345 123456 1234567 12345678 123456789 9 98 987 9876 98765 987654 9876543 98765432 987654321
for(int row=0;row<=1;row++) { for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) { for(int space=9;space>=10-row;space--) { System.out.print(j); } } System.out.println(); } } 2 public static void main(String agrs[]) { for(int row=0;row<=1;row++) { for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) { for(int space=9;space>=10-row;space--) { System.out.print(j); } } System.out.println(); } } }
Rewrite the following set of if statements using a nested if-else structure.
if (score is >= 90) grade = "A"; else if (score >= 80) grade ="B"; else if (score >= 70) grade = "C"; else if (score >= 60) grade = "D"; else grade = "F";
For the questions below, consider a class called ChessPiece. This class has two instance data, String type and int player. The variable type will store "King", "Queen", "Bishop", etc and the int player will store 0 or 1 depending on whose piece it is. We wish to implement Comparable for the ChessPiece class. Assume that, the current ChessPiece is compared to a ChessPiece passed as a parameter. Pieces are ordered as follows: "Pawn" is a lesser piece to a "Knight" and a "Bishop", "Knight" and "Bishop" are equivalent for this example, both are lesser pieces to a "Rook" which is a lesser piece to a "Queen" which is a lesser piece to a "King." Which of the following pieces of logic could be used in the method that implements Comparable? Assume that the method is passed Object a, which is really a ChessPiece. Also assume that ChessPiece has a method called returnType which returns the type of the given piece. Only one of these answers has correct logic.
if (this.type.equals(a.returnType( )) return 0;
Of the following if statements, which one correctly executes three instructions if the condition is true?
if (x < 0) { a = b * 2; y = x; z = a - y; }
****The following code has a syntax error immediately before the word else. What is the error and why does it arise? Fix the code so that this statement is a legal if-else statement.
if(x<0) { x++ } else x--;
Write a while loop that verifies that the user enters a positive integer value. You need to declare variables, etc.
int java.util.Scanner; public static void main(String args[]) { Scanner scan = new Scanner(System.in); int number; System.out.println(" Please enter a positive integer value: "); number = scan.nextInt(); while(number!0) { if(number>0) { System.out.println("Yes! that is a positive integer value! Your number is:"+number); } else { System.out.println("Error! Please enter a positive number: "); number=scan.nextInt(); } }
During program development, software requirements specify
what the task is that the program must perform
Write a method called sumRange that accepts two integer parameters that represent a range. Issue an error message and return 0 if the second parameter is less than the first. Otherwise, the method should return the sum of the integers in that range (inclusive).
public int sumRange (int start, int end) { int sum = 0; if (end < start) System.out.println ("ERROR: Invalid Range"); else for (int num = start; num <= end; num++) sum += num; return sum; }
Create an interface called Visible that includes two methods: makeVisible and makeInvisible. Both methods should take no parameters and should return a boolean result.
public interface Visible { public boolean makeVisible(); public boolean makeInvisible(); }
Static methods cannot
reference non-static instance data
Consider the following outline of a nested if-else structure which has more if clauses than else clauses. Which of the statements below is true regarding this structure?
statement2 will only execute if condition1 is true and condition2 is false
The do loop differs from the while loop in that
the do loop will always execute the body of the loop at least once
You might choose to use a switch statement instead of nested if-else statements if
there are two or more int variables being tested, each of which could be one of several hundred values
An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?
this