CSC 1350 Exam #2 Questions and
Answers
What are the two parts of an if statement?
a) An increment and a return value
b) An increment and a body
c) A check and an increment
d) A condition and a body - Correct Answers-d) A condition and a body
Which of the following statements is true about the if statement?
a) The if and else blocks should always be included within curly braces
b) The else block is optional
c) The if statement can have only one condition that evaluates to an integer value
d) The if block is optional - Correct Answers-b) The else blocks is optional
The following code snippet contains an error. What is the error?
if (cost > 100); {
cost = cost -10;
}
System.out.println("Discount cost: " + cost);
a) Logical error: use of an uninitialized variable
b) Logical error: if statement has do-nothing statement after if condition
c) Syntax error (won't compile)
d) Logical error: assignment statement does not show equality - Correct Answers-b)
Logical error: if statement has do-nothing statement after if condition
What is the output of the following code snippet?
int num = 100;
if (num > 100); {
num = num - 10;
}
System.out.println(num);
a) 100
b) 101
c) 90
d) 99 - Correct Answers-c) 90
The operator !> stands for ____________________.
a) Not greater than
b) This is not an operator in Java.
,c) Not less than
d) Not equal to - Correct Answers-b) This is not an operator in Java.
What is the output of the following code snippet?
int x = 50;
if (x > 100) {
x++;
}
else {
x--;
}
System.out.println(x);
a) 50
b) 51
c) 49
d) 52 - Correct Answers-c) 49
Assuming that the user enters 60 as the input, what is the output after running the
following code snippet?
int num = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
if (num < 10) {
System.out.println("Too small!");
}
else if (num < 50) {
System.out.println("Intermediate!");
}
else if (num < 100) {
System.out.println("High!");
}
else {
System.out.println("Too high!");
}
a) Too high!
b) Intermediate!
c) High!
d) Too small! - Correct Answers-c) High!
What is the output after running the following code snippet?
int number = 600;
if (number < 200) {
System.out.println("Low spender");
}
else if ( number < 500) {
, System.out.println("Spending in moderation");
}
else if (number < 1000) {
System.out.println("Above average!");
}
else {
System.out.println("High Roller!");
} - Correct Answers-b) Above average!
An if statement inside another if statement is called a ______________________.
a) break statement
b) syntax error, since that is not permitted in Java
c) switch statement
d) nested if statement - Correct Answers-d) nested if statement
Assuming that a user enters 10, 20, and 30 as input values one after another, separated
by spaces, what is the output of the following code snippet?
int num1 = 0;
int num2 = 0;
int num3 = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num1 = in.nextInt();
System.out.print("Enter a number: ");
num2 = in.nextInt();
System.out.print("Enter a number: ");
num3 = in.nextInt();
if (num1 > num2) {
if (num1 > num3) {
system.out.println(num1);
}
else {
System.out.println(num3);
}
}
else {
if (num2 > num3) {
System.out.println(num2);
}
else {
System.out.println(num3);
}
}
a) 0
b) 10
c) 20
Answers
What are the two parts of an if statement?
a) An increment and a return value
b) An increment and a body
c) A check and an increment
d) A condition and a body - Correct Answers-d) A condition and a body
Which of the following statements is true about the if statement?
a) The if and else blocks should always be included within curly braces
b) The else block is optional
c) The if statement can have only one condition that evaluates to an integer value
d) The if block is optional - Correct Answers-b) The else blocks is optional
The following code snippet contains an error. What is the error?
if (cost > 100); {
cost = cost -10;
}
System.out.println("Discount cost: " + cost);
a) Logical error: use of an uninitialized variable
b) Logical error: if statement has do-nothing statement after if condition
c) Syntax error (won't compile)
d) Logical error: assignment statement does not show equality - Correct Answers-b)
Logical error: if statement has do-nothing statement after if condition
What is the output of the following code snippet?
int num = 100;
if (num > 100); {
num = num - 10;
}
System.out.println(num);
a) 100
b) 101
c) 90
d) 99 - Correct Answers-c) 90
The operator !> stands for ____________________.
a) Not greater than
b) This is not an operator in Java.
,c) Not less than
d) Not equal to - Correct Answers-b) This is not an operator in Java.
What is the output of the following code snippet?
int x = 50;
if (x > 100) {
x++;
}
else {
x--;
}
System.out.println(x);
a) 50
b) 51
c) 49
d) 52 - Correct Answers-c) 49
Assuming that the user enters 60 as the input, what is the output after running the
following code snippet?
int num = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
if (num < 10) {
System.out.println("Too small!");
}
else if (num < 50) {
System.out.println("Intermediate!");
}
else if (num < 100) {
System.out.println("High!");
}
else {
System.out.println("Too high!");
}
a) Too high!
b) Intermediate!
c) High!
d) Too small! - Correct Answers-c) High!
What is the output after running the following code snippet?
int number = 600;
if (number < 200) {
System.out.println("Low spender");
}
else if ( number < 500) {
, System.out.println("Spending in moderation");
}
else if (number < 1000) {
System.out.println("Above average!");
}
else {
System.out.println("High Roller!");
} - Correct Answers-b) Above average!
An if statement inside another if statement is called a ______________________.
a) break statement
b) syntax error, since that is not permitted in Java
c) switch statement
d) nested if statement - Correct Answers-d) nested if statement
Assuming that a user enters 10, 20, and 30 as input values one after another, separated
by spaces, what is the output of the following code snippet?
int num1 = 0;
int num2 = 0;
int num3 = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num1 = in.nextInt();
System.out.print("Enter a number: ");
num2 = in.nextInt();
System.out.print("Enter a number: ");
num3 = in.nextInt();
if (num1 > num2) {
if (num1 > num3) {
system.out.println(num1);
}
else {
System.out.println(num3);
}
}
else {
if (num2 > num3) {
System.out.println(num2);
}
else {
System.out.println(num3);
}
}
a) 0
b) 10
c) 20