Write the java statement for:
Sum of a raised to b and cuberoot of c
Answer
Java statement is:
Math.pow(a, b) + Math.cbrt(c);
Name the following:
(a) Method with the same name as of the class and is invoked every time an object is created.
(b) Keyword to access the classes of a package.
Answer
(a) Constructor
(b) import
Name the following:
(a) A Character method that checks whether a character is an alphabet or a number.
(b) A Math method that does not have an argument.
Answer
(a) Character.isLetterOrDigit()
(b) Math.random()
Cyber police wanted to investigate a case; they wanted to check a list of phone numbers that had 945 anywhere in the phone number. Example: 7394567938, 7685483945.., a method was created to convert the phone number in long data type to a string and check for the existence of the number 945. Fill in the blanks (a) and (b) in the given method with appropriate Java statements:
void check(long pno)
{ String s = _______(a)_________;
if(______(b)_______)
System.out.println(pno);
} Answer
void check(long pno)
{ String s = String.valueOf(pno);
if(s.indexOf("945") >= 0)
System.out.println(pno);
} A manager wants to check the number of employees with names ending with KUMAR, and fill in the blanks in the given program segment with appropriate Java statements:
void count(String s[])
{
int i, l=_________, c=0;
for(i=0;i<l;i++)
{
if(________________)
c++;
}
System.out.println(c);
} Answer
void count(String s[])
{
int i, l = s.length, c = 0;
for(i = 0; i < l; i++)
{
if(s[i].endsWith("KUMAR"))
c++;
}
System.out.println(c);
} The output of a program which extracts a part of the string "COMPASSION" is as follows:
(a) "PASS"
(b) "PASSION"
Write appropriate Java statements to get the above outputs.
Answer
(a) "COMPASSION".substring(3, 7);
(b) "COMPASSION".substring(3);
Give the output of the following program segment:
for(k=a;k<=a*b;k+=a)
{ if (k%b==0)
break;
}
System.out.println(k);Give the output when a = 6, b = 4.
Answer
12
1. The loop starts with k = 6.
2. The loop runs while k <= 6 * 4, i.e., k <= 24.
3. In each iteration, k increases by 6.
4. The loop checks if k % 4 == 0. If true, it breaks the loop.
Iterations:
- First:
k = 6,6 % 4 = 2→ Not 0 → Loop continues - Second:
k = 12,12 % 4 = 0→ Condition true → Loop breaks
5. The current value of k is 12. So, the output is: 12.
Kamal wants to display only the alphabets stored in a character array. While compiling the code, an error was displayed. Check for the statement with the error and write the correct statement.
char arr[]={'4', '&', 'a', 'w', 'd'};
int i;
for(i=0;i<arr.length;i++)
{
if(Character.isLetter(arr))
System.out.println(arr);
}Answer
char arr[]={'4', '&', 'a', 'w', 'd'};
int i;
for(i=0;i<arr.length;i++)
{
if(Character.isLetter(arr)) //Error 1
System.out.println(arr); // Error 2
}1. In Character.isLetter(arr), arr is the entire array, but the method expects a single character (char), not an array.
2. In System.out.println(arr), arr prints the whole array, not the individual character.
The corrected code is as follows:
char arr[] = {'4', '&', 'a', 'w', 'd'};
int i;
for(i = 0; i < arr.length; i++)
{
if(Character.isLetter(arr[i]))
System.out.println(arr[i]);
}Name the type of type casting in the given statements:
(a) double m = 'b';
(b) char ch = (char)68;
Answer
(a) Implicit type casting
(b) Explicit type casting
Write Java statements for the following:
(a) To assign the cube root of -343 to a variable with the appropriate datatype.
(b) To assign the position of the last occurrence of @ in the String s with the appropriate datatype.
Answer
(a) double cubeRoot = Math.cbrt(-343);
(b) int pos = s.lastIndexOf('@');
Mention the output of this code snippet:
int lives = 5;
System.out.println(lives--);
System.out.println(lives);Answer
5
4
- The variable
livesis initialized to 5. - The statement
System.out.println(lives--);first prints the current value oflives, which is 5, and then decreases the value oflivesby 1 becauselives--is the postfix decrement operator. - After this statement, the value of
livesbecomes 4. - The next statement
System.out.println(lives);prints the updated value oflives, which is 4.
Convert the following for loop segment to an exit-controlled loop.
for(k = 10;k >= -1;k--)
System.out.println(k*2);
System.out.println(k*4); Answer
k = 10;
do {
System.out.println(k * 2);
k--;
} while (k >= -1);
System.out.println(k * 4);Give the output of the following program segment:
int x[]={2,45,7,67,12,3};
int i, t=0;
for(i=0, t=5;i<3;i++,t--)
{
System.out.println(x[i]+x[t]);
}Answer
5
57
74
1. Array x = [2, 45, 7, 67, 12, 3].
2. Initial values: i = 0, t = 5.
3. Loop condition: i < 3 (loop runs for i = 0, 1, 2)
Iteration 1:
i = 0, t = 5x[0] + x[5] = 2 + 3 = 5- Print:
5 - After iteration:
i = 1, t = 4
Iteration 2:
i = 1, t = 4x[1] + x[4] = 45 + 12 = 57- Print:
57 - After iteration:
i = 2, t = 3
Iteration 3:
i = 2, t = 3x[2] + x[3] = 7 + 67 = 74- Print:
74 - After iteration:
i = 3, t = 2
Loop ends because i is now 3 (not less than 3).
4. So the output is:
5
57
74
Write Java statements for the following:
(a) Initialise the array with the three favourite subjects.
(b) Declare an array to store the marks in 3 subjects of 40 students.
Answer
(a) String subjects[] = {"Maths", "Science", "English"};
(b) int marks[][] = new int[40][3]; or int marks[][] = new int[3][40];
A Student executes the given program segment and it results in 1.0, irrespective of the value of n. State the type of the error, write the correct statement:
void solve(int n)
{ double power=Math.pow(n, 2/3);
System.out.println(power);
}Answer
The type of error is logical error.
The correct statement is:
double power = Math.pow(n, 2.0 / 3);
Explanation — In Java, 2/3 uses integer division, which equals 0. So the code computes Math.pow(n, 0), and any number to the power 0 is 1.0, regardless of n. Make the exponent a floating-point value—e.g., 2.0/3 or 2/3.0 or 2.0/3.0—so it evaluates to 0.666… and correctly computes .
The corrected code is as follows:
void solve(int n)
{ double power = Math.pow(n, 2.0 / 3);
System.out.println(power);
}Consider the following program segment and answer the questions given:
for(int k=1;k<=5;k++)
System.out.println(k);
System.out.println(k);(a) Will the program segment get executed successfully?
(b) If not, state the type of error?
(c) How do you correct the program if it has any error?
Answer
(a) No, the program segment will not execute successfully.
(b) The error is a syntax error because the variable k is not known outside the loop. The variable k is created inside the for loop and only exists there. When the program tries to use k after the loop, it does not recognize it, so the program gives an error.
(c) To fix the error, declare k before the loop:
int k;
for (k = 1; k <= 5; k++)
System.out.println(k);
System.out.println(k);Consider the array:
int a[]={12, 35, 40, 22, 56, 9, 70};(a) In the above given array, using linear search, how many iterations are required to check for the existence of the value 56?
(b) If the array is arranged in descending order, how many iterations are required to check for the existence of 56 using linear search?
Answer
(a) 5
(b) 2
Explanation
(a) In linear search, we check each element of the array one by one starting from the first element. We compare each element with the value we want to find, which is 56 in this case. The search will check 12, then 35, then 40, then 22, and finally 56. Since 56 is the fifth element, it takes 5 iterations to find it.
(b) If the array is sorted in descending order, it will look like this: {70, 56, 40, 35, 22, 12, 9}. Now, when we do linear search, we again start from the first element and check each element one by one. The search will check 70 first and then 56, which is the second element. So, it takes only 2 iterations to find 56 in the sorted array.
Evaluate the expression:
3 * 6 % 5 * 4 / 2 * 7 % 5
Answer
Step 1: Multiplication and Modulus have same precedence, so we evaluate left to right.
- 3 * 6 = 18
- 18 % 5 = 3 (because 18 ÷ 5 leaves remainder 3)
Expression now: 3 * 4 / 2 * 7 % 5
Step 2: Continue left to right (multiplication, division, modulus have same precedence):
- 3 * 4 = 12
- 12 / 2 = 6
- 6 * 7 = 42
- 42 % 5 = 2 (since 42 ÷ 5 leaves remainder 2)
Final result: 2
Consider the following program which calculates the Norm of a matrix. Norm is square root of sum of squares of all elements.
Fill in the blanks (a) and (b) with appropriate java statements:
double norm()
{ int x[][]={{1,5,6},{4,2,9},{6,1,3}};
int r, c, sum=0;
for(r=0;r<3;r++)
{ for(c=0;c<3;c++)
sum=sum+_____(a)_____;
}
____(b)________;
}Answer
double norm()
{ int x[][]={{1,5,6},{4,2,9},{6,1,3}};
int r, c, sum=0;
for(r=0;r<3;r++)
{ for(c=0;c<3;c++)
sum=sum+x[r][c] * x[r][c];
}
return Math.sqrt(sum);
}Give the output of the following program segment:
for(r=1;r<=3;r++)
{ for(c=1;c<r;c++)
{ System.out.println(r);
}
}Answer
2
3
3
When
r = 1:- Inner loop runs for
cfrom 1 to less than 1 → No iterations (becausec < ris false at c=1) - So no output for r=1.
- Inner loop runs for
When
r = 2:- Inner loop runs for
c = 1(since1 < 2) - Prints 2 once.
- Inner loop runs for
When
r = 3:- Inner loop runs for
c = 1andc = 2(sincec < 3) - Prints
3twice.
- Inner loop runs for
Give the output of the following program segment:
void encode()
{
String s= "DICe";
char ch;
for(i=0;i<4;i++)
{ ch=s.charAt(i);
if("AEIOUaeiou".indexOf(ch)>=0)
System.out.println("@");
else
System.out.println(ch);
} Answer
The given code snippet is missing the closing curly brace } for the method encode().
The corrected code is as follows:
void encode()
{
String s= "DICe";
char ch;
for(i=0;i<4;i++)
{ ch=s.charAt(i);
if("AEIOUaeiou".indexOf(ch)>=0)
System.out.println("@");
else
System.out.println(ch);
}
}D
@
C
@
1. void encode() — Declares a method named encode that does not return any value (void).
2. String s = "DICe"; — Inside the method, a String variable s is initialized with the value "DICe".
3. char ch; — Declares a variable ch of type char which will be used to store each character of the string during the loop.
4. for(i=0;i<4;i++) — Starts a for loop where the integer variable i begins at 0.
- The loop continues as long as
iis less than 4. - After each iteration,
iis incremented by 1.
5. ch = s.charAt(i); — Inside the loop, the character at position i in the string s is extracted using charAt(i) and stored in the variable ch.
6. if("AEIOUaeiou".indexOf(ch) >= 0) — This if statement checks whether the character ch is a vowel.
- The string
"AEIOUaeiou"contains all uppercase and lowercase vowels. - The method
indexOf(ch)returns the position ofchin this string if found; otherwise, it returns-1. - If the return value is 0 or greater (meaning
chis found in the string), the condition evaluates to true.
7. System.out.println("@"); — If the condition is true (i.e., ch is a vowel), this line prints the symbol "@". 8. System.out.println(ch); — If the character is not a vowel, the original character ch is printed.
Name the following:
(a) What is the main condition to perform binary search on an array?
(b) A sort method in which consecutive elements are NOT compared.
Answer
(a) The array must be sorted.
(b) Selection sort.
Answer the following:
(a) Scanner class method to accept a character.
(b) Jump statement which stops the execution of a construct.
Answer
(a) The Scanner class method to accept a character is <Scanner object>.next().charAt(0).
(b) The jump statement which stops the execution of a construct is break.
How many times the given loop is executed? Give the output of the same.
for(k=10;k<=20;k+=4)
{
System.out.println(k);
if(k%3==0) continue;
} Answer
The loop exectes 3 times.
Output
10
14
18
Explanation
The loop starts from k = 10 and runs as long as k <= 20, increasing k by 4 each time. So, the values of k will be: 10, 14, and 18. When k becomes 22, the condition k <= 20 becomes false, so the loop stops. Hence, the loop runs 3 times.