Deck 2: Fundamental Data Types

Full screen (f)
exit full mode
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*/;

A) 1
B) 2
C) 4
D) The code has a syntax error
Use Space or
up arrow
down arrow
to flip the card.
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 +

A) a = 15, b = 16
B) / 2; b = a;
A++;
B) a = 16, b = 16
C) a = 16, b = 15
D) a = 15, b = 15
Question
What is wrong with the following code snippet?
Int average;
Average = 78A;

A) The average variable is never initialized.
B) The data type for the average variable is not specified.
C) The average variable is never assigned a value.
D) The average variable is assigned a non-numeric value.
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));
}

A) The new price is 25
B) The new price is 15
C) The new price is 22.5
D) The new price is 20.0
Question
What is wrong with the following code snippet?
<strong>What is wrong with the following code snippet?  </strong> A) The code snippet uses an uninitialized variable. B) The code snippet uses an undeclared variable. C) The code snippet attempts to assign a decimal value to an integer variable. D) The code snippet attempts to add a number to a string variable. <div style=padding-top: 35px>

A) The code snippet uses an uninitialized variable.
B) The code snippet uses an undeclared variable.
C) The code snippet attempts to assign a decimal value to an integer variable.
D) The code snippet attempts to add a number to a string variable.
Question
What is wrong with the following code?
Int count = 2000 * 3000 * 4000;

A) Wrong data type
B) Variable is undefined
C) Integer overflow
D) Illegal expression
Question
Which one of the following variables is assigned with valid literals?

A) int salary = 0;
Salary = 5000.50;
B) int salary1 = 0;
Salary1 = 1.2E6;
C) double salary2 = 0;
Salary2 = 2.96E-2;
D) long salary3 = 0;
Salary3 = 1E-6;
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);
}

A) 0
B) 3.14
C) 6.28
D) compile-time error
Question
Which of the following options declares a float variable?

A) Float age;
B) flt age;
C) float age;
D) age: float;
Question
Which of the following guidelines will make code more explanatory for others?

A) Use more statements in source code.
B) Add comments to source code.
C) Avoid usage of complex calculations in source code.
D) Always enclose the statements in curly braces in source code.
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);
}

A) 25
B) 50
C) 49
D) 26
Question
Which is the Java equivalent of the following mathematical expression?
C = √(a2 + b2)

A) c = Math.sqrt(a * 2 + b * 2);
B) c = Math.sqrt(a * 2) + Math.sqrt(b * 2);
C) c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
D) c = Math.sqrt(Math.pow(a, 2)) + Math.sqrt(Math.pow(b, 2));
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++;
}

A) -2
B) 0
C) 2
D) 4
Question
What is the result of the following expression?
Double d = 2.5 + 4 * -1.5 - (2.5 + 4) * -1.5;

A) 24.375
B) 6.25
C) 12.375
D) 6
Question
Which of the following statements is correct about constants?

A) Constants are written using capital letters because the compiler ignores constants declared in small letters.
B) The data stored inside a constant can be changed using an assignment statement.
C) You can make a variable constant by using the final reserved word when declaring it.
D) Constant variables can only be changed through the Math library.
Question
Which one of the following operators computes the remainder of an integer division?

A) /
B) %
C) \
D) !
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);
}

A) 25.0
B) 337.0
C) 7.0
D) 19.0
Question
What is the output of the following code snippet?
Public static void main(String[] args)
{
Int value = 3;
Value++;
System.out.println(value);
}

A) 2
B) 3
C) 4
D) No output due to syntax error
Question
What is the value of Math.pow(3, 2)?

A) 6
B) 9
C) 8
D) 5
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

A) Only I is valid
B) I and II are valid
C) II and III are valid
D) Only III is valid
Question
Which one of the following statements can be used to extract the last five characters from any string variable str?

A) str.substring(str.length() - 5, str.length())
B) str.substring(5, 5)
C) str.substring(str.length() - 4, 5)
D) str.substring(str.length() - 5, 5)
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);
}

A) 0
B) 4
C) 20
D) There will be no output due to a run-time error.
Question
What happens to the fractional part when a division is performed on two integer variables?

A) The fractional part is rounded off to the nearest integer value.
B) The fractional part is discarded.
C) Two integers cannot be used in division; at least one of the operands should be a floating-point number.
D) Instead of using an integer division, you should use the modulus operator to perform floating-point division.
Question
Which one of the following statements gives the absolute value of the floating-point number x = -25.50?

A) abs(x);
B) Math.abs(x);
C) x.abs();
D) x.absolute();
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);
}

A) I LOV
B) OVE M
C) V
D) VE MY
Question
Which operator is used to concatenate two or more strings?

A) +
B) %
C) &
D) ^
Question
Which of the methods below are static methods?
I) length
II) substring
III) pow
IV) sqrt

A) All the methods are static
B) Only I, II and III
C) Only II and IV
D) Only III and IV
Question
Which one of the following is a correct representation of the given mathematical expression in Java?
A + b
2

A) a + b % 2
B) / 2
B) a + b / 2
C) a + (b / 2)
D) (a +
Question
Which of the following is the Java equivalent of the following mathematical expression?
C = 2 π\pi . radius

A) c = 2 * Math.PI * radius * 2;
B) c = 2 * Math.PI * Math.pow(2, radius);
C) c = 2 * Math.PI * Math.pow(radius, 2);
D) c = 2 * Math.PI * radius;
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);
}

A) The value inside the variable r will be 0.326
B) The value inside the variable r will be 5.036
C) Variable r has to be defined as an integer because the % operator always returns an integer
D) The initialization of variable r is wrong, because the % operator expects integer values as operands
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);
}

A) 36
B) 36.525
C) 37
D) No output because the code snippet generates compilation errors
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)));
}

A) The new pay is 25000
B) The new pay is 25100
C) The new pay is 27500
D) The new pay is 30000
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);
}

A) 46
B) 4662
C) 107
D) 4562
Question
What is the value of Math.abs(-2)?

A) -2
B) 0
C) 2
D) 4
Question
How do you compute the length of the string str?

A) length(str)
B) length.str
C) str.length
D) str.length()
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);
}

A) The code snippet does not compile because the += operator cannot be used in this context.
B) Joe, Good morning
C) , Good morning
D) Joe
Question
Which one of the following refers to a number constant that appears in code without explanation?

A) Constant
B) Variable
C) Magic number
D) String literal
Question
Consider the following division statements:
I) 22 / 7
II) 22.0 / 7
III) 22 / 7.0
Which of the following is correct?

A) All three statements will return an integer value.
B) Only I will return an integer value.
C) Only I, II will return an integer value.
D) Only I and III will return an integer value.
Question
What is the result of the following statement?
String s = "You" + "had" + "me" + "at" + "hello";

A) The string s has the following value: "You had me at "hello"
B) The statement results in an error because the + operator can be used only with numbers
C) The statement results in an error because the + operation cannot be performed on string literals
D) The string s has the following value: "Youhadmeathello"
Question
What output is produced by these statements?
String name = "Joanne Hunt";
System.out.println(name.length());

A) 8
B) 10
C) 9
D) 11
Question
Which one of the following statements can be used to convert a string str to a double?

A) double n = str.parseDouble();
B) double n = Integer.parseDouble(str);
C) double n = Double.parseDouble(str);
D) double n = double.parseDouble(str);
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);

A) Ha
B) Har
C) Hy
D) Hry
Question
Which is the Java equivalent of the following mathematical expression?
C = (√a + √b)2

A) c = Math.sqrt(a * 2 + b * 2);
B) c = Math.sqrt(a * 2) + Math.sqrt(b * 2);
C) c = Math.sqrt(pow(a, 2) + Math.pow(b, 2));
D) 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 . π\pi .(radius)3

A) p = 2 * Math.PI * (radius * 3);
B) p = Math.PI * Math.pow(3, radius);
C) p = 2 * Math.PI * Math.pow(radius, 3);
D) p = 2 * Math.pow(Math.PI * radius, 3);
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;

A) x = 40, y = 20
B) x = 20, y = 10
C) x = 10, y = 20
D) x = 20, y = 20
Question
How do you extract the first 5 characters from the string str?

A) substring(str, 5)
B) substring.str(0, 5)
C) str.substring(5)
D) str.substring(0, 5)
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++;
}

A) 0
B) 1
C) 30
D) 31
Question
Which of the given System.out.print statements generates the following output?
ABCDE"\

A) System.out.println("ABCDE\"\\");
B) System.out.println("ABCDE"\");
C) System.out.println("ABCDE"\);
D) System.out.println("ABCDE\"\");
Question
Which one of the following statements can be used to extract the last 10 characters from the string variable str?

A) str.substring(str.length() - 10, str.length())
B) str.substring(10, str.length())
C) str.substring(str.length() - 9, 10)
D) str.substring(0, 10)
Question
Which one of the following statements displays the output as (1.23e+02)?

A) System.out.printf("%(5.2e", -123.0);
B) System.out.printf("%5.2e", -123.0);
C) System.out.printf("^5.2e", -123.0);
D) System.out.printf("%5.2E", -123.0);
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);
}

A) Compile-time error
B) Run-time error
C) Overflow error
D) Illegal expression
Question
One way to avoid round-off errors is to use:

A) Math.sqrt()
B) Math.pow()
C) Math.round()
D) Math.truncate()
Question
Which one of the following statements can be used to get the fifth character from a string str?

A) char c = str.charAt(5);
B) char c = str.charAt(4);
C) char c = str[5];
D) char c = str[4];
Question
Which of the given statements generates the following output?
\\\"///

A) System.out.println("\\\"///");
B) System.out.println("\\\\\\\"///");
C) System.out.println("\\\\\\""//////");
D) System.out.println("\\\"///");
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);
}

A) 23
B) 4523
C) 68
D) 2345
Question
Which one of the following statements displays the output as +00321.00?

A) System.out.printf("+%09.2f", 321.0);
B) System.out.printf("%009,2f", 321.0);
C) System.out.printf("+9.2f", 321.0);
D) System.out.printf("%09.00f", 321.0);
Question
Which one of the following statements defines a constant with the value 123?

A) final int MY_CONST = 123;
B) const int MY_CONST = 123;
C) final int MY_CONST; MY_CONST = 123;
D) static int MY_CONST = 123;
Question
Which one of the following statements displays the output as 54321.00?

A) System.out.printf("%8.2f", 54321.0);
B) System.out.printf("%8,2f", 54321.0);
C) System.out.printf(",8.2f", 54321.0);
D) System.out.printf("%8.00f", 54321.0);
Question
What is the output of the following code snippet?
Public static void main(String[] args)
{
Int num1 = 10;
Int num2 = 5;
Int num3 = 200;
Num3 = num3 % (num1 * num2);
System.out.println(num3);
}

A) 0
B) 4
C) 10
D) 250
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);
}

A) 25.0
B) 34
C) 7.0
D) 14
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);
}

A) 4
B) Compile-time error
C) Run-time error
D) 16
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);

A) First: William
Last:
B) First: William
Last: lastname
C) Code will not compile
D) Unpredictable output
Question
What does the following statement sequence print?
Final String str = "Java";
Str += " is powerful";
System.out.println(str);

A) Java is powerful
B) Java + is powerful
C) is powerful
D) Nothing; compile-time error
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);

A) Unpredictable result
B) The average is 91.5
C) The average is 91.5 The average is 91.5
D) The average is 91.5 The average is 0.0
Question
The problem solving process emphasizes a "first, do-it-by-hand" approach because

A) Pseudocode is not able to capture the subtleties of complex problems.
B) it is faster to do computations by hand than to do them by computer.
C) this guarantees that programs will be correct.
D) 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 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);
}

A) 579
B) Compile-time error
C) Run-time error
D) 123456
Question
At what point in the problem-solving process should one write pseudocode?

A) After writing Java code, as a way to summarize the code's algorithm
B) Before writing Java code, as a guide for a general solution
C) After defining Java variables so that the pseudocode and data types make sense
D) Before working out examples by hand in order to guide those examples
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);

A) The initial value of the counter is 0
B) The initial value of the counter is 1
C) The code will not compile
D) The initial value of the counter is
Question
Which statement is true?

A) Variables cannot be assigned and declared in the same statement
B) Variable names must contain at least one dollar sign
C) Variable names can be no more than 8 characters long
D) It is incorrect to initialize a string variable with a number
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);

A) Run-time error
B) He
C) Ho
D) Hry
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);

A) Java
B) Good Java
C) Good
D) Is Good
Question
What does the following statement sequence print?
String str = "Java";
Str += " is powerful";
System.out.println(str);

A) Java is powerful
B) Java + is powerful
C) is powerful
D) Compile-time error
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

A) I, II, III
B) II, III, IV
C) I, III, IV
D) I, II, IV
Question
What is the correct way to invoke methods on variables in Java that are strings?

A) Methods can only be invoked on string constants, not on variables.
B) For each method there is a special operator that must be used.
C) There are no methods available in Java for string variables.
D) Invoke them using the variable name and the dot (.) notation.
Question
The first step in problem solving is

A) To write the expression that calculates the answer
B) To understand the problem and its inputs and outputs
C) To do examples by hand that confirm the solution will work
D) To write Java code that can be executed and tested
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);
}

A) 579
B) Compile-time error
C) Run-time error
D) 123456
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?

A) String newNumber = "(" + phoneNumber.substring(3, 0) + ")";
B) String newNumber = "(" + ")" + phoneNumber;
C) String newNumber = "(" + phoneNumber.substring(1, 3) + ")" + phoneNumber.substring(3, 7);
D) String newNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 10);
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);

A) 2424
B) 2425
C) 2428
D) 2528
Question
The assignment operator

A) denotes mathematical equality
B) places a new value into a variable
C) means the same as the equals sign used in algebra
D) makes it illegal to write a statement like sum = sum + 4;
Question
Which statement about number literals in Java is false?

A) Numbers in exponential notation always have type double
B) Zero is an integer
C) Integers must be positive
D) An integer with fractional part of .0 has type double.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/103
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 2: Fundamental Data Types
1
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*/;

A) 1
B) 2
C) 4
D) The code has a syntax error
B
2
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 +

A) a = 15, b = 16
B) / 2; b = a;
A++;
B) a = 16, b = 16
C) a = 16, b = 15
D) a = 15, b = 15
C
3
What is wrong with the following code snippet?
Int average;
Average = 78A;

A) The average variable is never initialized.
B) The data type for the average variable is not specified.
C) The average variable is never assigned a value.
D) The average variable is assigned a non-numeric value.
D
4
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));
}

A) The new price is 25
B) The new price is 15
C) The new price is 22.5
D) The new price is 20.0
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
5
What is wrong with the following code snippet?
<strong>What is wrong with the following code snippet?  </strong> A) The code snippet uses an uninitialized variable. B) The code snippet uses an undeclared variable. C) The code snippet attempts to assign a decimal value to an integer variable. D) The code snippet attempts to add a number to a string variable.

A) The code snippet uses an uninitialized variable.
B) The code snippet uses an undeclared variable.
C) The code snippet attempts to assign a decimal value to an integer variable.
D) The code snippet attempts to add a number to a string variable.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
6
What is wrong with the following code?
Int count = 2000 * 3000 * 4000;

A) Wrong data type
B) Variable is undefined
C) Integer overflow
D) Illegal expression
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
7
Which one of the following variables is assigned with valid literals?

A) int salary = 0;
Salary = 5000.50;
B) int salary1 = 0;
Salary1 = 1.2E6;
C) double salary2 = 0;
Salary2 = 2.96E-2;
D) long salary3 = 0;
Salary3 = 1E-6;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
8
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);
}

A) 0
B) 3.14
C) 6.28
D) compile-time error
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
9
Which of the following options declares a float variable?

A) Float age;
B) flt age;
C) float age;
D) age: float;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following guidelines will make code more explanatory for others?

A) Use more statements in source code.
B) Add comments to source code.
C) Avoid usage of complex calculations in source code.
D) Always enclose the statements in curly braces in source code.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
11
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);
}

A) 25
B) 50
C) 49
D) 26
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
12
Which is the Java equivalent of the following mathematical expression?
C = √(a2 + b2)

A) c = Math.sqrt(a * 2 + b * 2);
B) c = Math.sqrt(a * 2) + Math.sqrt(b * 2);
C) c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
D) c = Math.sqrt(Math.pow(a, 2)) + Math.sqrt(Math.pow(b, 2));
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
13
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++;
}

A) -2
B) 0
C) 2
D) 4
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
14
What is the result of the following expression?
Double d = 2.5 + 4 * -1.5 - (2.5 + 4) * -1.5;

A) 24.375
B) 6.25
C) 12.375
D) 6
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following statements is correct about constants?

A) Constants are written using capital letters because the compiler ignores constants declared in small letters.
B) The data stored inside a constant can be changed using an assignment statement.
C) You can make a variable constant by using the final reserved word when declaring it.
D) Constant variables can only be changed through the Math library.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
16
Which one of the following operators computes the remainder of an integer division?

A) /
B) %
C) \
D) !
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
17
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);
}

A) 25.0
B) 337.0
C) 7.0
D) 19.0
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
18
What is the output of the following code snippet?
Public static void main(String[] args)
{
Int value = 3;
Value++;
System.out.println(value);
}

A) 2
B) 3
C) 4
D) No output due to syntax error
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
19
What is the value of Math.pow(3, 2)?

A) 6
B) 9
C) 8
D) 5
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
20
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

A) Only I is valid
B) I and II are valid
C) II and III are valid
D) Only III is valid
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
21
Which one of the following statements can be used to extract the last five characters from any string variable str?

A) str.substring(str.length() - 5, str.length())
B) str.substring(5, 5)
C) str.substring(str.length() - 4, 5)
D) str.substring(str.length() - 5, 5)
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
22
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);
}

A) 0
B) 4
C) 20
D) There will be no output due to a run-time error.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
23
What happens to the fractional part when a division is performed on two integer variables?

A) The fractional part is rounded off to the nearest integer value.
B) The fractional part is discarded.
C) Two integers cannot be used in division; at least one of the operands should be a floating-point number.
D) Instead of using an integer division, you should use the modulus operator to perform floating-point division.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
24
Which one of the following statements gives the absolute value of the floating-point number x = -25.50?

A) abs(x);
B) Math.abs(x);
C) x.abs();
D) x.absolute();
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
25
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);
}

A) I LOV
B) OVE M
C) V
D) VE MY
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
26
Which operator is used to concatenate two or more strings?

A) +
B) %
C) &
D) ^
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
27
Which of the methods below are static methods?
I) length
II) substring
III) pow
IV) sqrt

A) All the methods are static
B) Only I, II and III
C) Only II and IV
D) Only III and IV
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
28
Which one of the following is a correct representation of the given mathematical expression in Java?
A + b
2

A) a + b % 2
B) / 2
B) a + b / 2
C) a + (b / 2)
D) (a +
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
29
Which of the following is the Java equivalent of the following mathematical expression?
C = 2 π\pi . radius

A) c = 2 * Math.PI * radius * 2;
B) c = 2 * Math.PI * Math.pow(2, radius);
C) c = 2 * Math.PI * Math.pow(radius, 2);
D) c = 2 * Math.PI * radius;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
30
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);
}

A) The value inside the variable r will be 0.326
B) The value inside the variable r will be 5.036
C) Variable r has to be defined as an integer because the % operator always returns an integer
D) The initialization of variable r is wrong, because the % operator expects integer values as operands
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
31
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);
}

A) 36
B) 36.525
C) 37
D) No output because the code snippet generates compilation errors
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
32
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)));
}

A) The new pay is 25000
B) The new pay is 25100
C) The new pay is 27500
D) The new pay is 30000
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
33
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);
}

A) 46
B) 4662
C) 107
D) 4562
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
34
What is the value of Math.abs(-2)?

A) -2
B) 0
C) 2
D) 4
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
35
How do you compute the length of the string str?

A) length(str)
B) length.str
C) str.length
D) str.length()
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
36
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);
}

A) The code snippet does not compile because the += operator cannot be used in this context.
B) Joe, Good morning
C) , Good morning
D) Joe
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
37
Which one of the following refers to a number constant that appears in code without explanation?

A) Constant
B) Variable
C) Magic number
D) String literal
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
38
Consider the following division statements:
I) 22 / 7
II) 22.0 / 7
III) 22 / 7.0
Which of the following is correct?

A) All three statements will return an integer value.
B) Only I will return an integer value.
C) Only I, II will return an integer value.
D) Only I and III will return an integer value.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
39
What is the result of the following statement?
String s = "You" + "had" + "me" + "at" + "hello";

A) The string s has the following value: "You had me at "hello"
B) The statement results in an error because the + operator can be used only with numbers
C) The statement results in an error because the + operation cannot be performed on string literals
D) The string s has the following value: "Youhadmeathello"
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
40
What output is produced by these statements?
String name = "Joanne Hunt";
System.out.println(name.length());

A) 8
B) 10
C) 9
D) 11
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
41
Which one of the following statements can be used to convert a string str to a double?

A) double n = str.parseDouble();
B) double n = Integer.parseDouble(str);
C) double n = Double.parseDouble(str);
D) double n = double.parseDouble(str);
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
42
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);

A) Ha
B) Har
C) Hy
D) Hry
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
43
Which is the Java equivalent of the following mathematical expression?
C = (√a + √b)2

A) c = Math.sqrt(a * 2 + b * 2);
B) c = Math.sqrt(a * 2) + Math.sqrt(b * 2);
C) c = Math.sqrt(pow(a, 2) + Math.pow(b, 2));
D) c = Math.pow((Math.sqrt(a) + Math.sqrt(b)), 2);
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
44
Which of the following is the Java equivalent of the following mathematical expression?
P = 2 . π\pi .(radius)3

A) p = 2 * Math.PI * (radius * 3);
B) p = Math.PI * Math.pow(3, radius);
C) p = 2 * Math.PI * Math.pow(radius, 3);
D) p = 2 * Math.pow(Math.PI * radius, 3);
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
45
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;

A) x = 40, y = 20
B) x = 20, y = 10
C) x = 10, y = 20
D) x = 20, y = 20
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
46
How do you extract the first 5 characters from the string str?

A) substring(str, 5)
B) substring.str(0, 5)
C) str.substring(5)
D) str.substring(0, 5)
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
47
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++;
}

A) 0
B) 1
C) 30
D) 31
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
48
Which of the given System.out.print statements generates the following output?
ABCDE"\

A) System.out.println("ABCDE\"\\");
B) System.out.println("ABCDE"\");
C) System.out.println("ABCDE"\);
D) System.out.println("ABCDE\"\");
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
49
Which one of the following statements can be used to extract the last 10 characters from the string variable str?

A) str.substring(str.length() - 10, str.length())
B) str.substring(10, str.length())
C) str.substring(str.length() - 9, 10)
D) str.substring(0, 10)
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
50
Which one of the following statements displays the output as (1.23e+02)?

A) System.out.printf("%(5.2e", -123.0);
B) System.out.printf("%5.2e", -123.0);
C) System.out.printf("^5.2e", -123.0);
D) System.out.printf("%5.2E", -123.0);
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
51
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);
}

A) Compile-time error
B) Run-time error
C) Overflow error
D) Illegal expression
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
52
One way to avoid round-off errors is to use:

A) Math.sqrt()
B) Math.pow()
C) Math.round()
D) Math.truncate()
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
53
Which one of the following statements can be used to get the fifth character from a string str?

A) char c = str.charAt(5);
B) char c = str.charAt(4);
C) char c = str[5];
D) char c = str[4];
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
54
Which of the given statements generates the following output?
\\\"///

A) System.out.println("\\\"///");
B) System.out.println("\\\\\\\"///");
C) System.out.println("\\\\\\""//////");
D) System.out.println("\\\"///");
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
55
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);
}

A) 23
B) 4523
C) 68
D) 2345
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
56
Which one of the following statements displays the output as +00321.00?

A) System.out.printf("+%09.2f", 321.0);
B) System.out.printf("%009,2f", 321.0);
C) System.out.printf("+9.2f", 321.0);
D) System.out.printf("%09.00f", 321.0);
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
57
Which one of the following statements defines a constant with the value 123?

A) final int MY_CONST = 123;
B) const int MY_CONST = 123;
C) final int MY_CONST; MY_CONST = 123;
D) static int MY_CONST = 123;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
58
Which one of the following statements displays the output as 54321.00?

A) System.out.printf("%8.2f", 54321.0);
B) System.out.printf("%8,2f", 54321.0);
C) System.out.printf(",8.2f", 54321.0);
D) System.out.printf("%8.00f", 54321.0);
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
59
What is the output of the following code snippet?
Public static void main(String[] args)
{
Int num1 = 10;
Int num2 = 5;
Int num3 = 200;
Num3 = num3 % (num1 * num2);
System.out.println(num3);
}

A) 0
B) 4
C) 10
D) 250
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
60
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);
}

A) 25.0
B) 34
C) 7.0
D) 14
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
61
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);
}

A) 4
B) Compile-time error
C) Run-time error
D) 16
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
62
What is the output of the following code snippet?
String firstname = "William";
String lastname;
System.out.println("First: " + first);
System.out.println("Last: " + lastname);

A) First: William
Last:
B) First: William
Last: lastname
C) Code will not compile
D) Unpredictable output
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
63
What does the following statement sequence print?
Final String str = "Java";
Str += " is powerful";
System.out.println(str);

A) Java is powerful
B) Java + is powerful
C) is powerful
D) Nothing; compile-time error
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
64
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);

A) Unpredictable result
B) The average is 91.5
C) The average is 91.5 The average is 91.5
D) The average is 91.5 The average is 0.0
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
65
The problem solving process emphasizes a "first, do-it-by-hand" approach because

A) Pseudocode is not able to capture the subtleties of complex problems.
B) it is faster to do computations by hand than to do them by computer.
C) this guarantees that programs will be correct.
D) if programmers cannot compute a solution by hand, it is unlikely they will be able to write a program that can do it.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
66
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);
}

A) 579
B) Compile-time error
C) Run-time error
D) 123456
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
67
At what point in the problem-solving process should one write pseudocode?

A) After writing Java code, as a way to summarize the code's algorithm
B) Before writing Java code, as a guide for a general solution
C) After defining Java variables so that the pseudocode and data types make sense
D) Before working out examples by hand in order to guide those examples
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
68
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);

A) The initial value of the counter is 0
B) The initial value of the counter is 1
C) The code will not compile
D) The initial value of the counter is
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
69
Which statement is true?

A) Variables cannot be assigned and declared in the same statement
B) Variable names must contain at least one dollar sign
C) Variable names can be no more than 8 characters long
D) It is incorrect to initialize a string variable with a number
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
70
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);

A) Run-time error
B) He
C) Ho
D) Hry
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
71
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);

A) Java
B) Good Java
C) Good
D) Is Good
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
72
What does the following statement sequence print?
String str = "Java";
Str += " is powerful";
System.out.println(str);

A) Java is powerful
B) Java + is powerful
C) is powerful
D) Compile-time error
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
73
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

A) I, II, III
B) II, III, IV
C) I, III, IV
D) I, II, IV
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
74
What is the correct way to invoke methods on variables in Java that are strings?

A) Methods can only be invoked on string constants, not on variables.
B) For each method there is a special operator that must be used.
C) There are no methods available in Java for string variables.
D) Invoke them using the variable name and the dot (.) notation.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
75
The first step in problem solving is

A) To write the expression that calculates the answer
B) To understand the problem and its inputs and outputs
C) To do examples by hand that confirm the solution will work
D) To write Java code that can be executed and tested
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
76
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);
}

A) 579
B) Compile-time error
C) Run-time error
D) 123456
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
77
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?

A) String newNumber = "(" + phoneNumber.substring(3, 0) + ")";
B) String newNumber = "(" + ")" + phoneNumber;
C) String newNumber = "(" + phoneNumber.substring(1, 3) + ")" + phoneNumber.substring(3, 7);
D) String newNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 10);
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
78
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);

A) 2424
B) 2425
C) 2428
D) 2528
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
79
The assignment operator

A) denotes mathematical equality
B) places a new value into a variable
C) means the same as the equals sign used in algebra
D) makes it illegal to write a statement like sum = sum + 4;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
80
Which statement about number literals in Java is false?

A) Numbers in exponential notation always have type double
B) Zero is an integer
C) Integers must be positive
D) An integer with fractional part of .0 has type double.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 103 flashcards in this deck.