You are on page 1of 7

Assignment 2 [CPA – Grade 9] Due date: 26th November, 2017 (For all sections)

Q.1 What is the output in the following questions:

1. int i = 10;
int n = i++%5;
a. What is the value of i and n?
b. If we replace (i++) with (++i), what will be the value of i and n?
2. class Demo {
public static void main (String[] args){
int result = 1 + 2;
System.out.println(result);

result = result - 1;
System.out.println(result);

result = result * 2;
System.out.println(result);

result = result / 2;
System.out.println(result);

result = result + 8;
result = result % 7;
System.out.println(result);
}
}
Show the output of the above code by dry run.
3. class Demo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i);
++i;
System.out.println(i);
System.out.println(++i);
System.out.println(i++);
System.out.println(i);
}
}
Show the output of the above code by dry run
4. What is the result of following operations:
56 % 6
78 % -4
-34 % 5
-34 % -5
5% 1
1% 5
5. Show the output of following lines by dry run:
System.out.println(2 * (5 / 2 + 5 / 2));
System.out.println(2 * 5 / 2 + 2 * 5 / 2);
System.out.println(2 * (5 / 2));
System.out.println(2 * 5 / 2);
System.out.println("25 / 4 is " + 25 / 4);
System.out.println("25 / 4.0 is " + 25 / 4.0);
System.out.println("3 * 2 / 4 is " + 3 * 2 / 4);
System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4);
6. Show the output of following lines by dry run:
double amount = 5;
System.out.println(amount / 2);
System.out.println(5 / 2);
7. What is the output of following statements:
int x = 8;
System.out.println(++x * 3 + "," + x);
8. What is the output of following statements:
int g = 3;
System.out.print(++g * 8);
9. What is the output of the following statements:
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
10. What is the output of the following code:
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b));
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
11. What is the output of the following code:
double var1 = 1 + 5;
double var2 = var1 / 4;
int var3 = 1 + 5;
int var4 = var3 / 4;
System.out.print(var2 + " " + var4);
12. What is the output of the following code:
int a = 1;
int b = 2;
int c,d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c);
13. What is the output of the following code:
class Demo {
public static void main(String[] args){
String firstString = "This is";
String secondString = " a concatenated string.";
String thirdString = firstString+secondString;
System.out.println(thirdString);
}
}
14. Which data type would you use to represent the following values?
a) Child age
b) Employee salary
c) Whether somebody has children or not
d) Apartment letter
15. Find the value of the variable result after executing the following sentences:
int a;
int b;
int result;

a) a = 4;
b = 12;
result = a + b / 3;

b) a = 3;
a = a + 3;
b = 2;
result = a – b;

c) a = 2;
b = a + 1;
a = b + 2;
result = a + b + a;
result = -result;

d) a = 3;
b = 11;
result = (b % a) + 1;

e) a = 3;
b = a++;
result = 1;
result += a – b;

f) String s = “jjj”;
String t = “xxx”;
String result2 = s + s + t;

Q.2 MCQ’s:

1. What is the output of the program:


class test {
public static void main(String args[]) {
int x = 8;
System.out.println(++x * 3 + "," + x);
}
}
[A] 24,8
[B] 24,9
[C] 27,8
[D] 27,9
2. What will be the output of following program ?
class test
{
public static void main(String args[])
{
boolean ans=false;
if(ans=true)
System.out.print("Done");
else
System.out.print("Fail");
}
}
A. Done
B. Fail
C. Error
D. DoneFail
3. What will be the output of following program?
class test
{
public static void main(String args[])
{
int x=5,y;
y= ++x + x++ + --x;
System.out.println(x + "," + y);
}
}
A. 6,15
B. 6,18
C. 6,12
D. None of these
4. What will be the output of following program?
class Opr
{
public static void main(String args[])
{
byte a,b;
a=10; b=20;
a+=100;
b=a;
System.out.println(a +","+ b);
}
}
A. 110, 110
B. 10, 110
C. 10, 10
D. None of these
5. What will be the output of following program?
class test
{
public static void main(String args[])
{
int a,b,result;
a=10; b=20;
result=(b>=a);
System.out.print(result);
}
}
A. Error
B. 1
C. True
D. 20
6. What will be the output of following program?
class test
{
public static void main(String args[])
{
System.out.print( ((true && true) || false) );
}
}
A. Error
B. false
C. true
D. None of these
7. What will be the output of following program?
class test
{
public static void main(String args[])
{
int a,i;
i=10;
a=a+i;
System.out.print(a);

}
}
A. 10
B. Garbage
C. Error
D. None of these
8. With x = 0, which of the following are lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;

A. 1, 2 & 3
B. 1 & 4
C. 1, 2, 3 & 4
D. 3 & 2
9. Decrement operator, −−, decreases value of variable by what number?
A1
B2
C3
D4
10. Which of these values can a boolean variable contain?
A. True & False
B. 0 & 1
C. Any integer value
D. true
11. Which one is a valid declaration of a boolean?
A. boolean b1 = 1;
B. boolean b2 = ‘false’;
C. boolean b3 = false;
D. boolean b4 = ‘true’
12. What is the output of this program?
class test {
public static void main(String args[]) {
int var1 = 5;
int var2 = 6;
int var3;
var3 = ++ var2 * var1 / var2 + var2;
System.out.print(var3);
}
}
a) 10
b) 11
c) 12
d) 56
13. What is the output of this program?
class test {
public static void main(String args[]) {
double a, b;
a = 3.0;
b = 4.0;
double c = Math.sqrt(a * a + b * b);
System.out.println(c);
}
}
a) 5.0
b) 25.0
c) 7.0
d) Compilation Error
Q.3 WAP for the following questions:

1. WAP that reads a temperature value in Celsius degrees from the keyboard (user) and transforms it to
Fahrenheit degrees. The program must print the two values in the form: X Celsius degrees are Y Fahrenheit
degrees.
2. WAP that calculates the following statistical parameters of three values x1, x2 and x3. Values to be input/read
from the keyboard.

3. WAP to exchange (swap) the values of two integer variables (e.g.: if x is equal to 10 and y is equal to 5, at the
end of the program, x must be equal to 5 and y equals to 10). Note: Using a third variable as container.
4. WAP that reads two integer numbers on the keyboard and prints YES if the first of them is divisible by the
second one or NO if not.

Instructions:

1. All the questions are to be submitted in the Assignment book in the hand-written format.

2. Dry run is MANDATORY for the problems of Q. 1 and Q. 2. (Dry run is what we assume the program will do in mind,
without running it on a computer. For example, we did dry runs for all the question on increment/decrement operator
and then you all performed it in a program. For Q.1 and 2 ONLY DRY-RUN. NO PROGRAM)

Program to be written and output to be printed for Q.3 (Just like assignment 1 of Java)[WAP means Write A Program]

3. Don’t write ANY questions while writing the answers in the assignment book. Just mention the question number and
then the answer.

4. Sample answer for Q.1/Q.2 is attached. You can refer if you have doubts about what to write or how to do dry run:

[SAMPLE DRY RUN for Q.1 (i)]

You might also like