Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 18:36:30 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 2: Primitive Data Types and Operations

Section 2.2 Writing Simple Programs
1   To add number to sum, you write (Note: Java is case-sensitive)

A. number += sum;
B. number = sum + number;
C. sum = Number + sum;
D. sum += number;
E. sum = sum + number;

2   What is the printout of the following code:

double x = 5.5;
int y = (int)x;
System.out.println("x is " + x + " and y is " + y);

A. x is 5 and y is 6
B. x is 6.0 and y is 6.0
C. x is 6 and y is 6
D. x is 5.5 and y is 5
E. x is 5.5 and y is 5.0

Section 2.3 Identifiers
3   Is every letter in a Java keyword in lowercase?

A. true
B. false

4   Which of the following is a valid identifier?

A. $343
B. class
C. 9X
D. 8+9
E. radius

Section 2.6 Constants
5   To declare a constant MAX_LENGTH inside a method with value 99.98, you write

A. final MAX_LENGTH = 99.98;
B. final float MAX_LENGTH = 99.98;
C. double MAX_LENGTH = 99.98;
D. final double MAX_LENGTH = 99.98;

6   Which of the following is a constant, according to Java naming conventions?

A. MAX_VALUE
B. Test
C. read
D. ReadInt
E. COUNT

Section 2.7 Numeric Data Types and Operations
7   Which of these data type requires the most amount of memory?

A. long
B. int
C. short
D. byte

8   To declare an int variable number with initial value 2, you write

A. int number = 2L;
B. int number = 2l;
C. int number = 2;
D. int number = 2.0;

9   What is result of 45 / 4?

A. 10
B. 11
C. 11.25
D. 12

10   Which of the following expression results in a value 1?

A. 2 % 1
B. 15 % 4
C. 25 % 5
D. 37 % 6

11   Analyze the following code.

public class Test {
   public static void main(String[] args) {
     int month = 09;
     System.out.println("month is " + month);
   }
}

A. The program displays month is 09
B. The program displays month is 9
C. The program displays month is 9.0
D. The program has a syntax error, because 09 is an incorrect literal value.

Section 2.7.4 Shortcut Operators
12   what is y displayed in the following code?

public class Test1 {
   public static void main(String[] args) {
     int x = 1;
     int y = x = x + 1;
     System.out.println("y is " + y);
   }
}

A. y is 0.
B. y is 1 because x is assigned to y first.
C. y is 2 because x + 1 is assigned to x and then x is assigned to y.
D. The program has a syntax error since x is redeclared in the statement int y = x = x + 1.

13   What is i printed?
public class Test {
   public static void main(String[] args) {
     int j = 0;
     int i = ++j + j * 5;

     System.out.println("What is i? " + i);
   }
}

A. 0
B. 1
C. 5
D. 6
E. None of the above

14   What is i printed in the following code?

public class Test {
   public static void main(String[] args) {
     int j = 0;
     int i = j++ + j * 5;

     System.out.println("What is i? " + i);
   }
}

A. 0
B. 1
C. 5
D. 6
E. None of the above

15   what is y displayed in the following code?

public class Test {
   public static void main(String[] args) {
     int x = 1;
     int y = x++ + x;
     System.out.println("y is " + y);
   }
}

A. y is 1.
B. y is 2.
C. y is 3.
D. y is 4.

16   what is y displayed?

public class Test {
   public static void main(String[] args) {
     int x = 1;
     int y = x + x++;
     System.out.println("y is " + y);
   }
}

A. y is 1.
B. y is 2.
C. y is 3.
D. y is 4.

Section 2.8 Numeric Type Conversions
17   To assign a double variable d to an float variable x, you write

A. x = (long)d
B. x = (int)d;
C. x = d;
D. x = (float)d;

18   Which of the following assignment statements is illegal?

A. float f = -34;
B. int t = 23;
C. short s = 10;
D. int t = (int)false;
E. int t = 4.5;

19   If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value.

A. byte
B. int;
C. long;
D. double;

Section 2.9 Character Data Type and Operations
20   Which of the following is the correct expression of character 4?

A. 4
B. "4"
C. '\0004'
D. None of the above.

21   An int variable can hold __________.

A. 'x'
B. 120
C. 120.0
D. true
E. "120"

22   Which of the following assignment statements is correct?

A. char c = 'd';
B. char c = 100;
C. char c = "d";
D. char c = "100";
E. a and b.

Section 2.10 boolean Data Type and Operations
23   Which of the Boolean expressions below is incorrect?

A. (true) && (3 => 4)
B. !(x > 0) && (x > 0)
C. (x > 0) || (x < 0)
D. (x != 0) || (x = 0)
E. (-10 < x < 0)

24   Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?

A. 1 < x < 100 && x < 0
B. ((x < 100) && (x > 1)) || (x < 0)
C. ((x < 100) && (x > 1)) && (x < 0)
D. (1 > x > 100) || (x < 0)

25   The "less than or equal to" comparison operator in Java is __________.

A. <
B. <=
C. =<
D. <<
E. !=

26   The equal comparison operator in Java is __________.

A. <>
B. !=
C. ==
D. ^=
E. None of the above

27   Suppose x=10 and y=10 what is x after evaluating the expression (y > 10) & (x++ > 10).

A. 9
B. 10
C. 11

28   Suppose x=10 and y=10 what is x after evaluating the expression (y > 10) && (x++ > 10).

A. 9
B. 10
C. 11

29   Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) | (x++ > 10).

A. 9
B. 10
C. 11

30   Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10).

A. 9
B. 10
C. 11

31   What is the value of the following expression?

true | true && false

A. true
B. false

32   What is the value of the following expression?

true || true && false

A. true
B. false

33   What is the value of the following expression?

true | true & false

A. true
B. false

Section 2.13 Getting Input from Input Dialogs
34   The __________ method parses a string s to an int value.

A. integer.parseInt(s);
B. Integer.parseInt(s);
C. integer.parseInteger(s);
D. Integer.parseInteger(s);

35   The __________ method parses a string s to a double value.

A. double.parseDouble(s);
B. Double.parsedouble(s);
C. double.parseDouble(s);
D. Double.parseDouble(s);

36   Analyze the following code.

import javax.swing.*;

public class ShowErrors {
   public static void main(String[] args) {
     int i;
     int j;
     String s = JOptionPane.showInputDialog(null,
       "Enter an integer", "Input",
       JOptionPane.QUESTION_MESSAGE);
     j = Integer.parseInt(s);

     if (j > 3) i = (i + 4);
   }
}

A. The program cannot compile because j is not initialized.
B. The program cannot compile because i does not have an initial value when it is used in i = i + 4;
C. The program compiles but has a runtime error because i deos not have an initial value when it is used in i = i + 4;
D. The program compiles and runs fine.

Section 2.14 Case Studies
37   The __________ method returns a raised to the power of b.

A. Math.power(a, b);
B. Math.exponent(a, b);
C. Math.pow(a, b);
D. None of the above

Section 2.17 Programming Errors
38   If a program compiles fine, but it produces incorrect result, then the program suffers __________.
A. a compilation error
B. a runtim error
C. a logic error