COM121 - Introduction to Computer Programming Practice Questions
1. Which of the following are valid names for Java classes?
A. Hello World
B. Hello_World
C. helloWorld
D. HelloWorld
2. Write a Java program with a class named StarFigure that produces the
following output using a for-loop.
3. What is the name of the outermost wrapper that contains all your code?
A. Class
B. Main method
C. Curly brace
D. Program
4. What happens if you forget to add a semi-colon at the end of a line of code in
Java?
A. The computer reports a "compiler error"
B. The code won't save
C. The code will run, but nothing will happen
D. The computer will add it for you
5. How do you create a single line comment in Java?
A. // comment text
B. /* comment text
C. ** comment text
D. */ comment text
6. Select all the places you should put comments
1
A. On every line
B. Above the class header
C. After the last curly brace
D. Above each method
E. To describe particularly tricky lines of code
7. Which of the following are valid Strings in Java?
A. "hello world"
B. "h3110_w0r1d"
C. "hello
World"
D. ""hello" "world""
8. What does an escape sequence do?
A. Turns letters into a String
B. Tells the computer to include special characters in a String that would
otherwise break it
C. Lets you include numbers in a String
D. Turns a String into numbers
9. Write a complete Java program in a class named WellFormed that prints the
following output:
Output 1:
A well-formed Java program has
a main method with {and}
braces.
Output 2:
A [Link] statement
has (and) and usually a
String that starts and ends
with a " character.
(But we type \" instead!)
10. What is the correct syntax for a method header in Java?
A. public static void myMethodName () {
B. public class MyMethod {
2
C. public static void myMethodName {
D. public static void my Method Name () {
11. Where do you put static methods in Java?
A. After the class
B. Inside the class, outside the main method
C. At the end of the main method
D. Inside the main method, outside the class
12. The following program contains at least 10 errors. Correct them.
public class LotsOf Errors {
public static main(String com121) {
[Link](Hello, world!);
message()
}
public static void message {
[Link] println("This program surely cannot ";
[Link]("have any "errors" in it");
}
13. What will be the output from this Java program?
public class ControlFlow {
public static void main(String[] args) {
[Link]("start main");
method1();
method2();
method3();
[Link]("end main");
}
public static void method1() {
[Link]("enter method1");
method2();
[Link]("end method1");
}
3
public static void method2() {
[Link]("enter/end method2");
}
public static void method3() {
[Link]("enter method3");
method1();
[Link]("end method3");
}
}
14. What are the values of a, b, and c after the following code statements?
int a = 5;
int b = 10;
int c = b;
a = a + 1;
b = b - 1;
c = c + a;
15. What is the output from 7 / 2 * 2.0?
1. How many times will the following for-loop run?
for (int i = 1; i < 10; i++) {
[Link](i);
}
A. 1
B. 9
C. 10
D. it never stops
2. How many stars will the following code print?
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
[Link]("*");
}
}
A. 5
4
B. 10
C. 15
D. 50
3. What is the result of the following code?
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i; i++) {
[Link]("*");
}
[Link]();
}
4. Write a for-loop to produce the following output:
1
22
333
4444
55555
666666
7777777
5. Write a nested for-loop that produce the following output:
000111222333444555666777888999
6. How many times will the following while-loop run?
int x = 0;
while(x < 10) {
[Link](x);
}
A. 0
B. 9
C. 10
D. Infinite
7. Which of the following user inputs will cause this while-loop to terminate?
String playAgain = "yes";
while([Link]("y")) {
playGame();
playAgain = [Link]();
5
}
A. "Yes"
B. "no"
C. "yea"
D. "no thank you"
E. "Absolutely"
16. Select all valid relational operators for numerical values
A. =
B. <
C. >
D. !
E. ==
F. !=
G. <=
H. =>
I. >=
17. What is the result of the following piece of code?
"my string" == "My String"
A. True, both strings contain the same characters
B. False, the capitalization is different
C. False, you can't use "==" to evaluate Strings
D. Error, you can't use "==" to evaluate Strings
8. Translate each of the following English statements into logical tests that could
be used in an if/else statement. Write the appropriate logical test for each
statement below. Assume that three int variables, x, y, and z, have already been
declared.
A. z is odd.
B. z is not greater than y's square root.
C. y is positive.
D. Either x or y is even, and the other is odd. (Hint: Don't use && or ||.)
E. y is a multiple of z.
F. z is not zero.
G. y is greater in magnitude than z.
6
H. x and z are of opposite signs.
I. y is a nonnegative one-digit number.
J. z is nonnegative.
K. x is even
L. x is closer in value to y than z is.
9. Given the following variable declarations:
int x = 4;
int y = -3;
int z = 4;
What are the results of the following relational expressions?
A. x == 4
B. x == y
C. x == z
D. y == z
E. x + y > 0
F. x - z != 0
G. y * y <= z
H. y / y == 1
I. x * (y + 2) > y - (y + z) * 2
10. If x is 10, what would be the output of the following code?
if(x > 100) {
[Link]("a");
} else if (x > 50) {
[Link]("b");
} else if (x > 10) {
[Link]("c");
} else {
[Link]("d");
}
A. a
B. b
C. c
7
D. d
11. Which of the following is a correct syntax for an if-statement?
A. if x < y { [Link]("run if statement"); }
B. if [x = y] { [Link]("run if statement"); }
C. if (x == y) [ [Link]("run if statement"); ]
D. if (x >= y) { [Link]("run if statement"); }
12. Write Java code to read an integer from the user, then print even if that number
is an even number or odd otherwise. You may assume that the user types a
valid integer. The input/output should match the following example:
Type a number: 14
Even
13. Given the following piece of code, what is the output?
boolean x = true;
boolean y = false;
if(x && y) {
[Link]("a");
} else if (x || y) {
[Link]("b");
} else if (!x) {
[Link]("c");
} else {
[Link]("d");
}
A. a
B. b
C. c
D. d
14. Select all of the following values for x that would cause the following expression
to evaluate to true
x < 100 && x > 50 && x != 75
8
A. 100
B. 80
C. 75
D. 70
E. 50
F. 10
18. Write a method called inputBirthday that prompts a user to enter a month, day,
and year of birth, then prints the birthdate, and person’s age in a suitable
format. Here is an example dialogue with the user:
On what day of the month were you born? 8
What is the name of the month in which you were born? May
During what year were you born? 1981
You were born on May 8, 1981 and you are 39 years!
19. Trip Planner: For this project, you are going to write a program that asks the
user for some information about an international trip they are taking. Based on
that information you will need to do some conversions, using the correct data
types, to tell them some information to help them plan their trip. Here is my
sample output:
9
For your first project, we are going to break up the work into different parts.
You’ll want to think about the process of development when approaching any
project. As the course progresses you’ll be expected to do more of this planning
yourself, but for now pay attention to how we break the complex problem down
into smaller, easier to tackle, pieces.
Part 1 – Greeting
First, you’ll want to greet your user and ask them their name. As you can see
above the user types their response on the same line as the question. To do this
you’ll want to use a [Link]() statement instead of a
[Link]() statement. Once your user tells you their name use it to
greet them! Then ask them where they would like to go. Finally, tell your user
their destination sounds like a great trip. All the code to accomplish Part 1
should be in its own method.
HINT: Some names and destinations are more than 1 word! Plan accordingly.
10
Part 2 – Travel time and Budget
For this part, you’ll want to ask the user about how much time and money they
are budgeting for their trip. Ask the user:
1. How many days are they going to spend in their destination?
2. What is their total budget for the trip in USD?
3. What is the currency symbol for their destination? For example, the us dollar’s
is USD, the euro’s is EU, MWK for Malawian Kwacha etc…
4. The conversion rate between 1 USD to however many of that destinations
currency. For example, there are MWK 734.39 to 1 USD, so the user would type
in 734.39
Once you have all this info (stored in the correct variable types), use it to tell the
user the following information:
1. How much time the user will spend at their destination in days, hours, minutes
and seconds
2. Show the user their budget in USD for the whole trip and how much they can
spend in USD per day
3. Show the user their budget in the travel destination’s currency for the total trip
and per day
All the code to accomplish Part 2 should be in its own method.
HINT: keep in mind that if you divide integers you might lose some information
when it truncates.
Part 3 – Time Difference
For this part, you’ll need to ask the user about the time difference between their
home and where they are going. If the destination time zone is “behind” the user’s
home time zone the user should enter a negative number. For example, the time
difference between Blantyre and Nairobi is 1 hour, and the time difference
between New York and Mexico City is -1 hours.
11
Show the time it will be in the travel destination when it is midnight at home and
when it is noon at home. You can report these in 24 format, where midnight is
0:00 and noon is 12:00.
HINT: Your answers might be greater than 24. Try using the % or mod operator
to "wrap around" to 24-hour time format. For example, 12 % 24 is 12, but 25 %
24 is 1.
All the code to accomplish Part 3 should be in its own method.
Part 4 – Country Area
Only three countries in the world use the "imperial system", so most countries
report their distances in kilometres. For this part, ask the user the area of their
travel destination country in km^2. Then you’ll want to convert that to miles^2
and report those results back to the user.
All the code to accomplish Part 4 should be in its own method.
Part 5 – Round
You may have noticed some of our math has produced numbers with lots of
decimal places, making then not look very nice. In this part, you need to go back
and clean up those answers so they each only have 2 decimal places.
To do this you are going to use a combination of ints and doubles.
1. First, multiply the number you want to reformat by 100.
2. Cast the result of part 1 to an int like so:
(int) answerFromOne
This will drop any decimal places 3. Convert your answer back to a double, and
move the decimal place over by 2 numbers like so:
answerFromTwo / 100.0
If you follow the above steps you should find that any of your answers are not
limited to only 2 decimal places, however, those decimal places won’t be rounded,
they will just be the first two from the original answer.
This code will need to be added to each of the methods you wrote for parts II, III
and IV.
12