You are on page 1of 4

M105 – Meeting 2 - Extra Exercises

Exercise 1:
Given the following declarations: int i; double d;
What are the values of and after executing each of the following Java
statements?

a) i = 4 + 6 / 2;
b) i = (4+6) / 2;
c) i = 8 + 4 / 4 – 2;
d) i = 16 / 8 / 2;
e) i = 12 / 2 * 3;
f) i = 6 % 3;
g) d = 10 / 4 + 1;
h) d = 1 / 4 + 3 / 4;
i) d = 10.0 / 4 + 1;
j) d = 1.0 / 4 + 3.0 / 4;
k) d = 10 / 4 + 1.5;
l) i = 4 % 6;
m) i = 1 + 7 % 2;
n) i = 10 % 2 % 3;
o) d = (6 + 2 * (3 / 2)) / 10;

Solution
a) 7
b) 5
c) 7
d) 1
e) 18
f) 0
g) 3.0
h) 0.0
i) 3.5
j) 1.0
k) 3.5
l) 4
m) 2
n) 0
o) 0.0

Exercise 2:
Given the following declarations: double a, b, y;
Write an equivalent Java statement to the following arithmetic expression
assuming that the variables and have been initialized:

Solution

y=(a*a+b*b)/(a*b);
OR
y=(Math.pow(a,2)+Math.pow(b, 2))/(a*b);

1
Exercise 3:
What is the exact output of the following piece of code?

System.out.printf("%s\n%s%s\n",
"Welcome!", "You are in ", "AOU.");
System.out.println(2 + 3 + " books, " + 2 + 3 + " CD's and "
+ 2 * 3 + " bags are available.");
System.out.printf("There is %d %s in course M105\n", 1, "TMA");
System.out.printf("Average score is %.1f and %d%% of students"
+ " are above average.\n", 67.245, 25);
System.out.printf("See " + "You" + "!" + "\n");

Solution

Welcome!
You are in AOU.
5 books, 23 CD's and 6 bags are available.
There is 1 TMA in course M105
Average score is 67.2 and 25% of students are above the average.
See you!

Exercise 4:
Write a Java program that prompts the user to enter his/her name and age;
then displays on the screen the following message using printf method:
Your name is name and your age is age

Solution

import java.util.Scanner;
public class Q4 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter your name: ");
String name=input.nextLine();
System.out.print("Enter your age: ");
int age=input.nextInt();
System.out.println("Your name is "+name+" and your age is "+age);
}
}

2
Exercise 5: (from MTA, Fall 2012-2013)

Write a Java class called TriangleArea that reads from the user the base
of a triangle and its height as real numbers.
Then calculates and prints the area of the triangle (rounded to 3 decimal
places).
Hint:

Solution

import java.util.Scanner;
public class Q5 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter your base of a triangle: ");
double base=input.nextDouble();
System.out.print("Enter your height of a triangle: ");
double height=input.nextDouble();

double area = 0.5*base*height;


/// Error to write
// double area = 1/2*base*height; because this case integer division --> 0
System.out.printf("The area is: %.2f", area);
}
}

Exercise 6:
The following code includes 7 compilation errors (an error per line).
a) Discover and correct them.
b) Give the exact output after correcting the code.
integer a, b;
double d; e;
b = a + 2;
a = 1.0;
D = 5;
d + 1 = e;
System.out.println("b = %d, e = %.1f\n", b, e);

3
Solution
integer a, b; int not integer
Correction: int a,b;
double d; e; , not ;
Correction: double d, e;
b = a + 2; a must initialize before we use
it.
Correction: a=1; b=a+2;
a = 1.0; a must be integer value
Correction: a = 1;
D = 5; d not D (java is case sensitive)
Correction: d = 5;
d + 1 = e; Error in writing the assignment
statement
Correction: e=d+1
System.out.println("b = %d, e = %.1f\n", printf not println
b, e); Correction: System.out.printf(
"b = %d, e = %.1f\n", b, e);

Exercise 7:
Write a Java program that prompts the user to enter an integer consisting
of exactly 2 digits; then displays on the screen the sum of its
individual digits.

Example:
If the user enters 43, the message should be: The sum of the individual
digits of 43 is 7.

Solution

import java.util.Scanner;
public class Q7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer consisting of exactly 2 digits: ");
int x=input.nextInt();
while (x>=100 || x<=9){
System.out.print("Enter an integer consisting of exactly 2 digits: ");
x=input.nextInt();
}
int sum=x%10+(x/10);
System.out.println("The sum of the individual digits of "+x+" is "+sum);
}
}

You might also like