Exercise for lecture 3 and lecture 4
(1)
What is the range of values that can be assigned to a variable of type int?
(2)
What will the output of the following code?
for(int i = 1; i <3; i++) {
for(int j = 1; j <3; j++) {
System.out.print(i/j);
System.out.print(" "); // print a space
}
System.out.println();
}
(3)
What is the output of the following program?
int x = 3;
int y = 10;
System.out.println(y%x);
(4)
Find out errors in the following program
int i=1;
double d1=2.3, d2=3.7;
float f1=5.2, f2=1.1;
long x = 5L;
char ch = 'C';
d1 = i;
f1 = d1;
d2 = f2;
d2 = x;
x = ch;
ch = i;
i = x;
(5)
Write a program named Triangle.java such that
java Triangle 5 prints out 5 rows of stars like this
*
**
***
****
*****
In general java Triangle n prints out n rows of stars.
(6)
What is the return type of adding a float and an int?
(7)
What is wrong about the following program?(the following code is correct in C++, but not in java)
int x = 5;
if(x) {
System.out.println("x is not eqaul to zero.");
}
(8)
Write a program named Average.java such that
java Average double1 double2 ..... prints out the average of all the arguments.
(9)
What is the output of the following program?
int i = 2;
switch(i) {
case 2:
System.out.println("value is two");
case 3:
System.out.println("Value is three");
break;
default:
System.out.println("Value is " + j);
break;
}