please do this quickly
Institute of Software Engineering
Diploma in Comprehensive Master Java Developer (CMJD)
Batch – CMJD104
Module – Programming Fundamentals
Assignment – 03
2 | Programming Fundamentals – Assignment 03
1. Write a Java program to print the total of two integer numbers input by the keyboard.
2. Write a Java program using the help of the “Scanner”,
a. Input two values and store them in two variables.
b. Print the values as they assign to the variable and finally print them as “Values
are …”
3. Write a Java program to accomplish the following task.
a. Input your age.
b. Store it in an integer variable named “age”
c. Find your age after 3 years (No additional variables can be used).
d. Print the new age as “New age : “
4. What will be the output when you compile and run the following program?
class Example{
public static void main(String args[]){
System.out.println(10+20+30);
System.out.println(“10+20+30”);
System.out.println(“10+20″+30);
System.out.println(“10″+”20″+”30”);
System.out.println(“10″+20+30);
System.out.println(10+20+”30”);
System.out.println(10+”20″+30);
}
}
5. Which of the following lines are valid declarations? Select the three correct answers.
a. char a = ‘\u0061’;
b. char ‘a’ = ‘a’;
c. char \u0061 = ‘a’;
d. ch\u0061r a = ‘a’;
e. ch’a’r a = ‘a’;
6. What is the output for the following code fragment and explain your answer?
a. System.out.println(1+2+3);
b. System.out.println(“1″+”2″+”3″);
c. System.out.println(‘1’+’2’+’3’);
d. System.out.println(‘1’+” “+’2’+” “+’3’);
e. System.out.println(‘A’+’B’+’C’);
f. System.out.println(“A”+”B”+”C”);
g. System.out.println(‘A’+100+200);
h. System.out.println(‘A’+” “+’B’+” “+’C’);
3 | Programming Fundamentals – Assignment 03
7. What will be the outputs when you compile and run the following program and explain your
answer line by line?
class Example{
public static void main(String asrg[]){
char a=’a’;
System.out.println(a==’\u0061′);
System.out.println(\u0061==’\u0061′);
System.out.println(\u0061==97);
\u0061=’\u0041′;
System.out.println(‘A’==’\u0041′);
System.out.println(65==’\u0041’);
System.out.println(65==a);
System.out.println(‘\u0041’==a);
}
}
8. Complete the following program?
import java.util.*;
class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print(“Input your age : “);
int age=input.nextInt();
System.out.println(“Your current age is : “+age);
//————–Do not modify before this line—//your are allowed to insert any code here to
//increment the age by 10
//———————Do not modify after this line–System.out.println(“Your age after 10 years is : ”
+age);
}
}
9. Complete the following program?
import java.util.*;
class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print(“Input number 1 : “);
int num1=input.nextInt();
System.out.print(“Input number 2 : “);
int num2=input.nextInt();
System.out.println(num1+” “+num2);
4 | Programming Fundamentals – Assignment 03
//———–Do not modify before this line——-//Insert Java code(s) here to swap the two
//variables “num1” and “num2”
//as an example when you input 10 for
//num1 and 20 for num2, then
//the first output should be “10 20” and the
//second output “20 10″
//——Do not modify after this line———–System.out.println(num1+” “+num2);
}
}
10. Compare and contrast the following with suitable examples:
a. Conversion and Casting
b. Narrow Conversion and Narrow Casting
c. Wider Conversion and Wider Casting
11. Which of the following code fragments are legal?
A.
double d=’A’;
long l=(int)d;
B.
char ch=’A’;
double d=ch;
C.
byte b=’65’;
char ch=b;
D.
double d=’A’;
char ch=(short)d;
E.
float f=65;
int x=(char)f;
12. Given :
class Example{
public static void main(String args[]){
long l;
//Line 10
System.out.println(l);
}
}
Which of the following statements can be legally placed at Line 10 of the above
program.
a. l = 2147483647;
b. l = 2147583647;
c. l = 0xabcd;
d. l = 0bcdL;
e. l = 0101010110L;
5 | Programming Fundamentals – Assignment 03
13. Given :
class Demo {
public static void main(String args[]) {
int tot = 971;
double avg;
//insert code here //Line 4
System.out.println(“Average : “ + avg);
}
}
Which of the following statements can be inserted at “Line 4” to get output as “Average
: 97.1”
a. avg = (double) tot/10;
b. avg = tot/(double)10
c. avg = (double)(tot/10)
d. avg = tot/10
e. None of above
14. What will be the result of attempting to compile and run the following program?
class Example{
public static void main(String asrg[]){
double d;
d=5/2+5/2;
System.out.println(d);
d=5/2.0+5/2;
System.out.println(d);
d=5/2+5.0/2;
System.out.println(d);
d=5/2.0+5/2.0;
System.out.println(d);
}
}
a.
b.
c.
d.
e.
4.0 4.0 4 5.0
4.0 4.5 4.5 5.0
4 4.0 4.0 5.0
4.5 4.5 4 5.0
4 4.5 4.5 5
15. Which of the following lines are valid declarations?
a. char a = ‘\u0061’;
b. char ‘a’ = ‘a’;
c. char \u0061 = ‘a’;
d. ch\u0061r a = ‘a’;
6 | Programming Fundamentals – Assignment 03
e. ch’a’r a = ‘a’;
16. Which of the following are legal lines of code?
a. int a = (int )888.8;
b. byte x = (byte)1000L;
c. long l = (byte)100;
d. byte z = (byte)100L;
17. Which of the following lines can be inserted at the line 12 to get the output “-1”
class Example{
public static void main(String args[]){
int x;
byte b;
//insert code here Line 12
b=(byte)x;
System.out.println(b);
}
}
a. x=Short.MAX_VALUE;
c. x=-1;
e. x=Byte.MIN_VALUE;
g. x=Integer.MAX_VALUE;
b. x=Short.MIN_VALUE;
d. x=Byte.MAX_VALUE;
f. x=0;
h. x=Integer.MIN_VALUE;
18. Write the outputs for the following code lines.
Given Code: int a=10, b=7, c=-10, d=-7;
a. System.out.println(a%b);
b. System.out.println(-a%b);
c. System.out.println(a%-b);
d. System.out.println(-a%-b);
e. System.out.println(+a%+b);
f. System.out.println(c%d);
g. System.out.println(-c%d);
19. Which of the following code lines are legal?
int x=65;
final int y=65;
final int z;
z=65;
char ch;
ch=’A’;
//Line 1
ch=65; //Line 2
ch=x;
//Line 3
ch=y; //line 4
ch=z;
//Line 5
7 | Programming Fundamentals – Assignment 03
a.
c.
e.
Line 1
Line 3
Line 5
b. Line 2
d. Line 4
f. None of the above
20. Which statements are true? Select the three correct answers.
a. The result of the expression (1 + 2 + “3”) would be the string “33”.
b. The result of the expression (“1” + 2 + 3) would be the string “15”.
c. The result of the expression (4 + 1.0f) would be the float value 5.0f.
d. The result of the expression (10/9) would be the int value 1.
e. The result of the expression (‘a’ + 1) would be the char value ‘b’.
21. Which of the following are legal lines of code?
a. int a = (int )888.8;
b. byte x = (byte)1000L;
c. long l = (byte)100;
d. byte z = (byte)100L;
22. Write the outputs for the following code lines.
Given: int x=10,y=7;
a.
b.
c.
d.
e.
f..
System.out.println(x+y);
System.out.println(-x);
System.out.println(-x-y);
System.out.println(-(x-y));
System.out.println(+y);
System.out.println(+y-x);
23. Write the outputs for the following code lines.
int x=-100;
x=+x;
System.out.println(x);
x=-x;
System.out.println(x);
x=-x;
System.out.println(x);
x=x+x;
System.out.println(x);
x=-x-x;
System.out.println(x);
x=x-x;
System.out.println(x);
24. Write the outputs for the following code lines.
int x=100;
System.out.print(x++);
System.out.println(x++);
x++;
8 | Programming Fundamentals – Assignment 03
System.out.println(++x);
System.out.println(x++);
25. Write the outputs for the following code lines.
int x=100,y;
y=x++;
System.out.println(x+” “+y);
y=x++;
System.out.println(x+” “+y);
y=x++;
System.out.println(x+” “+y);
26. Write the outputs for the following code lines.
int x=100,y;
y=++x;
System.out.println(x+” “+y);
y=++x;
System.out.println(x+” “+y);
y=++x;
System.out.println(x+” “+y);
27. Write the outputs for the following code lines.
int x=100;
x=x++;
System.out.println(x);
x=x++;
System.out.println(x);
x=x++;
System.out.println(x);
x=++x;
System.out.println(x);
x=++x;
System.out.println(x);
x=++x;
System.out.println(x);
28. Write the outputs for the following code lines.
Given code :int a=10, b=7, c=-10, d=-7;
a.
b.
c.
d.
e.
System.out.println(10%7);
System.out.println(10%5);
System.out.println(10%17);
System.out.println(5.0%1.0);
System.out.println(5.5%1.1);
29. Explain the evaluation of following expressions
int a=10,b=20;
9 | Programming Fundamentals – Assignment 03
int x;
a.
c.
e.
g.
x= a + b;
x= ++a + b;
x= ++a + b++;
x= ++a + ++ b;
b.
d.
f.
h.
x= a +- b;
x= a + b++;
x= a++ + b++;
x= a++ + ++b;
30. What will be the result of attempting to compile and run the following program? Explain your
answers.
class Example{
public static void main(String[] args) {
int x;
x= 12 – 4 * 2;
System.out.println(“12 – 4 * 2 : “+x);
x= (12 – 4) * 2;
System.out.println(“(12 – 4) * 2 : “+x);
x= 12 – (4 * 2);
System.out.println(“12 – (4 * 2) : “+x);
}
}
31. Explain the evaluation of following expressions
int x;
a. x= 7 % 10 / 2 * 2;
b.
x= 7 % (10 / 2) * 2;
c. x= 7 % 10 / (2 * 2);
d.
x= 7 % (10 / (2 * 2));
e. x= 7 % ((10 / 2) * 2);
32. Explain the evaluation of following expressions
int a=100;
a. a= a + (a=6);
b. a= (a=6) + a;
c. a= (a=6) + (a=5);
d. a= a*3 + a;
33. Explain the evaluation of following expressions
int a=10;
int x;
a. x= a++ + a;
b. x= a + a++;
c. x= ++a + a;
d. x= a + ++a;
e. x= ++a + ++a;
f. x= a++ + a++;
g. x= ++a + a++;
h. x= a++ + ++a;
34. Write the outputs for the following code lines.
int x,y;
x=y=100;
x=x++ +x++ + x++ ;
System.out.println(x);
y=++y + ++y + ++y;
System.out.println(y);
10 | Programming Fundamentals – Assignment 03
y=x=100;
System.out.println();
x=x++ + ++y + ++x + y++;
System.out.println(x+” “+y);