Part A: What happens when you attempt to compile and execute the following appli
ID: 3714477 • Letter: P
Question
Part A:
What happens when you attempt to compile and execute the following application?
1. class XXX {
2. public static void main(String[] args) {
3. String s1 = "abcde";
4. String s2 = "abcde";
5. s1.toUpperCase();
6. if (s1 == s2)
7. System.out.println("YES");
8. else
9. System.out.println("NO");
10. }
11. }
a. Compilation error
b. Prints YES
c. Prints NO
d. Runtime Exception
Part B:
Which of the following statements are true? Choose 3
a. A value can not be assigned to a final field more than once.
b. A value can be assigned to a final field at any time or not at all.
c. Only static variables can be declared final.
d. A compile-time error is thrown if a blank final instance variable is not assigned a value before the end of each constructor.
e. A field can not be declared both final and volatile.
Part C:
class A {
public static void main(String[] args) {
int[][] a1 = {{1,2},{3,4,5},{6,7,8,9},{}};
System.out.print(a1.length);
}
}
What is the result of attempting to compile and run the program?
a. Prints: 0
b. Prints: 3
c. Prints: 4
d. Prints: 9
e. Prints: 10
f. Prints: 11
g. Compile-time error
h. Run-time error
i. None of the above
Part D:
Which of these lists contains at least one word that is not a Java keyword?
a. interface, static, void, catch, final
b. char, strictfp, finally, long, volatile
c. native, super, class, float, while
d. const, for, new, switch, import
e. continue, finalize, goto, package, synchronized
f. None of the above
Part E:
class A {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
int i = 1;
m(m(++i) - m(i++) + m(-i) * m(~i));
}
}
What is the result of attempting to compile and run the above program?
a. Prints: 2, 2, -3, -4, 8,
b. Prints: 2, 2, -3, -4, 12,
c. Prints: 2, 3, -3, -4, 7,
d. Prints: 1, 1, 1, 1, 0,
e. Prints: 2, 2, -2, -2, 4,
f. Prints: 2, 3, -2, -2, 3,
g. Prints: -1, -2, 2, 2, 0,
h. Run-time error
i. Compile-time error
j. None of the above
Explanation / Answer
1
String s1 = "abcde"; // s1 contain abcde
String s2 = "abcde"; // s2 contain abcde
s1.toUpperCase(); // s1 letters changed to upper case it cant assigned to s1 contain abcde
if (s1 == s2) // abcde and abcde are same so print YES
System.out.println("YES");
else
System.out.println("NO");
Prints YES
Option b correct
2.
Option a,b and d correct
C.
int[][] a1 = {{1,2},{3,4,5},{6,7,8,9},{}};
System.out.print(a1.length); // It prit 4 , here there are 2 rows and 3 columns
Option c correct
D.
Option e correct
E.
class Main {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
int i = 1; // variable declaration
m(m(++i) - m(i++) + m(-i) * m(~i));
}
}
m(++i) - i value is 2 pre incrementation
m(i++) - i value is 2 post incrementation, after any statement i become 3
m(-i) - i value is -3
m(~i) - i value is -4
m(m(++i) - m(i++) + m(-i) * m(~i)): m(2 - 2 + (-4) * (-3)) = m(2 - 2 + 12) = m(12)- i value is 12
Prints: 2, 2, -3, -4, 12,
Please rate ny answer