1. PRACTICE EXERCISES - 1
Date: 05-FEB-2022
1) Which of the following is the correct syntax to output a message?
a) System.out.println("Hello, world!");
b) System.println.out('Hello, world!');
c) System.println("Hello, world!");
d) Out.system.println"(Hello, world!)";
e) System.println(Hello, world!);
2) What is the output produced from the following statements?
a) System.out.println(""Quotes"");
b) System.out.println("Slashes //");
c) System.out.println("How '"confounding' "" it is!");
3) What is the output from the following Java program? (Assume that a tab is
expanded into eight spaces.)
public class Letter {
public static void main(String[] args) {
System.out.println("Dear "DoubleSlash" magazine,");
System.out.println();
System.out.println("tYour publication confuses me. Is it");
System.out.println("a slash or a //// slash?");
System.out.println("nSincerely,");
System.out.println("Susan "Suzy" Smith");
}
}
4) What series of println statements would produce the following output?
This is a test of your
knowledge of "quotes" used
in 'string literals.'
You're bound to "get it right"
if you read the section on
''quotes.''
5) What is the output produced from the following program? You may wish to
draw a structure diagram first.
public class Tricky {
public static void main(String[] args) {
message1();
message2();
System.out.println("Done with main.");
}
2. PRACTICE EXERCISES - 1
Date: 05-FEB-2022
public static void message1() {
System.out.println("This is message1.");
}
public static void message2() {
System.out.println("This is message2.");
message1();
System.out.println("Done with message2.");
}
}
6) What is the output produced from the following program? You may wish to
draw a structure diagram first.
public class Strange {
public static void main(String[] args) {
first();
third();
second();
third();
}
public static void first() {
System.out.println("Inside first method.");
}
public static void second() {
System.out.println("Inside second method.");
first();
}
public static void third() {
System.out.println("Inside third method.");
first();
second();
}
}
7) What is the output produced from the following program?
You may wish to draw a structure diagram first.
This is a slightly modified version of the previous problem, Strange.
public class Strange {
public static void main(String[] args) {
first();
3. PRACTICE EXERCISES - 1
Date: 05-FEB-2022
third();
second();
third();
}
public static void first() {
System.out.println("Inside first method.");
}
public static void second() {
System.out.println("Inside second method.");
first();
}
public static void third() {
first();
second();
System.out.println("Inside third method.");
}
}
8) What is the output produced from the following program? You may wish to
draw a structure diagram first. This is a slightly modified version of the previous
problem, Strange.
public class Strange {
public static void main(String[] args) {
second();
first();
second();
third();
}
public static void first() {
System.out.println("Inside first method.");
}
public static void second() {
System.out.println("Inside second method.");
first();
}
public static void third() {
System.out.println("Inside third method.");
first();
second();
}
}
4. PRACTICE EXERCISES - 1
Date: 05-FEB-2022
9) What is the output from the following Java program?
public class Confusing {
public static void method1() {
System.out.println("I am method 1.");
}
public static void method2() {
method1();
System.out.println("I am method 2.");
}
public static void method3() {
method2();
System.out.println("I am method 3.");
method1();
}
public static void main(String[] args) {
method1();
method3();
method2();
method3();
}
}
10) What is the output produced from the following program? You may wish to
draw a structure diagram first. This is a slightly modified version of the previous
problem,
public class Confusing {
public static void main(String[] args) {
method2();
method1();
method3();
method2();
}
public static void method2() {
method1();
System.out.println("I am method 2.");
}
public static void method3() {
method2();
System.out.println("I am method 3.");
method1();
}
public static void method1() {
System.out.println("I am method 1.");
}
5. PRACTICE EXERCISES - 1
Date: 05-FEB-2022
}
11) Which of the following are legal int literals?
a) 22
b) 1.5
c) 5.
d) -1
e) -6875309
f) '7'
g) 2.3
h) 10.0
12) What is the result of the following expression?
1 + 2 * 3 + 7 * 2 % 5
a) 1
b) 5
c) 2
d) 21
e) 11
13) Trace the evaluation of the following expressions, and give their resulting
values. Make sure to give a value of the appropriate type (such as including a .0 at
the end of a double).
a) 2 + 3 * 4 - 6
b) 14 / 7 * 2 + 30 / 5 + 1
c) (12 + 3) / 4 * 2
d) (238 % 10 + 3) % 7
e) (18 - 7) * (43 % 10)
f) 2 + 19 % 5 - (11 * (5 / 2))
g) 813 % 100 / 3 + 2.4
h) 26 % 10 % 4 * 3
i) 22 + 4 * 2
j) 23 % 8 % 3
k) 12 - 2 - 3
l) 6/2 + 7/3
m) 6 * 7 % 4
n) 3 * 4 + 2 * 3
o) 177 % 100 % 10 / 2
p) 89 % (5 + 5) % 5
q) 392 / 10 % 10 / 2
r) 8 * 2 - 7 / 4
s) 37 % 20 % 3 * 4
t) 17 % 10 / 4
6. PRACTICE EXERCISES - 1
Date: 05-FEB-2022
14) Which of the following choices is the correct syntax for declaring a real
number variable named 'grade' and initializing its value to 4.0?
int grade : 4.0;
4.0 = grade;
double grade = 4.0;
grade = 4;
grade = double 4.0;
15) Imagine you are writing a personal fitness program that stores the user's age,
gender, height (in feet or meters), and weight (to the nearest pound or kilogram).
Declare variables with the appropriate names and types to hold this information.
Write a complete variable declaration statement with the type, the variable name,
and a semicolon.
age ?
gender?
height ?
weight?
16) What is the value of variable x after the following code executes?
int x = 3;
x = x + 2;
x = x + x;
a) 5
b) 7
c) 3
d) 12
e) 10
17) What are the values of a, b, and c after the following code statements? (It
may help you to write down their values after each line.)
int a = 5;
int b = 10;
int c = b;
a = a + 1;
b = b - 1;
c = c + a;
a ?
b ?
c ?
7. PRACTICE EXERCISES - 1
Date: 05-FEB-2022
18) What are the values of first and second at the end of the following code?
int first = 8;
int second = 19;
first = first + second;
second = first - second;
first = first - second;
Output
first ________ second ___________
19) What are the values of i, j, and k after the following code statements?
int i = 2;
int j = 3;
int k = 4;
int x = i + j + k;
i = x - i - j;
j = x - j - k;
k = x - i - k;
i = ______ ; j = ________ ; k = ___________ ;
20) What is the output from the following code?
int max;
int min = 10;
max = 17 - 4 / 10;
max = max + 6;
min = max - min;
System.out.println(max * 2);
System.out.println(max + min);
System.out.println(max);
System.out.println(min);
1) Write a Java program to print the multiplications of two numbers
2) Write a Java program to print the result of the following operations.
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
3) Write a Java program to compute a specified formula.
Specified Formula : 4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))
8. PRACTICE EXERCISES - 1
Date: 05-FEB-2022
Expected Output
2.9760461760461765
4) Write a Java program that takes three numbers as input to calculate and print the
average of the numbers.
5) Write a Java program to convert temperature from Fahrenheit to Celsius degree.
Input a degree in Fahrenheit: 212
Expected Output:
212.0 degree Fahrenheit is equal to 100.0 in Celsius
6) Write a Java program to find the value of specified expression.
a) 101 + 0) / 3
b) 3.0e-6 * 10000000.1
c) true && true
d) false && true
e) (false && false) || (true && true)
f) (false || false) && (true && true)
Expected Output:
(101 + 0) / 3)-> 33
(3.0e-6 * 10000000.1)-> 30.0000003
(true && true)-> true
(false && true)-> false
((false && false) || (true && true))-> true
(false || false) && (true && true)-> false