uestions 7. As input before branching IF or ELSE IF statement
accepts ___. 1. Which selection statements test only for equality? (a) boolean (b) int (a) if (c) float (d) char (b) switch 8. IF statement is also called a ___ statement in Java. (c) if & switch (a) boolean (b) conditional (d) none of the mentioned (c) iterative (d) optional 2. Which of the selection statements are used in 9. IF-ELSE statements are similar to ___ in java. Java? (a) C style (a) if() (b) for() (b) C++ Style (c) continue (d) break (c) Both C and C++ style 3. Which of the following loops will execute the (d) None body of loop even when condition controlling the 10. State TRUE or FALSE. Every IF statement should loop is false? be followed by an ELSE or ELSE-IF statement. (a) do-while (a) TRUE (b) FALSE (b) while 11. ELSE statement should be preceded by ___ (c) for statement in Java. (d) none of the mentioned (a) IF (b) ELSE IF 4. Jump statements that can skip processing the (c) IF or ELSE IF (d) None remainder of the code in its body for a particular 12. State TRUE or FALSE. A Single-Line comment is iteration are ________. allowed in between if () and Left Brace ( { ). (a) break (b) return (a) FALSE (b) TRUE (c) exit (d) continue 13. State TRUE or FALSE. IF statement code should 5. Choose the statement which is incorrect. be defined in between two Braces. (a) s witch statement is more efficient than a set of (a) FALSE (b) TRUE nested ifs 14. State TRUE or FALSE. Code which is inside an (b) two case constants in the same switch can ELSE statement should be surrounded by Braces. have identical values (a) FALSE (b) TRUE (c) switch statement can only test for equality, 15. State TRUE or FALSE An ELSE or ELSE-IF whereas if statement can evaluate any type of statement can not exist alone without IF statement boolean expression in Java. (d) it is possible to create a nested switch (a) FALSE (b) TRUE statements 16. In condition of IF statement assess to boolean 6. IF-ELSE statement is also called ___. only if, the expression contains? (a) Branching statement (a) logical operators (b) Control statement (b) relational operators (c) Block statements (c) boolean operands (d) ALL (d) All 17. When the condition of an IF-statement is false, 24. Choose the correct output of the Java program. which statement will be true . if(3>1) (a) IF block is executed. { (b) ELSE block is executed. 4; (c) Both IF and ELSE blocks are skipped. } (d) Both IF and ELSE blocks are executed. (a) 0 (b) 4 18. Maximum lines of code that can be written inside (c) Compiler error (d) None a Java style IF, ELSE or IF-ELSE block. 25. Choose the correct output of the Java program (a) 32 (b) 64 with IF statement: (c) 512 (d) None if(true) 19. How is an IF-ELSE statement better than a { SWITCH statement. (a) Checking for More-than condition break;
(b) Checking for Less-than condition System.out.println(“ELEPHANT”);
(c) Checking for Ranges } (d) All (a) No output (b) ELEPHANT 20. Maximum number of ELSE-IF statements that can (c) Compiler error (d) None present in between starting IF and ending ELSE 26. A SWITCH case statement is a ___ control statements can be _________. statement in Java. (a) 32 (b) 64 (a) Iteration (b) Loop (c) 128 (d) None (c) Selection (d) Jump 21. Choose the correct output of Java program with IF 27. The alternative to SWITCH in Java language: statement: (a) break, continue (b) for, while if(1) (c) if, else (d) goto, exit { 28. The keywords used to implement a SWITCH case System.out.println(“OK”); in Java language. } (a) switch, case (b) default (a) OK (b) No output (c) break (d) All (c) Compiler error (d) None 29. The parts of a SWITCH in java includes: 22. Choose the correct output of the Java program (a) switch input condition with IF-ELSE statements: (b) case constants if(TRUE) System.out.println(“GO”); (c) case statements
else (d) All
System.out.println(“STOP”); 30. _______ type of data as input is accepted by a (a) GO (b) STOP SWITCH statement .
(c) Compiler error (d) None (a) byte (b) short
23. Choose the correct output of the Java program: (c) int (d) All int a=10; 31. In Iterator, hasMoreElements() method of Enumeration has been changed to: if(a==9) (a) hasNextElement() (b) isNext() System.out.println(“OK “); (c) hasNext() (d) name remains same System.out.println(“MASTER”); 32. What is used by TreeSet internally to store else elements? System.out.println(“BYE”); (a) HashMap (b) LinkedHashMap (a) OK MASTER (b) BYE (c) TreeMap (c) Compiler error (d) None 33. Which statement is an iteration statement? (a) case keyword (b) break keyword (a) switch (b) if-else (c) default keyword (d) None (c) if (d) do-while 40. The main purpose to design a SWITCH logic with a fall-through is _________ in Java. Fill in the blanks (a) to define ranges (b) to define additions 34. ______ data type is accepted by a switch statement (c) to improve switch block performance in Java . (d) None (a) enum (b) String 41. ___________ for loop declaration is not valid. (c) enum and String (d) long (a) for ( int i = 99; i >= 0; i / 9 ) 35. ________ version of Java supports String as the (b) for ( int i = 7; i <= 77; i += 7 ) input data type of a SWITCH. (c) for ( int i = 20; i >= 2; - -i ) (a) JDK 5 (b) JDK 6 (d) for ( int i = 2; i <= 20; i = 2* i ) (c) JDK 7 (d) JDK 8 42. _______ is not a flow control statement in Java. 36. _________ is the output of Java program with (a) break (b) continue Switch. (c) exit() (d) return int a=10; 43. ________ is acceptable types for x switch(a) switch(x) { { case 10: System.out.println(“TEN”); default: } System.out.println(“Hello”); (a) No output } (b) TEN (a) short (b) char (c) Compiler error as there is no BREAK. (c) long (d) float (d) None 37. ________ is the output of the Java program . Match the following int b=20; 44. Match the columns: switch(b) Group A Group B { default: System.out.println(“LION”); 1. Loop statement (i) It has empty initialization } 2. Empty loop in (ii) Which is executed (a) No output Java repeatedly clause (b) LION 3. nested loop (iii) To terminate (c) Compiler error as there are no CASE current iteration statements. 4. Functionality of (iv) A Loop within a (d) None break statement Loop 38. _________ is the output of the Java program . (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) String animal = “GOAT”; (b) 1-(i), 2-(ii), 3-(iv), 4-(iii) switch(animal) (c) 1-(ii), 2-(i), 3-(iii), 4-(iv) { (d) 1-(ii), 2-(iv), 3-(i), 4-(iii) break: System.out.println(“DOMESTIC”); 45. Match the columns: } Group A Group B (a) No output (b) GOAT 1. Functionality (i) do while loop (c) DOMESTIC (d) Compiler error of continue runs once even 39. A SWITCH fall through occurs only in the absence statement conditional clause of ___ in Java. is false 2. Difference (ii) To continue current 3. Control (iii) you can force between do iteration expressions for an immediate while loop and if statement termination of while loop in the loop Java 4. Break statement (iv) Boolean 3. Switch statement (iii) Used inside the expression switch to terminate (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) a statement (b) 1-(i), 2-(ii), 3-(iv), 4-(iii) sequence. (c) 1-(ii), 2-(ii), 3-(iii), 4-(iv) 4. Break statement (iv) Dispatch execution (d) 1-(ii), 2-(iv), 3-(i), 4-(iii) to different parts of your code based 48. Match the columns: on the value of an Group A Group B expression. 1. do-while (i) select among a large group of (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) values (b) 1-(i), 2-(ii), 3-(iv), 4-(iii) 2. Switch (ii) Useful when you (c) 1-(ii), 2-(i), 3-(iii), 4-(iv) statement process a menu selection. (d) 1-(ii), 2-(iv), 3-(i), 4-(iii) 3. Default is an (iii) More than one else 46. Match the columns: optional case clause. Group A Group B 4. One if can have (iv) Value of the 1. Switch statement (i) switch can only expression does not test for equality match with any of 2. Conditional (ii) provides a better the case values. statement alternative than a (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) large series of if- (b) 1-(i), 2-(ii), 3-(iv), 4-(iii) else-if statements. (c) 1-(ii), 2-(i), 3-(iii), 4-(iv) 3. A continue (iii) in the same switch (d) 1-(ii), 2-(iv), 3-(i), 4-(iii) statement can have identical values Programming based questions 4. No two case (iv) control to be constants transferred directly 49. Choose valid data type for variable “a” to print to the conditional “Hello World”? expression switch(a) (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) { System.out.println(“Hello World”); (b) 1-(i), 2-(ii), 3-(iv), 4-(iii) } (c) 1-(ii), 2-(i), 3-(iii), 4-(iv) (a) int and float (d) 1-(ii), 2-(iv), 3-(i), 4-(iii) (b) byte and short 47. Match the columns: (c) char and long Group A Group B (d) byte and char 1. A switch (i) Cannot be 50. Will the given Java code-snippet be compiled? statement reference outside switch(45) the loop. { 2. Variable declared (ii) usually more case 10: ; inside the for loop efficient than a } control set of nested ifs. (a) NO (b) YES 51. Choose the correct output for code snippet if (a) Infinite (b) 9 variable a=10? (c) 0 (d) 4 if(a<=0) 56. Concerning the program code given below, { answer the questions that follow: if(a==0) //It is a program of odd and even numbers. { public class IfElse System.out.println(“1 “); { } public static void main(String[] args) else { { //defining a variable System.out.println(“2 “); int number=13; } //Check if the number is divisible by 2 or not } if(number%2==0){ System.out.println(“3 “); System.out.println(“even number”); (a) 1 2 (b) 2 3 } (c) 1 3 (d) 3 else 52. How many times will the loop be executed? { ch = ‘b’; System.out.println(“odd number”); while(ch >= ‘a’ && ch <= ‘z’) } (a) 0 (b) 25 } (c) 26 (d) 1 (a) What will be the output? 53. Choose the correct output of the Code. (i) odd number void main() (ii) even number { (iii) whole number static a,b; (iv) natural number while(a > b++) (b) What is the name of the class? } (i) System.out.println (a) a=0 b=0 (ii) static void main() (b) a=0 b=0 (iii) public
(c) a=1 b=1 (iv) class IfElse
57. Concerning the program code given below, (d) none of the mentioned answer the questions that follow: 54. Which of the following loops will execute the public class LeapYear { body of loop even when condition controlling the loop is initially false? public static void main(String[] args) {
(a) do-while int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year (b) while % 400==0)){ (c) for System.out.println(“LEAP YEAR”); (d) None of the mentioned } 55. Identify how many times the loop will run? else{ for(digit = 0;digit < 9; digit++) System.out.println(“COMMON YEAR”); { } digit = digit *2; } digit--; } } (a) What will be the output? (i) LEAP YEAR (i) checking the number is odd or even (ii) COMMON YEAR (ii) checking the year is leap year or common (iii) WHOLE number year (iv) NATURAL number (iii) checking number is divisible by 4 or 400 (b) What this program is doing? (iv) giving remainder after dividing by 4
Answers
Multiple choice questions 9. (c) Both C and C++ style
10. (b) FALSE 1. (b) switch 11. (c) IF or ELSE IF Explanation: Use the switch statement to select 12. (b) TRUE one of many code blocks to be executed. 13. (a) FALSE Syntax 14. (b) TRUE switch(expression) { 15. (b) TRUE case x: 16. (d) All // code block 17. (d) Both IF and ELSE blocks are executed. break; 18. (d) None case y: 19. (d) All // code block 20. (c) 128 break; 21. (c) Compiler error default: Explanation: Compile Time Errors are those errors // code block which prevent the code from running because of } an incorrect syntax such as a missing semicolon at 2. (a) if() the end of a statement or a missing bracket, class Explanation: Use the if statement to specify a not found, etc. These kind of errors are easy to block of Java code to be executed if a condition is spot and rectify because the java compiler finds true. them for you. 3. (a) do-while 22. (c) Compiler error Explanation: The do/while loop is a variant of the Explanation: Compile Time Errors are those errors while loop. This loop will execute the code block which prevent the code from running because of once, before checking if the condition is true, then an incorrect syntax such as a missing semicolon it will repeat the loop as long as the condition is at the end of a statement or a missing bracket, true. class not found, etc.These kind of errors are easy 4. (d) continue to spot and rectify because the java compiler finds them for you. Explanation: The continue statement breaks one iteration (in the loop), if a specified condition 23. (c) Compiler error occurs, and continues with the next iteration in 24. (c) Compiler error the loop. 25. (c) Compiler error 5. (b) two case constants in the same switch can 26. (c) Selection have identical values Explanation: Selection statements allow you to 6. (d) ALL control the flow of program execution, on the 7. (a) boolean basis of the outcome of an expression or state 8. (b) conditional of a variable, known during runtime. Selection statements can be divided into the following Explanation: Use else if to specify a new condition categories: The if and if-else statements. The if- to test, if the first condition is false. else statements. 27. (c) if, else Match the following 28. (d) All 29. (d) All 44. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
30. (d) All 45. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
46. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) 31. (c) hasNext() 47. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) 32. (c) TreeMap 48. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii) 33. (d) do-while Programming based questions Fill in the blanks 49. (d) byte and char 34. (c) enum and String 50. (b) YES 35. (c) JDK 7 51. (d) 3 36. (b) TEN 52. (b) 25 37. (b) LION 53. (a) a=0 b=0
38. (d) Compiler error 54. (a) do-while
55. (a) Infinite 39. (b) break keyword 56. (a) (i) odd number 40. (a) To define ranges. (b) (iv) class IfElse 41. (a) for ( int i = 99; i >= 0; i / 9 ) 57. (a) (i) LEAP YEAR 42. (c) exit() (b) (ii) checking the year is leap year or common 43. (a) short year