1.
Write the value of z after the execution of the following code :
int j; int z, j=16;
z = (4*j++)/3;
Hint: 1)evaluate from Left to Right 2) Prefix(++j) change and use, postfix use and change(j++) 3)Binary operations based on BODMAS rule 4) division, multiplication, and modulus whichever comes first should be performed first. 5) numerator and denominator are integers perform only integer division 151/3 = 50 6) assign the value to z.
Correct Answer
A. 21
Explanation
The code initializes the variables j and z, and assigns the value 16 to j. Then, it calculates the value of z using the expression (4*j++)/3. Since the postfix increment operator is used (j++), the value of j is first used in the expression and then incremented. Therefore, the value of j in the expression is 16, and the expression simplifies to (4*16)/3 = 64/3 = 21. Finally, the value 21 is assigned to z.
2.
What will be the value of j and k after execution of the following code:
int j=22,k=20;
if(k>=j)
k=j;
j=k;
Correct Answer
B. K=20, j=20
Explanation
The code first checks if k is greater than or equal to j. Since k is 20 and j is 22, the condition is not true and k remains unchanged. Then, the value of k is assigned to j, so j becomes 20. Therefore, the final values are k=20 and j=20.
3.
Write the value of P after execution of following code :
int p; int k=18;
p = (6*++k)/3;
Correct Answer
B. P=38
Explanation
The code initializes two variables, p and k, with p being uninitialized and k being assigned the value 18. The expression (6*++k)/3 is then evaluated and the result is assigned to p. The operator ++k increments the value of k by 1 before the multiplication is performed. Therefore, the expression becomes (6*19)/3, which simplifies to 114/3 and further simplifies to 38. Therefore, the value of p after the execution of the code is 38.
4.
What will be displayed in jTextField1 and jTextField2 after the
following code is executed:
int t;
int s;
s = 17;
t = (4*s++)/2;
jTextField1.setText(" "+t);
jTextField2.setText(" "+s);
Correct Answer
C. 34 // jTextField1
18 // jTextField2
Explanation
After the code is executed, the value of s is incremented by 1 (s++), and then multiplied by 4. The result is divided by 2 and assigned to t. Therefore, t will have the value of 34.
The value of s is incremented to 18 due to the post-increment operator (s++). Therefore, jTextField2 will display the value 18.
5.
What will be displayed in jTextField1 and jTextField2 after the following code is executed:
int num = 35 , num1 = 86 ;
jTextField1.setText( num + num1 + " " ) ;
jTextField2.setText( " " + num + num1 ) ;
Hint : As you move from left to right, on the left and right of + operator if it is a number, it will add,
if anyone is a string it will concatenate
Correct Answer
A. 121 // jTextField1
3586 // jTextField2
Explanation
The code first adds the values of num and num1, which are 35 and 86 respectively. The sum is then converted to a string and displayed in jTextField1.
In jTextField2, the code concatenates the strings " " and "3586", which are the string representations of num and num1 respectively. Therefore, the final output in jTextField2 is "3586".
6.
Given a String object named 'code' having value as “918” stored in it. What will be the result of the following?
JOptionPane.showMessageDialog(null, " " +(code.length( )+ Integer.parseInt(code)));
Hint : String code = "918";
Correct Answer
B. 921
Explanation
The code.length() method returns the length of the string "code", which is 3. Integer.parseInt(code) converts the string "code" into an integer value, which is 918. Adding these two values together gives us 921. Therefore, the result of the given code will be 921.
7.
What will be the content of the jTextArea1 after executing the following code :
int n = 99;
jTextAreal.setText(Integer.toString(n++));
Hint : Postfix (n++) means use and change
Correct Answer
A. 99
Explanation
The content of jTextArea1 after executing the code will be 99. This is because the code uses the postfix increment operator (n++), which means that the value of n is first used in the Integer.toString() method and then incremented by 1. Therefore, the initial value of n (99) is displayed in the jTextArea1.
8.
Int d;
int c;
d=15;
c = (5*++d)%3+d;
Hint : modulus(%) returns the remainder of the division
Correct Answer
A. 18
Explanation
The code first increments the value of d by 1 using the pre-increment operator (++d). Then it multiplies 5 with the incremented value of d, which is 16. The modulus operator (%) is used to find the remainder when dividing the result (80) by 3. The remainder is 2. Finally, the remainder (2) is added to the original value of d (15), resulting in 17. Therefore, the correct answer is 17.
9.
Write the values of sum and x after execution of the following code :
int sum, x;
sum =1 7;
x =1 5;
sum = sum + (x++);
Correct Answer
B. Sum = 32
x=16
Explanation
The code initializes two variables, sum and x. It then assigns the value 17 to sum and the value 15 to x. The line sum = sum + (x++); adds the current value of x (15) to sum and then increments the value of x by 1. Therefore, the new value of sum is 32 (17 + 15) and the new value of x is 16.
10.
Write the value that will be assigned to variable C after executing the
following statement:
C = 125-15*4/3-10+6;
Correct Answer
B. 101
Explanation
The expression C = 125-15*4/3-10+6 can be simplified using the order of operations (PEMDAS). First, we perform the multiplication and division from left to right, so 15*4/3 equals 20. Then, we subtract 20 from 125, giving us 105. Next, we subtract 10 from 105, resulting in 95. Finally, we add 6 to 95, giving us the final value of C, which is 101.
11.
Write the values of c and d after execution of following code:
int a =1 1;
int b = 12;
int c;
int d;
c = ++b;
d = a++;
c++;
Correct Answer
C. C=14
d=11
Explanation
The code initializes variables a and b with the values 11 and 12 respectively.
The statement "c = ++b;" increments the value of b by 1 and assigns the new value (13) to c.
The statement "d = a++;" assigns the current value of a (11) to d and then increments the value of a by 1.
The statement "c++;" increments the value of c by 1.
Therefore, after the execution of the code, the value of c is 14 and the value of d is 11.