Which of the following lines of code will compile without error?Code 1: Code 2: Code 3: Code 4:int i=0;if(i) {System.out.println("Hello");}boolean b=true;boolean b2=true;if(b==b2) {System.out.println("So true");int i=1;int j=2;if(i=1|| j=2)System.out.println("OK");int i=1;int j=2;if(i==1 && j==2)System.out.println("OK");A. Code 1 B. Code 2 C. Code 3 D. Code 4 E. Code 2&4 both F. Code 2&3 both
Question
Which of the following lines of code will compile without error?Code 1: Code 2: Code 3: Code 4:int i=0;if(i) {System.out.println("Hello");}boolean b=true;boolean b2=true;if(b==b2) {System.out.println("So true");int i=1;int j=2;if(i=1|| j=2)System.out.println("OK");int i=1;int j=2;if(i==1 && j==2)System.out.println("OK");A. Code 1 B. Code 2 C. Code 3 D. Code 4 E. Code 2&4 both F. Code 2&3 both
Solution
The correct answer is E. Code 2 and Code 4.
Explanation:
Code 1: This code will not compile because in Java, the condition in an if statement must be a boolean expression. 'i' is an integer, not a boolean.
Code 2: This code will compile without error. The condition in the if statement is a boolean expression, which is required in Java.
Code 3: This code will not compile because the assignment operator (=) is used instead of the equality operator (==) in the if statement. Also, the logical OR operator (||) is used incorrectly.
Code 4: This code will compile without error. The condition in the if statement is a boolean expression, which is required in Java. The equality operator (==) and the logical AND operator (&&) are used correctly.
Similar Questions
47. The following code will point1.if(new Boolean("true") == new Boolean("true"))2. System.out.println("True");3. else4. System.out.println("False");A. Compilation errorB. No compilation error , but runtime exceptionC. Prints "True"D. Prints "False"E. Depends on the particular implementation of the Java Virtual Machine
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.
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); Code 2 has compile errors. Both Code 1 and Code 2 have compile errors. Code 1 has compile errors. Correct! Both Code 1 and Code 2 are correct, but Code 2 is better. Question 8Tips0 / 1 ptsWhat is y after the following switch statement is executed? int x = 3; int y = 4;switch (x + 3) { case 6 -> y = 0; case 7 -> y = 1; default -> y += 1;} 2 You Answered 5 1 Correct answer 0 Question 9Tips1 / 1 ptsCheck the following Java program carefully. What should be written in the blank? Choose from the given options. public class Test { public static void main(String[] args) { System.out.print("The grade is " + getGrade(78.5)); System.out.print("\nThe grade is " + getGrade(59.5)); } public static ________ getGrade(double score) { if (score >= 90.0) return 'A'; else if (score >= 80.0) return 'B'; else if (score >= 70.0) return 'C'; else if (score >= 60.0) return 'D'; else return 'F'; }} int Correct! char void String Question 10Tips1 / 1 ptsConsider the following incomplete code. The Missing body indicated by the comment in the method ‘display’ should be ___. public class Test { public static void main(String[] args) { System.out.println(display(5)); } public static int display(int number) { // Missing body }} System.out.println(number); System.out.println("number"); return "number"; Correct! return number; Question 11Tips1 / 1 pts Given the following method and the statements below, what is k after invoking nPrint("A message", k)? static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; }}int k = 2;nPrint("A message", k); 1 0 Correct! 2 3 Question 12Tips0 / 1 ptsThe statement System.out.printf("%3.1f", 1234.56) outputs __. A) C) D) E) B) Invalid statement. Gives error. 1234.56 123.4 Correct answer 1234.6 You Answered 1234.5 Question 13Tips0 / 1 ptsWhich of the following is correct to obtain a random integer between 5 and 10 (inclusive)?You Answered 5 + Math.random() * 6 5 + (int)(Math.random() * 5) 5 + Math.random() * 5 Correct answer 5 + (int)(Math.random() * 6) Question 14Tips1 / 1 ptsWhat is the output of the programme below?public static void main(String[] args) { int x = 0; while (x < 10) { if (x & 2 == 0) { System.out.print(x + “ “); } x++; }} 0 1 2 3 4 Correct! 0 2 4 6 8 2 4 6 8 10 1 3 5 7 9 Question 15Tips1 / 1 ptsConsider the program below: public static void main(String[] args) { int numThings = 0; for (int i = 0; i <= 10; i+=3) { numThings++; } }How many times does the body of the for loop run? 9 10 0 Correct! 4 Quiz score: 11 out of 15Submission details:
Which of the following Java program statements gives a compilation error?1. public class MyProgram {2. public static void main (String[] args) {3. int x = 5;4. int y = 7;5. product = x * y;6. System.out.println(product);7. }8. }Group of answer choicesNone. The program is correct.Line 2Line 5Line 6
What will be the result of compiling and runnig the following code:public class Test{ public static void main(String... args) throws Exception{ Integer i = 34; int l = 34; if(i.equals(l)){ System.out.println("true"); }else{ System.out.println("false"); } }} truefalseCompiler error None of thes
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.