Java test 1
Which of the following literals is NOT valid? '\n' '"' ""\n"" "\"\n"
""\n"", This is not valid because the double quotes inside the string literal must be represented using escape sequences.
What operator is used to perform string concatenation in Java?
+
If the variable num contains a positive integer, what are the only two possible results of the following expression? num % 2 -1 and 1 0 and 2 1 and 2 0 and 1
0 and 1
After the following line of code is executed, the value stored in the variable num will be in what range? num = (int) (Math.random() * 25); 0 to 25 0 to 24 1 to 24 1 to 25
0 to 24
2 If gen refers to a Random object, the following expression will produce a value in what range? gen.nextInt(30)
0 to 29
What is the result of the expression Math.floor(12.843)
12.0, returns it to the base of the number
What is the result of the expression Math.max(6, 17)?
17, this method returns the greater of the two numbers
If a String variable called pioneer contains the characters "Grace Murray Hopper", what is the result of the following expression? pioneer.length()
19, bc thats the number of characters in this expression
Which of the following is NOT a valid Java identifier? -first_place -1stPlace -place1 -FIRST_PLACE
1stPlace, because it starts with a digit
What result is printed by the following code? System.out.println(15 + 30);
45
What is the result of the expression Math.round(8.25)?
8, this rounds to 8
3 If gen refers to a Random object, the following expression will produce a value in what range? gen.nextInt(10) + 5
5 to 14, range of (10) is 9, then 9+5= 14 use 5 as the starting point
If the current value of the integer variable num is 4, what value will be stored in num after the following line of code is executed? num += 2;
6
If the current value of the integer variable size is 2, what value will be stored in size after the following line of code is executed? size *= 3;
6
What is a natural language?
A language that humans use to communicate, such as English.
What is the Java API?
A large library of classes
Which of the following will produce a syntax error?
Omitting a necessary semicolon in a program
Java was created by the Oracle Corporation, and is now managed by Sun Microsystems.
false sun then oracle
There is no way to stop an infinite loop
false,
What value is assigned to the integer variable num by the following statement if the current value of num is 4? num = num * 2
8
if a String variable called address contains the characters "123 Main Street", what is the result of the following expression? address.indexOf('3')
2
What is the result of the expression Math.pow(3, 3)?
27, 3 to the 3rd power
What is the result of the following Java expression? 43 % 5
3
If the current value of the integer variable count is 2, what value will be stored in count after the following line of code is executed? count += count * 3;
8, because pemdas you multiply 2*3 then add 2
If the current value of the integer variable num1 is 2 and the current value of the double variable num2 is 3.0, what value will be stored in num2 after the following line of code is executed? num2 /= num1 * num2;
3.0/2*3.0 = .5
What issue must be addressed when printing a long character string?
A character string literal cannot span across multiple lines. So you concatenate shorter strings into one long string.
What is a programming convention?
A guideline about programming style that makes code easier to read
The format method of the String class accepts the same parameters as the printf method.
true, it returns the formatted string it just doesnt print it
What does the following line of code do? System.out.println("Your age in 7 years: " + (19 + 7));
Adds 19 to 7, concatenates the result to the string, then prints it.
Which of the following is NOT a constant declared in the Java API? Math.E Conversions.KILOMETERS_PER_MILE Integer.MAX_VALUE Math.PI
Conversions.KILOMETERS_PER_MILE, There is no such constant in the Java API (and no class called Conversions for that matter).
Which of the following statements is true? A. The println statement sends text output to the editor. B. The name println is pronounced "print long." C. A println statement must be the first statement in a Java program. D. The println statement is a call to a method.
D. The println statement is a call to a method. a: false, sends it to the console window b. false, its print line c. false, class is the first statement
Which of the following will produce a runtime error?
Dividing by zero.
3 An object must be created when its object reference variable is declared.
False, those can be separate
What does an import statement do?
Identifies the package in which a class can be found.
What is the role of the seed value of a random number generator?
It's a number that is the basis of the calculated pseudorandom numbers.
The Java Virtual Machine is software
true, its not a machine
The slogan "Write Once, Run Anywhere" is used to promote what?
Java's cross-platform benefits, A Java program can be run on any platform with a Java Virtual Machine (JVM)
Which of the following identifiers follow the naming convention for a Java constant? maxPenalties Max_Penalties MAX_PENALTIES MAXPENALTIES
MAX_PENALTIES
What output would be produced when the following code is executed? String state = "Mississippi"; System.out.println(state.replace("is", "was"));
Mwasswassippi
2 If a String variable called greeting contains the characters "hello", what does the following expression do? greeting.toUpperCase()
Returns a new string containing the characters "HELLO"
What is syntax coloring?
Showing certain elements of program code in different colors
Which statement would produce the following output? Eeny Meeny Miny Moe
System.out.println("Eeny\n\tMeeny\nMiny\n\tMoe");
Which statement would produce the following output? "Oom Pah Pah Oom Pah Pah," that's how it goes.
System.out.println("\"Oom Pah Pah Oom Pah Pah,\" that's how it goes.");
Which of the following statements is true? -Blank lines in a Java program will be displayed in the output. -The body of a Java class is enclosed in square brackets: [ ] -The body of a Java method is enclosed in curly braces: { } -The println method is called through the System.in object.
The body of a Java method is enclosed in curly braces: { } the last one is wrong bc the println method is called through the println object
Which of the following statements is true? An if statement is an example of a repetition statement. The body of an if statement must be enclosed in braces. An if statement must contain an else clause. The condition of an if statement must be a boolean expression.
The condition of an if statement must be a boolean expression.
What happens to the current value of the integer variable size when the following statement is executed? size = 12;
The current value is overwritten by the new value.
What is wrong with the following code? if (count > 5) if (sum < MAX) System.out.println("cattywampus"); else System.out.println("snickersnee")
The indentation does not properly reflect the processing. this is the dangling clause problem
What is the syntax of a language?
The rules that determine how words and symbols can be combined.
What will happen if the variable total holds 5 when the following code is executed? if (total > 8) System.out.println("collywobbles")
The word collywobbles is not printed and processing continues.
A static import let's you refer to a static method without using its class name.
True
In a programming language, a syntactically valid statement has only one meaning.
True thats why we dont use language like English
What output does the following code produce? System.out.print("apple"); System.out.println("banana"); System.out.println("orange");
apple banana orange
what are the 8 primitive data types?
byte, short, int, long, float, double, char, boolean
What software tool is used to translate Java source code into Java bytecode?
compiler
What components are part of the von Neumann model?
control unit, arithmetic logic unit, memory, input and output
Two classes can have the same name if they are in different packages.
true, so each class doesnt have to be unique
What line of code would be equivalent to the following? depth += 50 * offset;
depth= depth + 50 * offset;
In a program, a class must always be referred to using its fully qualified name.
false
The not operator (!) requires two operands.
false
When reading input using a Scanner object, each input value must be on its own line.
false
A program that runs without generating a runtime error will produce correct results.
false It could still produce incorrect results.
Java programs cannot be run on a web page.
false can be run on any computer with java virtual machine
The Java Development Kit (JDK) is an example of an integrated development environment (IDE)
false its a command-line environment
If both operands to the remainder operator (%) are positive, a divisor of n will produce a result in the range 1 to n.
false, It will produce a result in the range 0 to n-1.
Information in secondary memory is lost if the power is turned off.
false, Secondary memory like a hard drive is nonvolitile, meaning it retains its information when power is lost.
John von Neumann is known for the observation that the number of transistors that fit on a computer chip doubles
false, That's Moore's Law, which was proposed by Intel co-founder Gordon Moore in 1965.
The exclusive or operator (^) produces results that are opposite to the regular or operator (||).
false, The only difference is when both operands are true.
when a method is called, the type and value of each argument are included in parentheses.
false, The types of the arguments are only included in the method declaration, not when calling it.
Using the Java API in a program is a rare occurrence.
false, Using the Java API in a program is a rare occurrence.
Java constants cannot be created for floating-point values.
false, You can make a constant out of any data type in Java.
A constant should not be declared with public visibility
false, because nobody can change it
The float and long data types both represent floating-point values.
false, float and double. long represents int
The return type of a method must either be a Java primitive type or void.
false, it can also return a method
A printf format specifier of %.3f will print a floating-point value truncated to 3 decimal places.
false, it will round it to 3 decimal places
1 The Java keyword new is a method that can be called to create objects.
false, its a keyword not a method
1 The String class is part of the java.util package.
false, its part of the java.lang``
Characters in Java are represented using the ASCII character set.
false, its unicode
The classes of the java.util package are automatically imported into every Java program.
false, java.lang is
The relational operators can be used to put String objects in alphabetical order.
false, objects such as < cannot be applied to objects
The end-of-line block style aligns opening and closing braces vertically.
false, thats aligned
The arithmetic operators have a lower precedence than the relational operators.
false, the other way around
There are exactly six primitive data types in Java.
false, there are 8
Internally, an upper-case letter ('A') is treated the same as its lower-case version ('a').
false, they're treated as different
The methods of the Math class are called through the class name because they are abstract.
false, theyre static
The less than operator (<) should not be used to compare floating-point values.
false, you can. the danger just comes from comparing them for equality
A multi-line Java comment begins with a double slash (//).
false, // is a single line comment multi-line is a /*
the byte data type represents a true or false value
false, boolean is true or false
A runtime error occurs when a printf field width is insufficient to display a value.
false, it grows to accommodate the larger value
A type cast is used to change the data type of a variable.
false, it only causes values to be treated as another type ONLY for the current expression
The assignment operator has higher precedence than the arithmetic operators.
false, the = has the lowest precedence of all
A String object cannot be created with a new operator
false, you can use new but a string literal will suffice
The expression x^y raises the value x to the power y.
false, Math.pow(x, y) raised x to the power y
An import statement should be written inside a class but before any methods.
false, before the class
If A is false, then the B operand in the expression A || B is not evaluated.
false, it needs to be evaluated
When reading input using a Scanner object, if the type of input does not match the expected type, the compiler will issue an error message.
false, this problem cant be detected during compiling bc its a runtime error
What is the output of the following statement? System.out.println("He said \"Avada Kedavra\"");
he said "Avada Kedavara"
Of the options given, what values of the variables height, size, and width would cause the following code to set the variable weight to 100? if (height < size) { weight = 50; if (width < 20) System.out.println("short"); } else { if (width > 100) weight = 100; System.out.println("tall");
height = 15, size = 10, width = 110
Which of the following identifiers follows the convention for naming Java variables? -totalValue -total_value -TotalValue -TOTALVALUE
totalValue, camelCase
A String method can be called through a string literal.
true
Suppose the integer variable hours contains a value that represents a number of hours. Which of the following expressions will compute how many 24-hour periods are represented by that value, without worrying about any leftover hours. hours / 24 hours % 24 hours / 24 + 1 hours % 24 + 1
hours / 24 Integer division throws the amount leftover away.
Which of the following is NOT a valid variable declaration in Java? float height = 20.0, length = 30.5; double total; String greeting = "Hi there!"; int first = true;
int first = true; you cannot assign a boolean statement to a int statement
A typical Java compiler produces which type of output?
java bytecode
What output is printed by the following code if it is executed when appetizer is 3 and entree is 12? if (appetizer > 1) if (entree < 7) System.out.println("ketchup"); else System.out.println("mustard"); else if (appetizer < 0) System.out.println("mayonnaise"); else System.out.println("relish");
mustard
Given the following declarations, which assignment statement would cause the compiler to issue an error? int num1 = 5, num2 = 3500; double num3 = 7.29;
num2 = num3; because a double value cannot be assigned to an integer variable without a cast
Which of the following is NOT a Java primitive type? -int -double -boolean -number
number
What are the two categories of data types in Java?
primitive data and object references
The main method of a Java program must have which of the following method headers? -public static void main(String[] args) -public abstract void main(String[] args) -public static int main() -private abstract void main()
public static void main(String[] args)
By default, what characters are used to separate input tokens when reading input using a Scanner object?
spaces, tabs, and newline characters
What should you pass to the Scanner constructor if you want to create a Scanner object that reads input typed at the keyboard.
system.in
What does the term case sensitive mean?
the difference between upper-case and lower-case letters matters.
What do the characters in a Unicode escape sequence represent?
the hexadecimal code for a particular Unicode character
What is debugging?
the process of determining the root cause of a problem and fixing it.
What output does the following code produce? System.out.print("Total: " + 100 + 20);
total: 10020
A comment in a Java program is ignored by the compiler.
true
A constant is created using the final modifier.
true
A constructor of a class has the same name as the class.
true
A method declaration includes parentheses after the method name, even if the method takes no parameters.
true
A method that has a return type of void does not have to have a return statement.
true
A variable can be declared inside the body of a method.
true
A widening conversion converts a primitive value to a larger primitive type.
true
An import statement tells the compiler which class you're referring to in a program.
true
Attempting to follow a null reference will cause an exception to be thrown.
true
If either or both operands to the division operator (/) are floating-point values, then the result will be a floating-point value.
true
If the compiler produces errors, it will not generate an executable version of the program.
true
If the type of the expression in a return statement does not match the return type of the method, the compiler will issue an error message.
true
It is usually not a good idea to explicitly return a true or false literal value in a method that returns a boolean.
true
The Math.PI constant represents pi to 15 decimal places.
true
The expression count++ can stand as a statement on its own.
true
The field width in a printf format specifier determines the total number of characters to be used to print the value.
true
The first parameter to the printf method is the format string.
true
The int and short data types both represent integers.
true
The next and nextLine methods of the Scanner class both read and return character strings.
true
When it comes to programming style, consistency is crucial.
true
full documentation about the Java API can be found online.
true
in some cases, a sentinel value can be used to signal the end of input.
true
Java was created by a development team led by James Gosling.
true "green team"
Java is a general-purpose programming language.
true can run on all kinds of devices
Java performs automatic garbage collection.
true it cleans up behind itself
Java is an object-oriented programming language.
true object is the fundamental programming element
Classes in the Java API are organized into packages.
true, A package is a collection of related classes.
A numeric literal is an explicit number used in a program.
true, As opposed to a variable or a named constant.
If A and B are both false, then the expression A && B is false.
true, Both operands have to be true for A && B to be true.
The printf method accepts a variable number of parameters.
true, It depends on the number of format specifiers you use.
Java source code cannot be executed directly by a computer.
true, It must first be translated into an executable form which is bytecode
Constants make maintenance tasks easier.
true, They also convey more meaning than literals and help prevent inadvertent errors.
Numeric primitive values in Java are signed.
true, each is either positive or negative
The body of a while loop may never execute.
true, if its wrong it wont run
A printf format specifier changes the value in the corresponding parameter.
true, it only changes the corresponding parameter not the original parameter
The result of a relational operator can be assigned to a boolean variable.
true, the relational operators all produce boolean results.
An entire package can be imported using one import statement.
true, using the wildcard * character
The term hertz can be used to describe a computer's speed.
true, It is an indicator of how many instructions can be performed every second.
The Math class contains a constant that represents the value pi to several digits.
true, its Math.PI it also has a constant for e, the base of the natural log.
Given the following declaration, which expression would produce the substring "ledge"? String words = "Knowledge is power.";
words.substring(4, 9);