Test Bank for Starting Out with Java: From Control Structures through Data Structures, 4th edition
Preview Extract
Starting Out with Java: From Control Structures through Data Structures 4e (Gaddis
and Muganda)
Chapter 2 Java Fundamentals
TRUE/FALSE
1. Programming style includes techniques for consistently putting spaces and indentation in a program to
help create visual cues.
ANS: T
2. Both character and string literals can be assigned to a char variable.
ANS: F
3. A variable’s scope is the part of the program that has access to that variable.
ANS: T
4. Named constants are initialized with a value and that value cannot change during the execution of the
program.
ANS: T
5. When you call one of the Scanner class’s methods to read a primitive value, such as nextInt or
nextDouble, and then call the nextLine method to read a string, an annoying and hard-to-find
problem can occur.
ANS: T
6. Class names and key words are examples of variables.
ANS: F
7. The Java API provides a class named Math that contains numerous methods which are useful for
performing complex mathematical operations.
ANS: T
8. The System.out.printf method allows you to format output in a variety of ways.
ANS: T
9. A Java program will not compile unless it contains the correct line numbers.
ANS: F
10. Java is not case sensitive.
ANS: F
11. If the compiler encounters a statement that uses a variable before the variable is declared, an error will
result.
ANS: T
MULTIPLE CHOICE
1. Which of the following is a value that is written into the code of a program?
a. a literal
b. an assignment statement
c. an operator
d. a variable
ANS: A
2. A Java program must have at least one of the following:
a.
b.
c.
d.
a comment
a class definition
a System.out.println(); statement
a variable declaration
ANS: B
3. Which of the following would contain the translated Java byte code for a program named Demo?
a. Demo.java
c. Demo.class
Demo.code
b.
d. Demo.byte
ANS: C
4. Which of the following is a named storage location in the computer’s memory?
a. a literal
b. an operator
c. a constant
d. a variable
ANS: D
5. Which of the following is not a valid Java comment?
a. /** Comment one */
b. */ Comment two /*
c. // Comment three
d. /* Comment four */
ANS: B
6. To compile a program named First you would use which of the following commands?
a. java First.java
c. javac First.java
b. javac First
d. compile First.javac
ANS: C
7. A Java source file must be saved with the extension
a. .java
b. .javac
ANS: A
c. .src
d. .class
8. Which of the following is not a rule that must be followed when naming identifiers?
a. After the first character, you may use the letters a-z, A-Z, an underscore, a dollar sign, or
the digits 0-9.
b. Identifiers can contain spaces.
c. Uppercase and lowercase characters are distinct.
d. The first character must be one of the letters a-z, A-Z, an underscore, or a dollar sign.
ANS: B
9. Character literals are enclosed in __________ and string literals are enclosed in __________.
a.
b.
c.
d.
single quotes, double quotes
double quotes, single quotes
single quotes, single quotes
double quotes, double quotes
ANS: A
10. Variables are classified according to their
a. names
b. values
c. locations
d. data types
ANS: D
11. What is the result of the following expression?
17 % 3 * 2 – 12 + 15
a. 105
b. 12
c. 7
d. 8
c. 5
d. 25
ANS: C
12. What is the result of the following expression?
10 + 5 * 3 – 20
a. -5
b. -50
ANS: C
13. In the following Java statement, what value is stored in the variable name?
String name = “John Doe”;
a.
b.
c.
d.
“name”
the memory address where “John Doe” is located
the memory address where name is located
John Doe
ANS: B
14. What is the value of z after the following statements have been executed?
int x = 4, y = 33;
double z;
z = (double) (y / x);
a. 8.25
b. 4
c. 0
d. 8.0
ANS: D
15. What output will be displayed as a result of executing the following code?
int x = 5, y = 20;
x += 32;
y /= 4;
System.out.println(“x = ” + x + “, y = ” + y);
a.
b.
c.
d.
x = 160, y = 80
x = 32, y = 4
x = 37, y = 5
x = 9, y = 52
ANS: C
16. Which of the following statements will correctly convert the data type, if x is a float and y is a
double?
a. x = float y;
b. x = y;
c. x = (float)y;
d. x = y;
ANS: C
17. Which of the following statements is invalid?
a. double r = 9.4632E15;
b. double r = 9.4632e15;
c. double r = 2.9X106;
d. double r = 326.75;
ANS: C
18. To print “Hello, world” on the monitor, which of the following Java statements should be used?
a.
b.
c.
d.
System.out.println(“Hello, world”);
System Print = “Hello, world”;
SystemOutPrintln(‘Hello, world’);
system.out.println(Hello, world);
ANS: A
19. The boolean data type may contain which of the following range of values?
a.
b.
c.
d.
-128 to + 127
true or false
-2,147,483,648 to +2,147,483,647
-32,768 to +32,767
ANS: B
20. Variables of the boolean data type are useful for
a.
b.
c.
d.
evaluating conditions that are either true or false
working with small integers
working with very large integers
evaluating scientific notation
ANS: A
21. What would be displayed as a result of executing the following code?
int x = 578;
System.out.print(“There are ” +
x + 5 + “n” +
“hens in the hen house.”);
a. There are 583
hens in the hen house.
b. There are 5785
hens in the hen house.
c. There are x5nhens in the hen house.
d. There are 5785 hens in the hen house.
ANS: B
22. What would be displayed as a result of executing the following code?
final int x = 22, y = 4;
y += x;
System.out.println(“x = ” + x + “, y = ” + y)
a. x = 22, y = 26
b. x = 22, y = 4
c. x = 22, y = 88
d. Nothing. There is an error in the code.
ANS: D
23. What would be displayed as a result of executing the following code?
int x = 15, y = 20, z = 32;
x += 12;
y /= 6;
z -= 14;
System.out.println(“x = ” + x +
“, y = ” + y +
“, z = ” + z);
a.
b.
c.
d.
x = 27, y = 3.333, z = 18
x = 27, y = 2, z = 18
x = 37, y = -14, z = 4
x = 27, y = 3, z = 18
ANS: D
24. What is the value of z after the following code is executed?
int x = 5, y = 28;
float z;
z = (float) (y / x);
a. 5.6
b. 3.0
c. 5.0
d. 5.60
ANS: C
25. Which of the following statements correctly creates a Scanner object for keyboard input?
a.
b.
c.
d.
Scanner kbd = new Scanner(System.keyboard);
Scanner keyboard = new Scanner(System.in);
Scanner keyboard(System.in);
Keyboard scanner = new Keyboard(System.in);
ANS: B
26. Which Scanner class method reads a String?
a. nextLine
b. charAt
c. nextString
d. getLine
ANS: A
27. The primitive data types only allow a(n) __________ to hold a single value.
a. class
b. literal
c. object
d. variable
ANS: D
28. In Java, __________ must be declared before they can be used.
a. variables
c. key words
b. literals
d. comments
ANS: A
29. If the following Java statements are executed, what will be displayed?
System.out.println(“The top three winners aren”);
System.out.print(“Jody, the Giantn”);
System.out.print(“Buffy, the Barbarian”);
System.out.println(“Adelle, the Alligator”);
a. The top three winners are
Jody, the Giant
Buffy, the Barbarian
Adelle, the Alligator
b. The top three winners are Jody, the GiantnBuffy, the BarbarianAdelle, and the Albino
c. The top three winners are
Jody, the GiantnBuffy, the BarbarianAdelle, the Alligator
d. The top three winners are
Jody, the Giant
Buffy, the BarbarianAdelle, the Alligator
ANS: D
30. A value that is written into the code of a program is a(n) __________.
a. literal
c. variable
b. assignment statement
d. operator
ANS: A
31. When the + operator is used with strings, it is known as the
a. assignment operator
c. addition operator
b. string concatenation operator
d. combines assignment operator
ANS: B
32. What would be printed out as a result of the following code?
System.out.println(“The quick brown fox” +
“jumped over the n”
“slow moving hen.”);
a. The quick brown fox jumped over the nslow moving hen.
b. The quick brown fox jumped over the
slow moving hen.
c. The quick brown fox
jumped over the
slow moving hen.
d. Nothing – this is an error
ANS: D
33. Which of the following is not a rule that must be followed when naming identifiers?
a. The first character must be one of the letters a-z, A-Z, and underscore or a dollar sign.
b. Identifiers can contain spaces.
c. Uppercase and lowercase characters are distinct.
d. After the first character, you may use the letters a-z, A-Z, the underscore, a dollar sign, or
digits 0-9.
ANS: B
34. Which of the following cannot be used as identifiers in Java?
a. variable names
c. key words
b. class names
d. objects
ANS: C
35. Which of the following is not a primitive data type?
a. short
c. float
b. long
d. string
ANS: D
36. Which of the following is valid?
a. float y;
y = 54.9;
b. float y;
double z;
z = 934.21;
y = z;
c. float w;
w = 1.0f;
d. float v;
v = 1.0
ANS: C
37. If x has been declared an int, which of the following statements is invalid?
a. x = 0;
c. x = 1,000;
b. x = -59832;
d. x = 592
ANS: C
38. To display the output on the next line, you can use the println method or use the __________
escape sequence in the print method.
a. n
b. r
c. t
d. b
ANS: A
39. Every Java application program must have
a. a class named MAIN
b. a method named main
c. at least two data types
d. integer variables
ANS: B
40. What will be displayed as a result of executing the following code?
int x = 6;
String msg = “I am enjoying this class.”;
String msg1 = msg.toUpperCase();
String msg2 = msg.toLowerCase();
char ltr = msg.charAt(x);
int strSize = msg.length();
System.out.println(msg);
System.out.println(msg1);
System.out.println(msg2);
System.out.println(“Character at index x = ” + ltr);
System.out.println(“msg has ” + strSize + “characters.”);
a. I am enjoying this class.
I AM ENJOYING THIS CLASS.
i am enjoying this class.
Character at index x = e
msg has 24 characters.
c. I am enjoying this class.
I AM ENJOYING THIS CLASS.
i am enjoying this class.
Character at index x = n
msg has 24 characters.
b. I am enjoying this class.
I AM ENJOYING THIS CLASS.
i am enjoying this class.
Character at index x = e
msg has 25 characters.
d. I am enjoying this class.
I AM ENJOYING THIS CLASS.
i am enjoying this class.
Character at index x = n
msg has 25characters.
ANS: D
41. What will be displayed as a result of executing the following code?
public class test
{
public static void main(String[] args)
{
int value1 = 9;
System.out.println(value1);
int value2 = 45;
System.out.println(value2);
System.out.println(value3);
value = 16;
}
}
a. 9
45
16
c. 9 45 16
b. 94516
d. Nothing. This is an error
ANS: D
Document Preview (8 of 225 Pages)
User generated content is uploaded by users for the purposes of learning and should be used following SchloarOn's honor code & terms of service.
You are viewing preview pages of the document. Purchase to get full access instantly.
-37%
Test Bank for Starting Out with Java: From Control Structures through Data Structures, 4th edition
$18.99 $29.99Save:$11.00(37%)
24/7 Live Chat
Instant Download
100% Confidential
Store
Olivia Smith
0 (0 Reviews)
Best Selling
Test Bank for Strategies For Reading Assessment And Instruction: Helping Every Child Succeed, 6th Edition
$18.99 $29.99Save:$11.00(37%)
Chemistry: Principles And Reactions, 7th Edition Test Bank
$18.99 $29.99Save:$11.00(37%)
Test Bank for Hospitality Facilities Management and Design, 4th Edition
$18.99 $29.99Save:$11.00(37%)
The World Of Customer Service, 3rd Edition Test Bank
$18.99 $29.99Save:$11.00(37%)
Data Structures and Other Objects Using C++ 4th Edition Solution Manual
$18.99 $29.99Save:$11.00(37%)
2023-2024 ATI Pediatrics Proctored Exam with Answers (139 Solved Questions)
$18.99 $29.99Save:$11.00(37%)