Introduction to Java Programming, Includes Data Structures, Eleventh Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Chapter 2 Elementary Programming


Section 2.2 Writing a Simple Program
2.1  _______ is the code with natural language mixed with Java code.
A. Java program
B. A Java statement
C. Pseudocode
D. A flowchart diagram

2.2  What is the exact output of the following code?

  double area = 3.5;
  System.out.print("area");
  System.out.print(area);
A. 3.53.5
B. 3.5 3.5
C. area3.5
D. area 3.5

Section 2.3 Reading Input from the Console
2.3  Suppose a Scanner object is created as follows, what method do you use to read a real number?

Scanner input = new Scanner(System.in);
A. input.nextDouble();
B. input.nextdouble();
C. input.double();
D. input.Double();

2.4  The following code fragment reads in two numbers:

Scanner input = new Scanner(System.in);
int i = input.nextInt();
double d = input.nextDouble();

What is the incorrect way to enter these two numbers?
A. Enter an integer, a space, a double value, and then the Enter key.
B. Enter an integer, two spaces, a double value, and then the Enter key.
C. Enter an integer, an Enter key, a double value, and then the Enter key.
D. Enter a numeric value with a decimal point, a space, an integer, and then the Enter key.

2.5  If you enter 1 2 3, when you run this program, what will be the output?

import java.util.Scanner;

public class Test1 {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter three numbers: ");
    double number1 = input.nextDouble();
    double number2 = input.nextDouble();
    double number3 = input.nextDouble();

    // Compute average
    double average = (number1 + number2 + number3) / 3;

    // Display result
    System.out.println(average);
  }
}
A. 1.0
B. 2.0
C. 3.0
D. 4.0

Section 2.4 Identifiers
2.6  Every letter in a Java keyword is in lowercase?
A. true
B. false

2.7  Which of the following is a valid identifier?
A. $343
B. class
C. 9X
D. 8+9
E. radius

Section 2.5 Variables
2.8  Which of the following are correct names for variables according to Java naming conventions?
A. radius
B. Radius
C. RADIUS
D. findArea
E. FindArea

2.9  Which of the following are correct ways to declare variables?
A. int length; int width;
B. int length, width;
C. int length; width;
D. int length, int width;

Section 2.6 Assignment Statements and Assignment Expressions
2.10  ____________ is the Java assignment operator.
A. ==
B. :=
C. =
D. =:

2.11  To assign a value 1 to variable x, you write
A. 1 = x;
B. x = 1;
C. x := 1;
D. 1 := x;
E. x == 1;

2.12  Which of the following assignment statements is incorrect?
A. i = j = k = 1;
B. i = 1; j = 1; k = 1;
C. i = 1 = j = 1 = k = 1;
D. i == j == k == 1;

Section 2.7 Named Constants
2.13  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;

2.14  Which of the following is a constant, according to Java naming conventions?
A. MAX_VALUE
B. Test
C. read
D. ReadInt
E. COUNT

2.15  To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159.
A. variables
B. methods
C. constants
D. classes

Section 2.8 Naming Conventions
2.16  According to Java naming convention, which of the following names can be variables?
A. FindArea
B. findArea
C. totalLength
D. TOTAL_LENGTH
E. class

Section 2.9 Numeric Data Types and OperationsSection 2.9.1 Numeric Types
2.17  Which of these data types requires the most amount of memory?
A. long
B. int
C. short
D. byte

2.18  When assigning a literal to a variable of the byte type, if the literal is too large to be stored as a byte value, it _____________.
A. causes overflow
B. causes underflow
C. causes no error
D. cannot happen in Java
E. receives a compile error

Section 2.9.3 Numeric Operators
2.19  What is the result of 45 / 4?
A. 10
B. 11
C. 11.25
D. 12

2.20  Which of the following expression results in a value 1?
A. 2 % 1
B. 15 % 4
C. 25 % 5
D. 37 % 6

2.21  25 % 1 is _____
A. 1
B. 2
C. 3
D. 4
E. 0

2.22  -25 % 5 is _____
A. 1
B. 2
C. 3
D. 4
E. 0

2.23  24 % 5 is _____
A. 1
B. 2
C. 3
D. 4
E. 0

2.24  -24 % 5 is _____
A. -1
B. -2
C. -3
D. -4
E. 0

2.25  -24 % -5 is _____
A. 3
B. -3
C. 4
D. -4
E. 0

Section 2.9.4 Exponent Operations
2.26  How do you write 2.5 ^ 3.1 in Java?
A. 2.5 * 3.1
B. Math.pow(2.5, 3.1)
C. Math.pow(3.1, 2.5)
D. 2.5 ** 3.1
E. 3.1 ** 2.5

2.27  Math.pow(2, 3) returns __________.
A. 9
B. 8
C. 9.0
D. 8.0

2.28  Math.pow(4, 1 / 2) returns __________.
A. 2
B. 2.0
C. 0
D. 1.0
E. 1

2.29  Math.pow(4, 1.0 / 2) returns __________.
A. 2
B. 2.0
C. 0
D. 1.0
E. 1

2.30  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. Math.pow(b, a)

Section 2.10 Numeric Literals
2.31  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;

2.32  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.

2.33  Which of the following is incorrect?
A. 1_2
B. 0.4_56
C. 1_200_229
D. _4544

2.34  Which of the following are the same as 1545.534?
A. 1.545534e+3
B. 0.1545534e+4
C. 1545534.0e-3
D. 154553.4e-2

2.35  Which of the following is incorrect?
A. int x = 9;
B. long x = 9;
C. float x = 1.0;
D. double x = 1.0;

Section 2.11 Evaluating Expressions and Operator Precedence
2.36  The expression 4 + 20 / (3 - 1) * 2 is evaluated to
A. 4
B. 20
C. 24
D. 9
E. 25

Section 2.12 Case Study: Displaying the Current Time
2.37  The System.currentTimeMillis() returns ________________ .
A. the current time.
B. the current time in milliseconds.
C. the current time in milliseconds since midnight.
D. the current time in milliseconds since midnight, January 1, 1970.
E. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).

2.38  To obtain the current second, use _________.
A. System.currentTimeMillis() % 3600
B. System.currentTimeMillis() % 60
C. System.currentTimeMillis() / 1000 % 60
D. System.currentTimeMillis() / 1000 / 60 % 60
E. System.currentTimeMillis() / 1000 / 60 / 60 % 24

2.39  To obtain the current minute, use _________.
A. System.currentTimeMillis() % 3600
B. System.currentTimeMillis() % 60
C. System.currentTimeMillis() / 1000 % 60
D. System.currentTimeMillis() / 1000 / 60 % 60
E. System.currentTimeMillis() / 1000 / 60 / 60 % 24

2.40  To obtain the current hour in UTC, use _________.
A. System.currentTimeMillis() % 3600
B. System.currentTimeMillis() % 60
C. System.currentTimeMillis() / 1000 % 60
D. System.currentTimeMillis() / 1000 / 60 % 60
E. System.currentTimeMillis() / 1000 / 60 / 60 % 24

Section 2.13 Augmented Assignment Operators
2.41  To add a value 1 to variable x, you write
A. 1 + x = x;
B. x += 1;
C. x := 1;
D. x = x + 1;
E. x = 1 + x;

2.42  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.43  Suppose x is 1. What is x after x += 2?
A. 0
B. 1
C. 2
D. 3
E. 4

2.44  Suppose x is 1. What is x after x -= 1?
A. 0
B. 1
C. 2
D. -1
E. -2

2.45  What is x after the following statements?

int x = 2;
int y = 1;
x *= y + 1;
A. x is 1.
B. x is 2.
C. x is 3.
D. x is 4.

2.46  What is x after the following statements?

int x = 1;
x *= x + 1;
A. x is 1.
B. x is 2.
C. x is 3.
D. x is 4.

2.47  Which of the following statements are the same?

(A) x -= x + 4
(B) x = x + 4 - x
(C) x = x - (x + 4)
A. (A) and (B) are the same
B. (A) and (C) are the same
C. (B) and (C) are the same
D. (A), (B), and (C) are the same

Section 2.14 Increment and Decrement Operators
2.48  Are the following four statements equivalent?

  number += 1;
  number = number + 1;
  number++;
  ++number;
A. Yes
B. No

2.49  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

2.50  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

2.51  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.

2.52  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.15 Numeric Type Conversions
2.53  To assign a double variable d to a float variable x, you write
A. x = (long)d
B. x = (int)d;
C. x = d;
D. x = (float)d;

2.54  Which of the following expressions will yield 0.5?
A. 1 / 2
B. 1.0 / 2
C. (double) (1 / 2)
D. (double) 1 / 2
E. 1 / 2.0

2.55  What is the output 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

2.56  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;

2.57  What is the value of (double)5/2?
A. 2
B. 2.5
C. 3
D. 2.0
E. 3.0

2.58  What is the value of (double)(5/2)?
A. 2
B. 2.5
C. 3
D. 2.0
E. 3.0

2.59  Which of the following expression results in 45.37?
A. (int)(45.378 * 100) / 100
B. (int)(45.378 * 100) / 100.0
C. (int)(45.378 * 100 / 100)
D. (int)(45.378) * 100 / 100.0

2.60  The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
A. 76.02
B. 76
C. 76.0252175
D. 76.03

2.61  If you attempt to add an int, a byte, a long, and a double, the result will be a(n) __________ value.
A. byte
B. int
C. long
D. double

Section 2.16 Software Life Cycle
2.62  _____________ is a formal process that seeks to understand the problem and document in detail what the software system needs to do.
A. Requirements specification
B. Analysis
C. Design
D. Implementation
E. Testing

2.63  _____________ seeks to analyze the data flow and to identify the system?s input and output. When you do analysis, it helps to identify what the output is first, and then figure out what input data you need in order to produce the output.
A. Requirements specification
B. Analysis
C. Design
D. Implementation
E. Testing

Section 2.18 Common Errors and Pitfalls
2.64  Analyze the following code:

public class Test {
  public static void main(String[] args) {
    int n = 10000 * 10000 * 10000;
    System.out.println("n is " + n);
  }
}
A. The program displays n is 1000000000000.
B. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program is aborted.
C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow.
D. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program is aborted.
E. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program continues to execute because Java does not report errors on underflow.