CSE Chapter 2 Quiz

24 July 2022
4.7 (114 reviews)
108 test answers

Unlock all answers in this set

Unlock answers (104)
question
Which of the following options declares a float variable?
answer
float age;
question
What is the result of the following code snippet? public static void main(String[] args { double circleRadius; double circleVolume = 22 / 7 * circleRadius * circleRadius; System.out.println(circleVolume); }
answer
compile-time error
question
What is wrong with the following code snippet? public class Area { public static void main(String[] args) { int width = 10; height = 20.00; System.out.println("area = " + (width * height)); } }
answer
The code snippet uses an undeclared variable.
question
What is wrong with the following code snippet? int average; average = 78A;
answer
The average variable is assigned a non-numeric value.
question
Which of the following guidelines will make code more explanatory for others?
answer
Add comments to source code
question
What will be the value stored in the variable x after the execution of the following code snippet? int a = 10; int b = 20; int c = 2; int x = b / a /*c*/;
answer
2
question
Which of the following statements with comments is(are) valid? I. int cnt = 0; /* Set count to 0 II. int cnt = 0; /* Set count to 0 */ III. int cnt = 0; // Set count to 0
answer
I and III
question
What is wrong with the following code? int count = 2000 * 3000 * 4000;
answer
Integer overflow
question
Which one of the following variables is assigned with valid literals?
answer
double salary2 = 0; salary2 = 2.96E-2;
question
What will be the value inside the variables a and b after the given set of assignments? int a = 20; int b = 10; a = (a + b) / 2; b = a; a++;
answer
a = 16, b = 15
question
What is the value inside the value variable at the end of the given code snippet? public static void main(String[] args) { int value = 3; value = value - 2 * value; value++; }
answer
-2
question
What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0;
answer
num1 = 4.20 and num2 = 47.0
question
What is the result of the following expression? double d = 2.5 + 4 * -1.5 - (2.5 + 4) * -1.5;
answer
6.25
question
What is the output of the following code snippet? public static void main(String[] args) { int value = 3; value++; System.out.println(value); }
answer
4
question
What is the output of the following code snippet? public static void main(String[] args) { int value = 25; value = value * 2; value--; System.out.println(value); }
answer
49
question
Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the following code snippet, what is the output? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the price: "); double price = in.nextDouble(); System.out.print("Enter the discount rate: "); double discount = in.nextDouble(); System.out.println("The new price is " + price - price * (discount / 100.0)); }
answer
The new price is 22.5
question
Which of the following statements is correct about constants?
answer
You can make a variable constant by using the final reserved word when declaring it.
question
Which one of the following operators computes the remainder of an integer division?
answer
%
question
What is the value of Math.pow(3, 2)?
answer
9
question
What is the output of the following code snippet? public static void main(String[] args) { double a; a = Math.sqrt(9.0) + Math.sqrt(16.0); System.out.println(a); }
answer
7.0
question
Which is the Java equivalent of the following mathematical expression? c = ?(a2 + b2)
answer
c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
question
Which one of the following is a correct representation of the given mathematical expression in Java? (a + b)/2
answer
(a + b) / 2
question
Which of the following is the Java equivalent of the following mathematical expression? c = 2 radius
answer
c = 2 * Math.PI * radius;
question
What is the result of the following statement? String s = "You" + "had" + "me" + "at" + "hello";
answer
The string s has the following value: "Youhadmeathello"
question
Which operator is used to concatenate two or more strings?
answer
+
question
What output is produced by these statements? String name = "Joanne Hunt"; System.out.println(name.length());
answer
11
question
How do you compute the length of the string str?
answer
str.length()
question
What is the output of the following code snippet? public static void main(String[] args){ { String str1; str1 = "I LOVE MY COUNTRY"; String str2 = str1.substring(4, 9); System.out.println(str2); }
answer
VE MY
question
What is the output of the following code snippet? public static void main(String[] args) { int s; double f = 365.25; s = f / 10; System.out.println(s); }
answer
36
question
Assuming that the user inputs "Joe" at the prompt, what is the output of the following code snippet? public static void main(String[] args) { System.out.print("Enter your name "); String name; Scanner in = new Scanner(System.in); name = in.next(); name += ", Good morning"; System.out.print(name); }
answer
Joe, Good Morning
question
Which one of the following refers to a number constant that appears in code without explanation?
answer
Magic number
question
What happens to the fractional part when a division is performed on two integer variables?
answer
The fractional part is discarded.
question
Consider the following division statements: I. 22 / 7 II. 22.0 / 7 III. 22 / 7.0 Which of the following is correct?
answer
I only
question
Which of the following options is valid with reference to the code snippet? public static void main(String[] args) { double d = 45.326; double r = d % 9.0; System.out.println(r); }
answer
The value inside the variable r will be 0.326
question
What is the output of the following code snippet? public static void main(String[] args) { int var1 = 10; int var2 = 2; int var3 = 20; var3 = var3 / (var1 % var2); System.out.println(var3); }
answer
There will be no output due to a run-time error.
question
Which one of the following statements gives the absolute value of the floating-point number x = -25.50?
answer
Math.abs(x);
question
Assuming that the user enters 45 and 62 as inputs for n1 and n2, respectively, what is the output of the following code snippet? public static void main(String[] args) { System.out.print("Enter a number: "); Scanner in = new Scanner(System.in); String n1 = in.next(); System.out.print("Enter another number: "); String n2 = in.next(); String result = n1 + n2; System.out.print(result); }
answer
4562
question
Which of the methods below are static methods? I. length II. Substring III. Pow IV. sqrt
answer
III and IV
question
Which one of the following statements can be used to extract the last five characters from any string variable str?
answer
str.substring(str.length() - 5, str.length())
question
Assuming that the user inputs a value of 25000 for the pay and 10 for the bonus rate in the following code snippet, what is the output? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the pay: "); double pay = in.nextDouble(); System.out.print("Enter the bonus rate: "); double bonus = in.nextDouble(); System.out.println("The new pay is " + (pay + pay * (bonus / 100.0))); }
answer
The new pay is 27500
question
What is the value of Math.abs(-2)?
answer
2
question
What is the output of the following code snippet? public static void main(String[] args) { double x; x = Math.pow(3.0, 2.0) + Math.pow(4.0, 2.0); System.out.println(x); }
answer
25.0
question
Which is the Java equivalent of the following mathematical expression? c = (?a + ?b)2
answer
c = Math.pow((Math.sqrt(a) + Math.sqrt(b)), 2);
question
Which of the following is the Java equivalent of the following mathematical expression? p = 2 (radius)3
answer
p = 2 * Math.PI * Math.pow(radius, 3);
question
How do you extract the first 5 characters from the string str?
answer
str.substring(0, 5)
question
Which of the given System.out.print statements generates the following output? ABCDE"
answer
System.out.println("ABCDE"");
question
Which of the given statements generates the following output? "///
answer
System.out.println(""///");
question
What will be the value inside the variables x and y after the given set of assignments? int x = 20; int y = 10; x = (x - y) * 2; y = x / 2;
answer
x = 20, y = 10
question
What is the value inside the var variable at the end of the given code snippet? public static void main(String[] args) { int var = 30; var = var + 2 / var; var++; }
answer
31
question
Assuming that the user enters 23 and 45 as inputs for num1 and num2, respectively, what is the output of the following code snippet? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String num1 = in.next(); System.out.print("Enter another number: "); String num2 = in.next(); System.out.println(num1 + num2); }
answer
2345
question
Which one of the following statements can be used to extract the last 10 characters from the string variable str? str.substring
answer
str.substring(str.length() - 10, str.length())
question
Which one of the following statements can be used to convert a string str to a double?
answer
double n = Double.parseDouble(str);
question
Which one of the following statements can be used to get the fifth character from a string str?
answer
char c = str.charAt(4);
question
Which one of the following statements displays the output as 54321.00?
answer
System.out.printf("%8.2f", 54321.0);
question
Which one of the following statements displays the output as (1.23e+02)?
answer
System.out.printf("%(5.2e", -123.0);
question
Which one of the following statements defines a constant with the value 123?
answer
final int MY_CONST = 123;
question
Which one of the following statements displays the output as +00321.00?
answer
System.out.printf("+%09.2f", 321.0);
question
One way to avoid round-off errors is to use:
answer
Math.round()
question
What (if any) type of error occurs with the following code if the user input is ABC? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.next(); int count = Integer.parseInt(str); System.out.println("Input is " + count); }
answer
Run-time error
question
What does the following statement sequence print? String str = "Harry"; int n = str.length(); String mystery = str.substring(0, 1) + str.substring(n - 2, n); System.out.println(mystery);
answer
Hry
question
What does the following statement sequence print? String str = "Hello"; int n = str.length(); String mystery = str.substring(0, 1) + str.substring(n - 2, n + 1); System.out.println(mystery);
answer
Run-time error
question
What does the following statement sequence print? String str = "Java Is Good"; int n = str.length(); String mystery = str.substring(n - 4, n) + str.charAt(4) + str.substring(0, 4); System.out.println(mystery);
answer
Good Java
question
What does the following statement sequence print? final String str = "Java"; str += " is powerful"; System.out.println(str);
answer
Nothing; compile-time error
question
What does the following statement sequence print? String str = "Java"; str += " is powerful"; System.out.println(str);
answer
Java is powerful
question
What does the following statement sequence print if the user input is 123? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number "); int myInt = in.nextInt(); myInt += 456; System.out.println(myInt); }
answer
579
question
What does the following statement sequence print if the user input is 123? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.next(); str += 456; System.out.println(str); }
answer
123456
question
What is the output of the following statement sequence? public static void main(String[] args) { int x = 100.0 % 6.0; System.out.println(x); }
answer
Compile-time error
question
Which statement is true?
answer
It is incorrect to initialize a string variable with a number
question
Which statement about number literals in Java is false?
answer
Integers must be positive
question
Which option represents the best choice for a variable name to represent the average grade of students on an exam?
answer
averageGrade
question
The assignment operator
answer
places a new value into a variable
question
Which of the following statements about constants in Java are true? I. Although not required, constants are commonly named using uppercase letters II. Only integer values can appear as constants III. A variable can be defined with an initial value, but the reserved word final prevents it from being changed IV. A named constant makes computations that use it clearer I, III, IV
answer
I, III, and IV
question
What is the output of this code snippet? int sum = 22; sum = sum + 2; System.out.print(sum); // sum = sum + 4; System.out.print(sum);
answer
2424
question
What is the output of this code snippet? double average; int grade1 = 87; int grade2 = 94; // System.out.print("The average is " + (grade1 + grade2) / 2.0); System.out.print("The average is " + average);
answer
Unpredictable result
question
What is the output of the following code snippet? int counter = 0; counter++; System.out.print("The initial value of the counter is "); System.out.println(count);
answer
The code will not compile
question
Which statements about numeric types in Java are true? I. There is more than one integer type II. The data type float uses twice the storage of double III. The numeric range of the Java integer type is related to powers of two
answer
I and III
question
The typical ranges for integers may seem strange but are derived from
answer
To understand the problem and its inputs and outputs
question
What is result of evaluating the following expression? (45 / 6) % 5
answer
2
question
What is the difference between the result of the following two Java statements? I. int cents = (int)(100 * price + 0.5); II. int cents = (100 * price + 0.5);
answer
Statement I compiles, but II does not
question
At what point in the problem-solving process should one write pseudocode?
answer
Before writing Java code, as a guide for a general solution
question
The problem solving process emphasizes a "first, do-it-by-hand" approach because
answer
if programmers cannot compute a solution by hand, it is unlikely they will be able to write a program that can do it
question
What is the output of the following code snippet? String firstname = "William"; String lastname; System.out.println("First: " + first); System.out.println("Last: " + lastname);
answer
Code will not compile
question
What is the correct way to invoke methods on variables in Java that are strings?
answer
Invoke them using the variable name and the dot (.) notation.
question
Suppose a phone number, stored as a ten-character string (of digits only) called phoneNumber, must be converted into a string that has parentheses around the area code. Which statement below will do that?
answer
String newNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 10);
question
Which of the following options defines an integer variable?
answer
int age;
question
Which statement is true about variable names in Java?
answer
They can contain an underscore symbol ("_")
question
Consider the following Java variable names: I. 1stInstance II. basicInt% III. empName_ IV. addressLine1 V. DISCOUNT Which of the following options is correct?
answer
III, IV, and V
question
Which is the appropriate time to initialize a variable?
answer
When you define it
question
What is the result of the following code snippet? double bottles; double bottleVolume = bottles * 2; System.out.println(bottleVolume);
answer
Does not compile
question
Which one of the following is a correct method for defining and initializing an integer variable with name value?
answer
int value = 30;
question
What is wrong with the following code snippet? int size = 42; cost = 9.99; System.out.println("size = " + size); System.out.println(" cost = " + cost);
answer
The code snippet uses a variable that has not been declared.
question
Which one of the following reserved words is used in Java to represent a value without a fractional part?
answer
int
question
In an airline reservation system, the number of available seats in an airplane is required. Which data type should be used to store this value?
answer
int
question
In an airline reservation system, the cost of an airline ticket is required. Which data type should be used to store this value?
answer
double
question
What is wrong with the following code snippet? int price; price = 9.42;
answer
The price variable is assigned a decimal value.
question
Which one of the following is an assignment statement?
answer
a = 20;
question
Which one of the following types of statements is an instruction to replace the existing value of a variable with another value?
answer
Assignment
question
What is the meaning of x = 0; in Java?
answer
It sets the variable x to zero.
question
What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0;
answer
num1 = 4.20 and num2 = 47.0
question
Which of the following statements places input into the variable value given this line of code? Scanner in = new Scanner(System.in);
answer
int value = in.nextInt();
question
Assuming that the user inputs a value of 30 for the price and 10 for the discount rate in the following code snippet, what is the output? Scanner in = new Scanner(System.in); System.out.print("Enter the price: "); double price = in.nextDouble(); System.out.print("Enter the discount rate: "); double discount = in.nextDouble(); System.out.print("The new price is "); System.out.println(price - price * (discount / 100.0));
answer
The new price is 27.0
question
Which of the following statements is correct about constants?
answer
Variables defined using final make a code snippet more readable and easier to maintain.
question
What is the value of Math.pow(2, 3)?
answer
8
question
Which one of the following is a correct representation of the given mathematical expression in Java?
answer
(a - b / 2) / 2
question
Given the definition final double PI = 3.14159; which of the following is the Java equivalent of the mathematical expression c = radius^2
answer
c = PI * Math.pow(radius, 2);
question
Which of the following is the mathematical equivalent of the following Java expression? h = (4.0 * a * b - Math.pow(b, 2)) / c;
answer
h = (4ab - b2) / c
question
Which of the following statements displays price = 20.00
answer
System.out.print("price = "); System.out.printf("%10.2f", price);
question
What is the output of the following code snippet? System.out.printf("%5.3f", 20.0);
answer
20.000