These are all Java questions. Please do not answer if you are unsure of your ans
ID: 3687701 • Letter: T
Question
These are all Java questions. Please do not answer if you are unsure of your answers. Thank you.
1:A
What does the following statement sequence print?
String str = "Java";
str += " is powerful";
System.out.println(str);
Select one:
a. Java is powerful
b. Java + is powerful
c. is powerful
d. Compile-time error
2A:
Assuming that a user enters 64 as his score, what is the output of the following
code snippet?
int score = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter your score: ");
score = in.nextInt();
if (score < 40)
{
System.out.println("F");
}
else if (score >= 40 || score < 50)
{
System.out.println("D");
}
else if (score >= 50 || score < 60)
{
System.out.println("C");
}
else if (score >= 60 || score < 70)
{
System.out.println("B");
}
else if (score >= 70 || score < 80)
{
System.out.println("B+");
}
else
{
System.out.println("A");
}
Select one:
a. D
b. C
c. B
d. A
3A:
In an airline reservation system, the cost of an airline ticket is required. Which data
type should be used to store this value?
Select one:
a. int
b. byte
c. double
d. short
4A:
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);
}
Select one:
a. Compile-time error
b. Run-time error
c. Overflow error
d. Illegal expression
5A:
How many times is the text “Let's have fun with Java.” printed when this code
snippet is run?
int i = 0;
do
{
System.out.println("Let's have fun with Java.");
i++;
if (i % 2 == 0)
{
i = 10;
}
}
while (i <= 10);
Select one:
a. 1
b. 2
c. 3
d. 10
6A:
Is there any thing wrong with the following code snippet?
String[] data = { "abc", "def", "ghi", "jkl" };
String searchedValue = "ghi";
int pos = 0;
boolean found = false;
while (pos < data.length)
{
if (data[pos].equals(searchedValue))
{
found = true;
}
else
{
found = false;
}
pos++;
}
if (found)
{
System.out.println("Found at position: " + pos);
}
else
Question
22
Not yet answered
Marked out of 1.00
{
System.out.println("Not found");
}
Select one:
a. There is nothing wrong.
b. There is compile-time error.
c. There is a bounds error.
d. There is a logic error
7A:
Characters that are grouped together between double quotes (quotation marks) in
Java are called
Select one:
a. reserved words
b. syntax
c. symbols
d. strings
8A:
Characters that are grouped together between double quotes (quotation marks) in
Java are called
Select one:
a. reserved words
b. syntax
c. symbols
d. strings
9A:
Question
23
Not yet answered
Marked out of 1.00
Assuming that a user enters 22 as the price of an object, which of the following
hand-trace tables is valid for the given code snippet?
int price = 0;
String status = "";
Scanner in = new Scanner(System.in);
System.out.print("Please enter object’s price: ");
price = in.nextInt();
if (price >= 50)
{
status = "reasonable";
if (price >= 75)
{
status = "costly";
}
}
else
{
status = "inexpensive";
if (price <= 25)
{
status = "reasonable";
}
}
Select one:
a.
price
status
0
""
22
"inexpensive"
"reasonable"
b.
price
status
0
"inexpensive"
22
"reasonable"
c.
price
status
0
22
"reasonable"
"costly"
d.
price
status
0
"reasonable"
22
"costly"
10A:
Consider the following code snippet. Assuming that the user inputs 75 as the age,
what is the output?
int age = 0;
Scanner in = new Scanner(System.in);
System.out.print("Please enter your age: ");
age = in.nextInt();
if (age < 10)
{
System.out.print("Child ");
}
if (age < 30)
{
System.out.print("Young adult ");
}
if (age < 70)
{
System.out.print("Old ");
}
if (age < 100)
{
System.out.print("Impressively old ");
}
Select one:
Question
25
Not yet answered
Marked out of 1.00
a. Impressively old
b. Child Young adult Old
c. Young adult Old
d. Child Young adult Old Impressively old
11A:
Given the following method, what do we need to fix?
public static String getPerformance(char grade)
{
switch (grade)
{
case 'A': return "Excellent"; break;
case 'B': return "Good"; break;
case 'C': return "Mediocre"; break;
case 'D': return "Weak"; break;
case 'F': return "Bad"; break;
}
}
Select one:
a. Remove all the break statements
b. Add a local boolean variable definition
c. Remove all the break statements and add a return statement before the
end of method
d. Remove the switch statement and use and if statement
12A:
The enhanced for loop
Select one:
a. is convenient for traversing all elements in an array
b. is convenient for traversing elements in a partially filled array
c. is only used for arrays of integers
d. is used to initialize all array elements to a common value
13A:
Suppose that a program asks a user to enter multiple integers, either positive or
negative, to do some calculation. The data entry will stop when the user enters a
certain value to indicate the end of the data. What value should the code use as
the sentinel?
Select one:
a. 0
b. -1
c. 999
d. An alphabetic character
14A:
An if statement inside another if statement is called a
Select one:
a. switch statement
b. nested if statement
c. break statement
d. syntax error, since that is not permitted in Java
15A:
Which Java statement prints a blank line?
Select one:
a. public static void main(String[] args)
b. System.out.print();
c. System.out.println();
d. System.out.Println();
Explanation / Answer
1:A
What does the following statement sequence print? A
String str = "Java"; //Assigns the string Java to str.
str += " is powerful"; //Appends " is powerful" to str.
System.out.println(str); //So, when printed the output is "Java is powerful"
Select one:
a. Java is powerful
b. Java + is powerful
c. is powerful
d. Compile-time error
2A:
Assuming that a user enters 64 as his score, what is the output of the following
code snippet? A
int score = 0; //Initially score is 0.
Scanner in = new Scanner(System.in); //Creates a scanner object.
System.out.print("Enter your score: "); //Prompts for input.
score = in.nextInt(); //Reads an integer, and given 64 is entered.
if (score < 40) //This statement (64 < 40) evaluates to false.
{
System.out.println("F");
}
else if (score >= 40 || score < 50) //This statement (64 >= 40 || 64 < 50) evaluates to true.
{
System.out.println("D"); //So, this statement is printed, and will come out of the ladder.
}
else if (score >= 50 || score < 60)
{
System.out.println("C");
}
else if (score >= 60 || score < 70)
{
System.out.println("B");
}
else if (score >= 70 || score < 80)
{
System.out.println("B+");
}
else
{
System.out.println("A");
}
Select one:
a. D
b. C
c. B
d. A
3A:
In an airline reservation system, the cost of an airline ticket is required. Which data
type should be used to store this value? C
Select one:
a. int
b. byte
c. double
d. short
4A:
What (if any) type of error occurs with the following code if the user input is ABC? B
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); //Trying to parse the entered string "ABC" as integer and will fail.
System.out.println("Input is " + count);
}
Select one:
a. Compile-time error
b. Run-time error
c. Overflow error
d. Illegal expression
5A:
How many times is the text “Let's have fun with Java.” printed when this code
snippet is run? C
int i = 0;
do
{
//For i value of 0, this statement will be executed.
//For i value of 1, this statement will be executed again.
//For i value is 10, this statement will be executed again.
System.out.println("Let's have fun with Java.");
//i value is incremented to 1.
//Next time i value is incremented to 2.
//Next time, i value is now incremented to 11.
i++;
//1 % 2 == 0 condition fails.
//Second time, i value is 2 and 2 % 2 == 0 condition satisfies.
//Third time, i value is 11 and 11 % 2 == 0 condition fails.
if (i % 2 == 0)
{
i = 10; //i value will be updated to 10.
}
}
while (i <= 10);
//i value is 1, and is <= 10 is true.
//Second time, 10 <= 10 is again true.
//Third time, 11 <= 10 is false.
//So, the statement will be executed for 3 times.
Select one:
a. 1
b. 2
c. 3
d. 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.