Question 2(i) Write the Java expression for (p + q)2 [2](ii) Evaluate the expression when the value of x = 2:x = x ++ + ++ x + x[2](iii) The following code segment should print “You can go out” if you have doneyour homework (dh) and cleaned your room(cr). However, the code has errors.Fix the code so that it compiles and runs correctly.boolean dh = True;boolean cr= true;if (dh && cr)System.out.println("You cannot go out");elseSystem.out.println("You can go out");[2]T24 861 – SPECIMEN 7 of 10(iv) Sam executes the following program segment and the answer displayed is zeroirrespective of any non zero values are given. Name the error. How the programcan be modified to get the correct answer?void triangle(double b, double h){ double a;a = ½ * b * h;System.out.println(“Area=”+a); }[2](v) How many times will the following loop execute? What value will be returned?int x=2;int y=50;do{++x;y-=x++;}while(x<=10);return y;[2](vi) Write the output of the following String methods:(a) "ARTIFICIAL ".indexOf('I' )(b) “DOG and PUPPY”. trim().length()[2](vii) Name any two jump statements. [2](viii) Predict the output of the following code snippet:String a="20";String b="23";int p=Integer.parseInt(a);int q=Integer.parseInt(b);System.out.print(a+"*"+b);[2](ix) When there is no explicit initialization, what are the default values set forvariables in the following cases?(a) Integer variable(b) String variable[2]T24 861 – SPECIMEN 8 of 10(x) int P [ ]={ 12,14,16,18}; int Q[ ]={ 20,22,24};Place all elements of P array and Q array in the array R one after the other.(a) What will be the size of array R [ ] ?(b) Write index position of first and last element?
Question
Question 2(i) Write the Java expression for (p + q)2 2 Evaluate the expression when the value of x = 2:x = x ++ + ++ x + x2 The following code segment should print “You can go out” if you have doneyour homework (dh) and cleaned your room(cr). However, the code has errors.Fix the code so that it compiles and runs correctly.boolean dh = True;boolean cr= true;if (dh && cr)System.out.println("You cannot go out");elseSystem.out.println("You can go out");[2]T24 861 – SPECIMEN 7 of 10(iv) Sam executes the following program segment and the answer displayed is zeroirrespective of any non zero values are given. Name the error. How the programcan be modified to get the correct answer?void triangle(double b, double h){ double a;a = ½ * b * h;System.out.println(“Area=”+a); }2 How many times will the following loop execute? What value will be returned?int x=2;int y=50;do{++x;y-=x++;}while(x<=10);return y;2 Write the output of the following String methods:(a) "ARTIFICIAL ".indexOf('I' )(b) “DOG and PUPPY”. trim().length()2 Name any two jump statements. 2 Predict the output of the following code snippet:String a="20";String b="23";int p=Integer.parseInt(a);int q=Integer.parseInt(b);System.out.print(a+"*"+b);2 When there is no explicit initialization, what are the default values set forvariables in the following cases?(a) Integer variable(b) String variable[2]T24 861 – SPECIMEN 8 of 10(x) int P [ ]={ 12,14,16,18}; int Q[ ]={ 20,22,24};Place all elements of P array and Q array in the array R one after the other.(a) What will be the size of array R [ ] ?(b) Write index position of first and last element?
Solution
(i) The Java expression for (p + q)2 is: Math.pow((p + q), 2)
(ii) The expression when the value of x = 2: x = x++ + ++x + x; is not valid in Java. The correct expression could be: x = x++ + ++x; Then x would be 5.
(iii) The corrected code is:
boolean dh = true;
boolean cr= true;
if (dh && cr)
System.out.println("You can go out");
else
System.out.println("You cannot go out");
(iv) The error in the program is that ½ is interpreted as an integer division and results in 0. The correct code is:
void triangle(double b, double h){
double a;
a = 0.5 * b * h;
System.out.println("Area="+a);
}
(v) The loop will execute 9 times. The returned value will be 11.
(vi) (a) "ARTIFICIAL ".indexOf('I' ) will output 1. (b) “DOG and PUPPY”. trim().length() will output 12.
(vii) Two jump statements in Java are break and continue.
(viii) The output of the following code snippet will be "20*23".
(ix) (a) The default value for an Integer variable is 0. (b) The default value for a String variable is null.
(x) (a) The size of array R[] will be 7. (b) The index position of the first element is 0 and the last element is 6.
Similar Questions
Select the correct answerWhat will be the output of the following Java program? class access { public int x; static int y; void cal(int a, int b) { x += a ; y += b; } } class static_specifier { public static void main(String args[]) { access obj1 = new access(); access obj2 = new access(); obj1.x = 0; obj1.y = 0; obj1.cal(1, 2); obj2.x = 0; obj2.cal(2, 3); System.out.println(obj1.x + " " + obj2.y); } }Options3 22 31 21 5
What is the output of the following Java program?
answerWhat will be the output of the following Java program?class output_odd { public static void main(String args[]) { int p = 2; int q = 0; for ( ; q < 8; ++q) { if (q % p == 0) continue; else if (q == 6) break; else System.out.print(q + " "); } } }
Analyze the following pieces of code carefully. Code 1:int number = 45;boolean even;if (number % 2 == 0) even = true;else even = false;Code 2:int number = 45;boolean even = (number % 2 == 0);Group of answer choicesBoth Code 1 and Code 2 are correct, but Code 2 is better.Both Code 1 and Code 2 have compile errors.Code 2 has compile errors.Code 1 has compile errors.
Select the correct answerWhat will be the output of the following Java program?class java { public static void main(String args[]) { int var1 = 3; int var2 = 8; if ((var2 = 2) == var1) System.out.print(var2); else System.out.print(++var2); } }Options4213
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.