(a) Write a Java expression for the following:
$\bold{\frac{\sqrt{3x+x^2}}{a + b}}$
Show SolutionHide Solution↓
Math.sqrt(3 * x + x * x) / (a + b)
(b) What is the value of y after evaluating the expression given below?
y+= ++y + y-- + --y; when int y=8
y+= ++y + y-- + --y
⇒ y = y + (++y + y-- + --y)
⇒ y = 8 + (9 + 9 + 7)
⇒ y = 8 + 25
⇒ y = 33
(c) Give the output of the following:
- Math.floor (-4.7)
- Math.ceil(3.4) + Math.pow(2, 3)
- -5.0
- 12.0
(d) Write two characteristics of a constructor.
Two characteristics of a constructor are:
- A constructor has the same name as its class.
- A constructor does not have a return type.
(e) Write the output for the following:
System.out.println("Incredible"+"\n"+"world");
Output of the above code is:
Incredible
world
\n is the escape sequence for newline so Incredible and world are printed on two different lines.
(f) Convert the following if else if construct into switch case
if( var==1)
System.out.println("good");
else if(var==2)
System.out.println("better");
else if(var==3)
System.out.println("best");
else
System.out.println("invalid");
switch (var) {
case 1:
System.out.println("good");
break;
case 2:
System.out.println("better");
break;
case 3:
System.out.println("best");
break;
default:
System.out.println("invalid");
}
(g) Give the output of the following string functions:
- "ACHIEVEMENT".replace('E', 'A')
- "DEDICATE".compareTo("DEVOTE")
- ACHIAVAMANT
- -18
- "ACHIEVEMENT".replace('E', 'A') will replace all the E's in ACHIEVEMENT with A's.
- The first letters that are different in DEDICATE and DEVOTE are D and V respectively. So the output will be:
ASCII code of D - ASCII code of V
⇒ 68 - 86
⇒ -18
(h) Consider the following String array and give the output
String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};
System.out.println(arr[0].length() > arr[3].length());
System.out.print(arr[4].substring(0,3));
Output of the above code is:
false
JAI
- arr[0] is DELHI and arr[3] is LUCKNOW. Length of DELHI is 5 and LUCKNOW is 7. As 5 > 7 is false so the output is false.
- arr[4] is JAIPUR. arr[4].substring(0,3) extracts the characters from index 0 till index 2 of JAIPUR so JAI is the output.
(i) Rewrite the following using ternary operator:
if (bill > 10000 ) discount = bill * 10.0/100; else discount = bill * 5.0/100;
discount = bill > 10000 ? bill * 10.0/100 : bill * 5.0/100;
(j) Give the output of the following program segment and also mention how many times the loop is executed:
int i; for ( i = 5 ; i > 10; i ++ ) System.out.println( i ); System.out.println( i * 4 );
Output of the above code is:
20
Loop executes 0 times.
In the loop, i is initialized to 5 but the condition of the loop is i > 10. As 5 is less than 10 so the condition of the loop is false and it is not executed. There are no curly braces after the loop which means that the statement System.out.println( i ); is inside the loop and the statement System.out.println( i * 4 ); is outside the loop. Loop is not executed so System.out.println( i ); is not executed but System.out.println( i * 4 ); being outside the loop is executed and prints the output as 20 (i * 4 ⇒ 5 * 4 ⇒ 20).