x = true
y = 36
The ternary operator (a > b)? true : false returns true as its condition a > b is true so it returns its first expression that is true.
The ternary operator (a < b)? a : b returns b as its condition a < b is false so it returns its second expression that is b. Value of b is 36 so y also becomes 36.
(b) State the values of n and ch.
char c = 'A';
int n = c + 1;
char ch = (char)n;
Value of n is 66 and ch is B.
int n = c + 1, here due to implicit conversion, 'A' will be converted to its ASCII value 65. 1 is added to it making the value of n 66.
char ch = (char)n, here through explicit conversion 66 is converted to its corresponding character that is 'B' so value of ch becomes B.
(c) What will be the result stored in x after evaluating the following expression?
int x = 4;
x += (x++) + (++x) + x;
x += (x++) + (++x) + x
⇒ x = x + ((x++) + (++x) + x)
⇒ x = 4 + (4 + 6 + 6)
⇒ x = 4 + 16
⇒ x = 20
(d) Give output of the following program segment:
double x = 2.9, y = 2.5;
System.out.println(Math.min(Math.floor(x), y));
System.out.println(Math.max(Math.ceil(x), y));
2.0
3.0
Math.floor(2.9) gives 2.0. Math.min(2.0, 2.5) gives 2.0.
Math.ceil(2.9) gives 3.0. Math.max(3.0, 2.5) gives 3.0.
(e) State the output of the following program segment:
String s = "Examination";
int n = s.length();
System.out.println(s.startsWith(s.substring(5, n)));
System.out.println(s.charAt(2) == s.charAt(6));
false
true
Putting the value of n in s.substring(5, n), it becomes s.substring(5, 11). This gives "nation". As s does not start with "nation" so s.startsWith(s.substring(5, n)) returns false.
s.charAt(2) is a and s.charAt(6) is also a so both are equal
(f) State the method that:
- Converts a string to a primitive float data type.
- Determines if the specified character is an uppercase character.
- Float.parseFloat()
- Character.isUpperCase()
(g) State the data type and values of a and b after the following segment is executed:
String s1 = "Computer", s2 = "Applications"
a = (s1.compareTo(s2));
b = (s1.equals(s2));
Data type of a is int and value is 2. Data type of b is boolean and value is false.
(h) What will the following code output:
String s = "malayalam";
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'));
0
8
The first m appears at index 0 and the last m appears at index 8.
(i) Rewrite the following program segment using while instead of for statement.
int f = 1, i;
for(i = 1; i <= 5; i++){
f *= i;
System.out.println(f);
}
int f = 1, i;
while (i <= 5) {
f *= i;
System.out.println(f);
i++;
}
(j) In the program given below, state the name and the value of the:
- method argument or argument variable
- class variable
- local variable
- instance variable
class MyClass{
static int x = 7;
int y = 2;
public static void main(String args[]){
MyClass obj = new MyClass();
System.out.println(x);
obj.sampleMethod(5);
int a = 6;
System.out.println(a);
}
void sampleMethod(int n){
System.out.println(n);
System.out.println(y);
}
}
- Method argument/Argument variable is n
- Class variable is x
- Local variable is a
- Instance variable is y