(a) State the data type and value of y after the following is executed:
char x='7'; y=Character.isLetter(x);
Show SolutionHide Solution↓
Data type of y is boolean and value is false. Character.isLetter() method checks if its argument is a letter or not and as 7 is a number not a letter hence it returns false.
(b) What is the function of catch block in exception handling ? Where does it appear in a program ?
Catch block contains statements that we want to execute in case an exception is thrown in the try block. Catch block appears immediately after the try block in a program.
(c) State the output when the following program segment is executed:
String a ="Smartphone", b="Graphic Art"; String h=a.substring(2, 5); String k=b.substring(8).toUpperCase(); System.out.println(h); System.out.println(k.equalsIgnoreCase(h));
The output of the above code is:
art
true
a.substring(2, 5) extracts the characters of string a starting from index 2 till index 4. So h contains the string "art". b.substring(8) extracts characters of b from index 8 till the end so it returns "Art". toUpperCase() converts it into uppercase. Thus string "ART" gets assigned to k. Values of h and k are "art" and "ART", respectively. As both h and k differ in case only hence equalsIgnoreCase returns true.
(d) The access specifier that gives the most accessibility is ________ and the least accessibility is ________.
The access specifier that gives the most accessibility is public and the least accessibility is private.
(e) (i) Name the mathematical function which is used to find sine of an angle given in radians.
(ii) Name a string function which removes the blank spaces provided in the prefix and suffix of a string.
(i) Math.sin() (ii) trim()
(f) (i) What will this code print ?
int arr[]=new int[5]; System.out.println(arr);
- 0
- value stored in arr[0]
- 0000
- garbage value
This code will print garbage value
(ii) Name the keyword which is used- to resolve the conflict between method parameter and instance variables/fields.
this keyword
(g) State the package that contains the class:
- BufferedReader
- Scanner
- java.io
- java.util
(h) Write the output of the following program code:
char ch ;
int x=97;
do
{
ch=(char)x;
System.out.print(ch + " " );
if(x%10 == 0)
break;
++x;
} while(x<=100);
Answer
The output of the above code is:
a b c d
The below table shows the dry run of the program:
| x | ch | Output | Remarks |
|---|---|---|---|
| 97 | a | a | 1st Iteration |
| 98 | b | a b | 2nd Iteration |
| 99 | c | a b c | 3rd Iteration |
| 100 | d | a b c d | 4th Iteration — As x%10 becomes true, break statement is executed and exists the loop. |
(i) Write the Java expressions for:
$\frac{a^2+b^2}{2ab}$
(a * a + b * b) / (2 * a * b)
(j) If int y = 10 then find int z = (++y * (y++ + 5));
z = (++y * (y++ + 5))
⇒ z = (11 * (11 + 5))
⇒ z = (11 * 16)
⇒ z = 176