Review Questions – August 29, 2014 What will be printed by the

advertisement
Review Questions – August 29, 2014
1. What will be printed by the following code segment?
boolean flag = true;
int x = -1;
if (flag && (x > 0))
System.out.println(“yes”);
else if (x==0)
System.out.println(“maybe”);
else if (!flag)
System.out.println(“sometimes”);
else
System.out.println(“no”);
a.
b.
c.
d.
e.
yes
maybe
sometimes
no
There will be an error because you can’t mix integers and Booleans in the same expression.
2. The expression !f || g is the same as which of the following?
a.
b.
c.
d.
e.
F || !g
!(f || g)
!(f && g)
!(!f && !g)
!(f && !g)
3. Assume num and max are integer variables. Consider the code
while (num<max)
num++;
Which values of num and max will cause the body of the loop to be executed exactly once?
a.
b.
c.
d.
e.
num = 1, max = 1;
num = 1, max = 2;
num = 2, max = 2;
num = 2, max = 1;
num = 1, max = 3;
4. Which of the following expression tests to make sure the grade is between 0 and 100 inclusive?
a.
b.
c.
d.
e.
(grade <= 100) || (graph <= 0)
(grade <= 100) || (graph >= 0)
(grade < 101) || (graph > -1)
(grade <= 100) && (graph >= 0)
(grade >= 100) && (graph <= 0)
5. What is wrong with the following code segment? Rewrite it so that it produces correct output.
iIf (total == MAX)
if (total < sum)
System.out.println(“total == MAX and is < sum.”);
else
System.out.println(“total is not equal to MAX”);
6. 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”);
7. What output is produced by the following code fragment?
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”);
Enter the following programs in JCreator or NetBeans. Check the results and make sure that you
understand the function of each line in the programs.
3.6 CountDigits
//********************************************************************
// CountDigits.java
Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.6
//********************************************************************
import java.util.Scanner;
public class CountDigits
{
//----------------------------------------------------------------// Counts the number of odd, even, and zero digits in an
// integer input value.
//----------------------------------------------------------------public static void main (String[] args)
{
int oddCount = 0, evenCount = 0, zeroCount = 0;
int value, digit;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter an integer value: ");
value = scan.nextInt();
value = Math.abs (value);
if (value == 0)
zeroCount++;
while (value > 0)
{
digit = value % 10;
if (digit == 0)
zeroCount++;
else
if (digit%2 == 0)
evenCount++;
else
oddCount++;
value = value / 10;
}
System.out.println ("Zero digits: " + zeroCount);
System.out.println ("Even digits: " + evenCount);
System.out.println ("Odd digits: " + oddCount);
}
}
3.10 HiLo
//********************************************************************
// HiLo.java
Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.10
//********************************************************************
import java.util.Scanner;
public class HiLo
{
//----------------------------------------------------------------// Randomly selects a number in a particular range, which the
// user attempts to guess.
//----------------------------------------------------------------public static void main (String[] args)
{
final int MAX = 100;
int target, count = 0, guess;
String again;
Scanner scan = new Scanner(System.in);
do
{
System.out.println();
System.out.println ("Guess a number between 1 and " + MAX);
target = (int) (Math.random() * MAX) + 1;
do
{
System.out.println();
System.out.print ("Enter your guess (0 to quit): ");
guess = scan.nextInt();
count = count + 1;
if (guess > 0)
if (guess == target)
System.out.println ("Right! Guesses: " + count);
else
if (guess < target)
System.out.println ("Your guess was too LOW.");
else
System.out.println ("Your guess was too HIGH.");
}
while (guess != target && guess > 0);
System.out.println();
System.out.print ("Play again (y/n)?: ");
again = scan.nextLine();
}
while (again.equalsIgnoreCase ("y"));
}
}
Download