Assignment 3
Assignment 3
Batch - CMJD105
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 …”
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);
}
}
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);
}
}
e. ch'a'r a = 'a';
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;
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);
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. Line 1 b. Line 2
c. Line 3 d. Line 4
e. Line 5 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'.
a. System.out.println(x+y);
b. System.out.println(-x);
c. System.out.println(-x-y);
d. System.out.println(-(x-y));
e. System.out.println(+y);
f.. System.out.println(+y-x);
System.out.println(++x);
System.out.println(x++);
a. System.out.println(10%7);
b. System.out.println(10%5);
c. System.out.println(10%17);
d. System.out.println(5.0%1.0);
e. System.out.println(5.5%1.1);
int a=10,b=20;
9 | Programming Fundamentals – Assignment 03
int x;
a. x= a + b; b. x= a +- b;
c. x= ++a + b; d. x= a + b++;
e. x= ++a + b++; f. x= a++ + b++;
g. x= ++a + ++ b; h. 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);
}
}
y=x=100;
System.out.println();
x=x++ + ++y + ++x + y++;
System.out.println(x+" "+y);