Assignment 4
Assignment 4
1. Input two numbers and check whether the first number is greater than the second number. If so
add the two numbers otherwise display the two numbers.
2. Write a java program to find the absolute number of a given integer number.
3. Enter marks Obtained by a student for Chemistry, Physics and Combined math. Calculate the total
and average. If the average is greater than 75 then display “Pass” otherwise “Fail”. Write a java
program to perform the above task.
4. Enter the unit price and amount bought from a product. Calculate the total. If the total is greater
than Rs.1500/- display “You are entitled to the super draw. Otherwise display “try again”.
5. Enter unit price and amount bought from a product. Calculate the total. If the total is more than
Rs.500/- give 5% discount. Calculate the discount and new total and display those. Otherwise
display “No discount given”.
6. Write a Java program to get a Year from user input and find whether it is a leap year or not.
7. Write a Java program to find & print the area of a circle when user input the radius.
9. Write a java program to find the maximum number of three integer numbers input by the
keyboard and print results as follows; “Maximum number is : 45”
10. Write a Java program to input an integer number from the keyboard and print whether the
number is odd or even.
11. Which of the following lines can be legally inserted at line 10?
class Example{
public static void main(String[] args) {
int x = 10;
//Insert code here//Line 10
}
}
3 | Programming Fundamentals – Assignment 04
A. if(x){} B. if(x=10){}
C. if(x==10){} D. if(x=100!=10){}
E. if((x=100)!=10){} F. if((x=100)>0==true){}
12. Which of the following lines can be legally inserted at line 10?
class Example{
public static void main(String[] args) {
int x = 10;
boolean b=true;
//Insert code here//Line 10
}
}
A. if(b){} B. if(b=false){}
C. if(b==false){} D. if(b=false==false){}
E. if((b=false)==false){}
F. if(b=(false==true)){}
if(++x==x){
System.out.println("++x==x : "+x);
}
x=99;
if(x==x++){
System.out.println("x==x++ : "+x);
}
x=99;
if(x==++x){
System.out.println("x==++x : "+x);
}
x=99;
if(++x==++x){
System.out.println("++x==++x : "+x);
}
x=99;
if(x++==x++){
System.out.println("x++==x++ : "+x);
}
x=99;
if(++x==x++){
System.out.println("++x==x++ : "+x);
}
x=99;
if(x++==++x){
System.out.println("x++==++x : "+x);
}
if(++x==++x){
System.out.println("++x==++x : "+x);
}
if(x++==x++){
System.out.println("x++==x++ : "+x);
}
if(++x==x++){
System.out.println("++x==x++ : "+x);
}
20. Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print?
System.out.println( i ==1); //Line 1
System.out.println( j ==3); //Line 2
System.out.println( ( i >=1) && ( j <4) ); //Line 3
System.out.println( ( m <=99) & ( k < m ) ); //Line 4
System.out.println( ( j >= i ) || ( k == m ) ); //Line 5
System.out.println( ( k + m < j )|(3- j>= k)); //Line 6
System.out.println( !( k > m ) ); //Line 7
22. Given:
class Example{
public static void main(String args[]){
//line 5
switch(x){
default : System.out.print("4 ");
case 1 : System.out.print("1 ");
case 2 : System.out.print("2 ");
case 3 : System.out.print("3 ");
}
}
}
What will be the outputs when you insert the following codes at line 5?
A. int x=1; B. int x=2; C. int x=3;
D. int x=4; E. int x=0; F. int x=5;
24. Which of the following code fragments can be inserted at line 10 to legal line 12
final int y;
y=100;
int z=100;
int a;
//Insert code here //Line 10
System.out.println(a); //Line 12
25. Given:
class Example {
public static void main(String[] args) {
int a = 2;
char b,c,d;
b = (a < 2) ? 'f' : 'g'; // 1
if (a < 2) c = 'h'; else c = 'i'; // 2
if (a < 2) d = 'j'; // 3
if (a > 2) d = 'k'; // 4
if (a == 2) d = 'l'; // 5
System.out.print(b + "," + c + "," + d); // 6
}
}
What is the result of attempting to compile and run the program?
A. Prints: g, i, l
B. Compiler Error: variable b might not have been initialized.
C. Compiler Error: variable c might not have been initialized.
D. Compiler Error: variable d might not have been initialized.
E. Runtime Exception
F. None of the above.
}
}
Select one option?
A. Prints 10 B. Prints -1 -1
C. Prints -2 -2 D. Prints 0 0
32. Given:
class Example{
public static void main(String args[]){
//line 5
switch(x){
default : System.out.print("4 ");
case 1 : System.out.print("1 ");
case 2 : System.out.print("2 ");
case 3 : System.out.print("3 ");
11 | Programming Fundamentals – Assignment 04
}
}
}
What will be the outputs when you insert the following codes at the line 5?
A. int x=1; B. int x=2; C. int x=3;
D. int x=4; E. int x=0; F. int x=5;
33. Given:
//Insert code here //line 4
switch(x){
case 'A' : System.out.println("65 ");break;
case 'B' : System.out.println("66 ");break;
case 'C' : System.out.println("67 ");break;
default : System.out.println("wrong ");
}
Which of the following codes can be inserted legally at line 4
A. char x=’A’; B. int x=65; C. int x=65536;
D. byte x=65; E. short x=66 ; F. boolean x =true;
G. String x=”A”; H. double x=65.0;
34. Given:
class Example{
public static void main(String args[]){
//Line 5
switch(x){
default : System.out.print("4 ");break;
case 2 : System.out.print("2 ");
case 3 : System.out.print("3 ");
case 1 : System.out.print("1 ");break;
}
}
}
What will be the output when you insert the following codes at line 5?
A. int x=1; B. int x=2; C. int x=3;
D. int x=4; E. int x=0; F. int x=5;
35. Given:
class Example{
public static void main(String args[]){
//line 5
switch(x){
12 | Programming Fundamentals – Assignment 04
What will be the outputs when you insert the following codes at line 5?
A. int x=1; B. int x=2; C. int x=3;
D. int x=4; E. int x=0; F. int x=5;