0% found this document useful (0 votes)
395 views

MC Ch02 IntroApplication

This document contains 33 multiple choice questions about Java programming concepts covered in Chapter 2 of a Java textbook. The questions cover topics like Java syntax, variables, data types, operators, conditional statements, and input/output. For each question, the possible answer choices and correct answer are provided.

Uploaded by

meziacri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
395 views

MC Ch02 IntroApplication

This document contains 33 multiple choice questions about Java programming concepts covered in Chapter 2 of a Java textbook. The questions cover topics like Java syntax, variables, data types, operators, conditional statements, and input/output. For each question, the possible answer choices and correct answer are provided.

Uploaded by

meziacri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter 02 Java How to Program, 11/e Multiple Choice Test Bank Page 1 of 6

Chapter 02: Introduction to Java Applications

Q1: End-of-line comments that should be ignored by the compiler are denoted using
a. Two forward slashes (//).
b. Three forward slashes (///).
c. A slash and a star (/*).
d. A slash and two stars (/**).
ANS: a. Two forward slashes (//).

Q2: Which of the following is not a valid Java identifier?


a. my Value
b. $_AAA1
c. width
d. m_x
ANS: a. my Value (Identifiers may not contain blanks).

Q3: Which of the following cannot cause a syntax error to be reported by the Java compiler?
a. Mismatched {}
b. Missing */ in a comment that begins with /*
c. Missing ;
d. An extra blank line.
ANS: d. An extra blank line.

Q4: Which of the following does not contain a syntax error?


a. System.out.println('Hello world!'):
b. System.out.println("Hello
world!");
c. System.out.println("Hello world!");
d. System.out.println(Hello world!);
ANS: c. System.out.println("Hello world!");

Q5: Which command compiles the Java source code file Welcome.java?
a. cd Welcome.java
b. javac Welcome.java
c. java Welcome.java
d. compile Welcome.java
ANS: b. javac Welcome.java

Q6: Which command executes the Java class file Welcome.class?


a. java welcome
b. java Welcome.class
c. java Welcome
d. run Welcome.class
ANS: c. java Welcome (Note that you must use the same capitalization as the class name.)

Q7: Which is the output of the following statements?


System.out.print("Hello ");
System.out.println("World");
a. Hello World
b. HelloWorld
Chapter 02 Java How to Program, 11/e Multiple Choice Test Bank Page 2 of 6
c. Hello
World
d. World
Hello
ANS: a. Hello World

Q8: Which of the following is the escape character?


a. *
b. \
c. \n
d. "
ANS: b. \

Q9: Which of the following statements will print a single line containing
"hello there"?
a. System.out.println("hello");
System.out.println(" there");
b. System.out.println("hello" , " there");
c. System.out.println("hello");
System.out.print(" there");
d. System.out.print("hello");
System.out.println(" there");
ANS: d. System.out.print("hello");
System.out.println(" there");

Q10: Which of the following escape sequences represents a carriage return?


a. \n.
b. \r.
c. \cr.
d. \c.
ANS: b. \r.

Q11: Which of the following statements would display the phrase Java is fun?
a. System.out.println("hellois fun\rJava ");
b. System.out.println('Java is fun');
c. System.out.println("\"Java is fun\"");
d. System.out.println(Java is fun);
ANS: a. System.out.println("hellois fun\rJava ");

Q12: When method printf requires multiple arguments, the arguments are separated with ________.
a. colons (:).
b. semicolons (;).
c. commas (,).
d. periods (.).
ANS: c. commas (,).

Q13: Which of the following statement displays Hello World?


a. System.out.printf("%2s", "Hello " "World");
b. System.out.printf("%s %s", "Hello", "World");
c. System.out.printf("%s%s", "Hello, World");
d. System.out.printf("s% s%", "Hello", "World");
ANS: b. System.out.printf("%s %s", "Hello", "World");
Chapter 02 Java How to Program, 11/e Multiple Choice Test Bank Page 3 of 6
Q14: Programs remember numbers and other data in the computer's memory and access that data through
program elements called
a. comments.
b. messages.
c. integers.
d. variables.
ANS: d. variables.

Q15: All import declarations must be placed


a. inside the class declaration’s body.
b. before the class declaration.
c. after the class declaration.
d. all of the above will work.
ANS: b. before the class declaration.

Q16: Java's predefined classes are grouped into


a. packets.
b. declarations.
c. Galleries.
d. packages.
ANS: d. packages.

Q17: Which of the following is a variable declaration statement?


a. int total;
b. import java.util.Scanner;
c. public static void main(String args[])
d. // first string entered by user
ANS: a. int total;

Q18: A(n) ________ enables a program to read data from the user.
a. printf.
b. import declaration.
c. Scanner.
d. main.
ANS: c. Scanner.

Q19: Which of the following statements is true?


a. System.out.print("Enter your age: "); prompts the user to take action.
b. Class names typically begin with a capital letter.
c. Package java.lang is imported in every Java program.
d. All of the above are true.
ANS: d. All of the above are true.

Q20: Which of the following is not a Java primitive type?


a. char
b. byte
c. real
d. double
ANS: c. real
Chapter 02 Java How to Program, 11/e Multiple Choice Test Bank Page 4 of 6
Q21: Which of the following statements is false?
a. Primitive types are keywords.
b. Primitive types must appear in all lowercase letters.
c. Real numbers contain decimal points.
d. Variable name identifiers must begin with a lowercase letter.
ANS: d. Variable name identifiers must begin with a lowercase letter. This is not required, but it is a convention.

Q22: Which of the following is a Scanner method for inputting an integer value?
a. nextInteger
b. integer
c. nextInt
d. int
ANS: c. nextInt.

Q23: Given the Java statement

int number1 = input.nextInt();

in which input is a Scanner, which of the following occurs if the user does not enter a valid int value?
a. A compilation error occurs.
b. The program continues executing and assigns the value 0 to number1.
c. A runtime logic error occurs.
d. None of the above.
ANS: c. A runtime logic error occurs.

Q24: Portions of statements that contain calculations are called


a. variables.
b. constants.
c. expressions.
d. None of the above.
ANS: c. expressions.

Q25: Given the Java statement

int sum = number1 + number2;

which of the following statements is false?


a. It’s an assignment statement.
b. It calculates the sum of variables number1 and number2.
c. The operands of the addition operator are number1 and number2.
d. It assigns the value of number1 to sum.
ANS: d. It assigns the value of number1 to sum. Actually, it assigns the total of number1 and number2 to sum.

Q26: The format specifier ________ is a placeholder for an int value.


a. %n
b. %d
c. %int
d. %s
ANS: b. %d
Chapter 02 Java How to Program, 11/e Multiple Choice Test Bank Page 5 of 6
Q27: Optional parentheses in expressions are said to be
a. redundant.
b. binary operators.
c. implied.
d. declared.
ANS: a. redundant.

Q28: Which of the following statements does not alter the value stored in a memory location?
a. int a;
b. number = 12;
c. y = y + 2;
d. width = Integer.parseInt(input);
ANS: a. int a;

Q29: What is the value of result after the following Java statements execute (assume all variables are of type int)?
a = 4;
b = 12;
c = 37;
d = 51;
result = d % a * c + a % b + a;
a. 119
b. 51
c. 127
d. 59
ANS: a. 119

Q30: Which of the following is not an arithmetic operator?


a. +
b. -
c. .
d. %
ANS: c

Q31: What will be output after the following Java statements have been executed (assume all variables are of type
int)?
a = 4;
b = 12;
c = 37;
d = 51;

if (a < b) {
System.out.println("a < b");
}

if (a > b) {
System.out.println("a > b");
}

if (d <= c) {
System.out.println("d <= c");
}

if (c != d) {
Chapter 02 Java How to Program, 11/e Multiple Choice Test Bank Page 6 of 6
System.out.println("c != d");
}

a. a<b
c != d
b. a < b
d <= c
c != d
c. a > b
c != d
d. a < b
c<d
a != b
ANS: a. a < b
c != d

Q32: Which of the following is not a compilation error?


a. Neglecting to initialize a local variable in a method before it is used.
b. Placing a semicolon at the end of the first line of an if statement.
c. Omitting the left and right parenthesis for the condition of an if statement.
d. All are compilation errors.
ANS: b. Placing a semicolon at the end of the first line of an if statement.

Q33: Each of the following is a relational or equality operator except:


a. <=
b. =!
c. ==
d. >
ANS: b. =!

You might also like