JAVA CH 3&4
What symbol is used to indicate a format specifier for output in Java? & % $ #
%
Suppose you want to output a double value using System.out.printf in Java so that the value is displayed in a field that is 12 characters wide, rounds to 2 decimal places, and is left-aligned. What would be the correct format specifier? %12.2f %-f12.2 %f12.2 %-12.2f
%-12.2f
Suppose you want to output an integer value using System.out.printf in Java so that the value is displayed in a field that is 15 characters wide and is right-aligned. What would be the correct format specifier? %15d %d15 %-d15 %-15d
%15d
Suppose you want to output an integer using System.out.printf in Java so that the integer is displayed in a field that is 6 characters wide and is right-aligned. What would be the correct format specifier? %-6d %6d %d6 %-6d
%6d
Calculate the value of z after the following Java code executes: public static void main(String[] args) { // TODO Auto-generated method stubint x = 8;int y = 6; int z = 0; if ((x <= 7) || (y > 6))z = x + y; else if ((x > 7) && (y < 5)) z = x - y; else if ((x <= 7) || (y != 6)) z = x * y; elsez = 1; System.out.println("z = " + z);}
1
Calculate the value of z after the following Java code executes: public static void main(String[] args) { // TODO Auto-generated method stub int x = 5; int y = 6; int z = 0; if ((x <= 7) && (y > 5)) z = x + y; else if ((x > 7) || (y < 5)) z = x - y; else if ((x <= 7) && (y < 6)) z = x * y; else z = 1; System.out.println("z = " + z); 11 -1 30 1
11
Calculate the value of z after the following Java code executes: public static void main(String[] args) { // TODO Auto-generated method stubint x = 6;int y = 3;int z = 0; if ((x <= 7) && (y > 5)) z = x + y; else if ((x > 7) || (y < 5)) z = x - y; else if ((x <= 7) && (y < 6)) z = x * y;elsez = 1; System.out.println("z = " + z);}
3
Suppose you have declared a double variable called z in a Java program. Which is the proper way to set z equal to 5 raised to the third power by using Java's pow method? z = pow(5,3); z = pow(3,5) z = Math.pow(5,3); z = Math.pow(3,5);
z = Math.pow(5,3);
When the following Java code executes: String word = "Skywalker"; String sub = word.substring(6,9); what is the value of sub? ker lke lker
ker
Which Java input method is generally used for strings that contain whitespace characters? next() nextLine() both of them Neither of them
nextLine
Which of the following can be used to initialize the value of a parameter when a method is called? A numeric literal , A variable, A mathematical expression. All of the above
All of the above
In Java, strings are immutable. (T or F)
False
After the following Java code executes: int x = 7; int y = 5; int z = 4; if ((x != z) && (y < x) || (z > y)) System.out.println("Angels"); else System.out.println("Dodgers"); the output will be Dodgers. (T or F)
False ((7 != 4) && (5 < 7) || (4 > 5) True AND True OR False True OR False True Boolean expression is True, so Angels is output.)
In a Java if-else statement, the keywords if and else must always be on different lines of code and never on the same line. (T or F)
False (You are allowed to put if and else on the same line of code in Java.)
Given the following Java boolean expression:(x >= 3) || (y < 6)If x = 2 and y = 6, is the expression true or false? (T or F)
False ((2 >= 3) || (6 < 6) FALSE || FALSE So it's FALSE)
After the following Java code executes: int x = 7; int y = 8; int z = 7; if ((x > z) && (y <= x) || (z >= y) System.out.println("Angels"); else System.out.println("Dodgers"); the output will be Angels. (T or F)
False ((7 > 7) AND (8 <= 7) OR (7 >= 8) False AND False OR False False OR False False)
A method in Java is only allowed to have no more than one parameter defined for it.
False (A method in Java can have as many parameters as you want.)
After the following Java code executes: String word = "Skywalker"; char letter = word.charAt(3); the value of letter will be 'y' (T or F)
False (False First index in a string is 0, so letter will be "w:)
To display a floating-point number in Java with a field length of 11 characters and 3 characters after the decimal point, the format specifier would be: %8.3f (T or F)
False (It would be %11.3f)
In Java, the index value of the first character of a string object is 1. (T or F)
False (It's 0)
A char variable in Java can have methods called directly from it, just like a String variable. (T or F)
False (String variables can do this because String is an object. However, char is a primitive type, so it can't.)
Which of the following can parameters of primitive types be used for in Java?
Sending values into a method
The test condition in an if-else statement in Java must be enclosed in __________.
Parentheses
When the following Java code executes: String word = "Skywalker"; String sub = word.substring(2,6); what is the value of sub? ywalk kywa ywal kywal
ywal
Given the following Java code, double x = Math.round(7.5); the value of the variable x will be 7.
The value of x will be 8
Value of char values in Java are allowed to be compared using comparison operators (<, >, <=, >=, ==, !=). (T or F)
True
When the following Java code executes: String word = "Barcelona"; String part = word.substring(4,9); the value of part will be "elona". (T or F)
True
In Java, a method with any return type other than ________ must contain a return statement.
Void
When calling a method that has multiple parameters in Java, the call must have values for__________________.
all the parameters in the method definition in the correct order.
In Java, if you want to call a method contained in a class object, what symbol must go between the object name and the method name? semicolon, comma, colon dot
dot
After the following Java code executes: int x = 7; int y = 8; int z = 4; if ((x != z) && (y < x) || (z > y)) System.out.println("Angels"); else System.out.println("Dodgers"); the output will be Dodgers.
false ((7 != 4) AND (8 < 7) OR (4 > 8) True AND False OR False False OR False False The boolean expression is False, so Dodgers is displayed.)
