CS 1400 FINAL REVIEW QUESTIONS WITH VERIFIED
ANSWERS
True or False, the best way to make a single line comment is by using the /** **/
comment style. - Answers - False, it is //
Which of the following commands will generate the output below?
Hello World! - Answers - System.out.print("Hello World!");
Which of the following best describes how troubleshooting and correcting compiling
errors should be approached? - Answers - Fix one error at a time and work your way to
fixing all compiling errors.
True or False, Computer programming jobs are becoming few and far between because
machines have taken over the task of creating and maintaining current applications. -
Answers - False.
Part of being a good programmer includes the ability to effectively troubleshoot and
solve problems. - Answers - True.
What are two valid numerical variable types? - Answers - double and int
Which of the following is a valid way in Java of assigning the variable x with the value of
x + 3? - Answers - x = x + 3;
Consider the following code:
import java.util.Scanner;
public class GetUsersName {
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
??? inputUserName;
System.out.print("Please enter your name: ");
inputUserName = scnr.nextLine();
System.out.println("Hello " + inputUserName);
}
}
Which of the following variable types should be used in this scenario where the ???s
are located? - Answers - String
Which is a shorthand way of subtracting 5 from the value already assigned to variable
x? - Answers - x -= 5;
,Which of the following is a valid value that can be assigned to a Boolean variable? -
Answers - true
Consider the following code:
int userAge = 60;
double discountPercentage = 0.0;
if (userAge > 60)
{
discountPercentage = 0.15;
}
else if (userAge < 60)
{
discountPercentage = 0.10;
}
discountPercentage *= 100;
System.out.println("The discount percentage is " + discountPercentage + "%");
What will the discount percentage value be that is displayed to the user? - Answers -
0.0%
Would you say the code in the previous question is operating correctly? Are there any
syntax or logic errors, or is the code complete and correct? - Answers - The code has
one or more logic errors.
It doesn't make sense.... think about it... if you are under 60 you get 10% off, if you are
above 60 you get 15%, and if you are 60 you get no discount. that's weird right?
True or false.
Multiple if statements can be nested inside each other. - Answers - True.
True or false.
If statements must contain either an else or an else if, otherwise a compiling error will
occur. - Answers - False.
Write an if statement that checks if a person is exactly 16 years old. If the statement
evaluates to true, print a line stating "Happy Sweet 16!". You can use the variable
userAge to write the statement. You do not need to write the statements necessary to
declare the variable or capture the input (unless you want to show off). - Answers - int
userAge = 16;
if(userAge == 16) {
System.out.println("Happy Sweet 16!");
, }
True or false.
Loops can only work with positive values. No negative values can be used. - Answers -
False.
A(n) ___________ is the word used to describe each cycle or repetition through a loop.
- Answers - iteration
True or false.
Infinite loops are usually favorable because they ensure that the program will continue
running and operating as intended. - Answers - False.
What is the name of the value that can be used to immediately end or terminate a loop?
- Answers - sentinel
When the number of times a loop should run is a known number, a for loop is the better
option. - Answers - True.
Which term accurately describes placing one loop inside of another loop? - Answers -
Nesting
Write a for loop that will run 5 times and display the following output:
0
1
2
3
4 - Answers - for (int i = 0; i < 5; i++)
{
System.out.println(i);
}
Write a while loop that will run 5 times and display the following output:
5
4
3
2
1 - Answers - Scanner scnr = new Scanner(System.in);
int x = 5;
while (x >= 1)
{
System.out.println(x);
ANSWERS
True or False, the best way to make a single line comment is by using the /** **/
comment style. - Answers - False, it is //
Which of the following commands will generate the output below?
Hello World! - Answers - System.out.print("Hello World!");
Which of the following best describes how troubleshooting and correcting compiling
errors should be approached? - Answers - Fix one error at a time and work your way to
fixing all compiling errors.
True or False, Computer programming jobs are becoming few and far between because
machines have taken over the task of creating and maintaining current applications. -
Answers - False.
Part of being a good programmer includes the ability to effectively troubleshoot and
solve problems. - Answers - True.
What are two valid numerical variable types? - Answers - double and int
Which of the following is a valid way in Java of assigning the variable x with the value of
x + 3? - Answers - x = x + 3;
Consider the following code:
import java.util.Scanner;
public class GetUsersName {
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
??? inputUserName;
System.out.print("Please enter your name: ");
inputUserName = scnr.nextLine();
System.out.println("Hello " + inputUserName);
}
}
Which of the following variable types should be used in this scenario where the ???s
are located? - Answers - String
Which is a shorthand way of subtracting 5 from the value already assigned to variable
x? - Answers - x -= 5;
,Which of the following is a valid value that can be assigned to a Boolean variable? -
Answers - true
Consider the following code:
int userAge = 60;
double discountPercentage = 0.0;
if (userAge > 60)
{
discountPercentage = 0.15;
}
else if (userAge < 60)
{
discountPercentage = 0.10;
}
discountPercentage *= 100;
System.out.println("The discount percentage is " + discountPercentage + "%");
What will the discount percentage value be that is displayed to the user? - Answers -
0.0%
Would you say the code in the previous question is operating correctly? Are there any
syntax or logic errors, or is the code complete and correct? - Answers - The code has
one or more logic errors.
It doesn't make sense.... think about it... if you are under 60 you get 10% off, if you are
above 60 you get 15%, and if you are 60 you get no discount. that's weird right?
True or false.
Multiple if statements can be nested inside each other. - Answers - True.
True or false.
If statements must contain either an else or an else if, otherwise a compiling error will
occur. - Answers - False.
Write an if statement that checks if a person is exactly 16 years old. If the statement
evaluates to true, print a line stating "Happy Sweet 16!". You can use the variable
userAge to write the statement. You do not need to write the statements necessary to
declare the variable or capture the input (unless you want to show off). - Answers - int
userAge = 16;
if(userAge == 16) {
System.out.println("Happy Sweet 16!");
, }
True or false.
Loops can only work with positive values. No negative values can be used. - Answers -
False.
A(n) ___________ is the word used to describe each cycle or repetition through a loop.
- Answers - iteration
True or false.
Infinite loops are usually favorable because they ensure that the program will continue
running and operating as intended. - Answers - False.
What is the name of the value that can be used to immediately end or terminate a loop?
- Answers - sentinel
When the number of times a loop should run is a known number, a for loop is the better
option. - Answers - True.
Which term accurately describes placing one loop inside of another loop? - Answers -
Nesting
Write a for loop that will run 5 times and display the following output:
0
1
2
3
4 - Answers - for (int i = 0; i < 5; i++)
{
System.out.println(i);
}
Write a while loop that will run 5 times and display the following output:
5
4
3
2
1 - Answers - Scanner scnr = new Scanner(System.in);
int x = 5;
while (x >= 1)
{
System.out.println(x);