(a) Give the output of the following string functions:
- (a)"MISSISSIPPI".indexOf('S') + "MISSISSIPPI".lastIndexOf('I')
- (b)"CABLE".compareTo("CADET")
Show SolutionHide Solution↓
- 2 + 10 = 12
- ASCII Code of B - ASCII Code of D
⇒ 66 - 68
⇒ -2
(b) Give the output of the following Math functions:
- Math.ceil(4.2)
- Math.abs(-4)
- 5.0
- 4
(c) What is a parameterized constructor ?
A Parameterised constructor receives parameters at the time of creating an object and initializes the object with the received values.
(d) Write down java expression for:
$T = \sqrt{A^2 + B^2 + C^2}$
T = Math.sqrt(a*a + b*b + c*c)
(e) Rewrite the following using ternary operator:
if (x%2 == 0)
System.out.print("EVEN");
else
System.out.print("ODD");
System.out.print(x%2 == 0 ? "EVEN" : "ODD");
(f) Convert the following while loop to the corresponding for loop:
int m = 5, n = 10;
while (n>=1)
{
System.out.println(m*n);
n--;
}
int m = 5;
for (int n = 10; n >= 1; n--) {
System.out.println(m*n);
}
(g) Write one difference between primitive data types and composite data types.
Primitive Data Types are built-in data types defined by Java language specification whereas Composite Data Types are defined by the programmer.
(h) Analyze the given program segment and answer the following questions:
- Write the output of the program segment.
- How many times does the body of the loop gets executed ?
for(int m = 5; m <= 20; m += 5)
{ if(m % 3 == 0)
break;
else
if(m % 5 == 0)
System.out.println(m);
continue;
}
5
10
Loop executes 3 times.
Below dry run of the loop explains its working.
| m | Output | Remarks |
|---|---|---|
| 5 | 5 | 1st Iteration |
| 10 | 5 10 | 2nd Iteration |
| 15 | 5 10 | 3rd Iteration — As m % 3 becomes true, break statement exists the loop. |
(i) Give the output of the following expression:
a+= a++ + ++a + --a + a-- ; when a = 7
a+= a++ + ++a + --a + a--
⇒ a = a + (a++ + ++a + --a + a--)
⇒ a = 7 + (7 + 9 + 8 + 8)
⇒ a = 7 + 32
⇒ a = 39
(j) Write the return type of the following library functions:
- isLetterOrDigit(char)
- replace(char, char)
- boolean
- String