Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Switch Case in Java Interview MCQ Questions and Answers

Switch Case in Java

Table of Contents

    • Switch Case in Java
    • Java Switch case uses the following predefined keywords.
  • Control Statement Switch Case in Java
    • Switch Case Fall Through (Range Defining)
    • Switch Case INPUT Types
    • Example: Switch with enums
    • Switch Case Errors in Java
    • Switch Case Vs IF-ELSE-IF Ladder Performance
  • Switch Case in Java Interview MCQ
    • 1) A SWITCH case statement in Java is a ___ control statement.
    • 2) Which is the alternative to SWITCH in Java language?
    • 3) What are the keywords used to implement a SWITCH case in Java language?
    • 4) What are the parts of a SWITCH in java?
    • 5) A SWITCH statement accepts ___ type of data as input.
    • 6) A switch statement in Java accepts ___ as input data.
    • 8) Which version of Java did start supporting String as the input data type of a SWITCH?
    • 17) A SWITCH fall through occurs in Java only in the absence of ___.
    • 18) What is the purpose of designing a SWITCH logic with a fall-through in Java?

Study and learn Interview MCQ Questions and Answers on Java Switch Case Statements. Attend job interviews easily with these Multiple Choice Questions.

Switch Case in Java

A Java language provides an alternative to the IF ELSE-IF ladder in the form of SWITCH CASE.

Java Switch case uses the following predefined keywords.

  • switch
  • case
  • default
  • break

Control Statement Switch Case in Java

A SWITCH case allows a programmer to define a set of conditions in the form of constants. So, it is also called a multi-condition if-else-if ladder.

SWITCH case has the following parts.

  1. SWITCH INPUT
  2. CASE constants
  3. CASE statements

Syntax:

switch(input)
{
  case constant1: //statements
  case constant2: //statements
  default: //statements
}

SWITCH case INPUT is compare against each case constant for matching or equality. If the condition is satisfied, the corresponding CASE Statements are executed.

Example:

int a=5;
switch(a)
{
  case 3: System.out.println("THREE"); break;
  case 5: System.out.println("FIVE"); break;
  default: System.out.println("UNKNOWN");
}
//OUTPUT
//FIVE

Switch Case Fall Through (Range Defining)

Program control moves to the next CASE if there is no break statement after the SWITCH CASE statements. The statements listed in the following CASE are also carried out. All other statements are carried out down the ladder without checking the case constants. There is no purpose to this behaviour. If a break statement is present, program control leaves the SWITCH case after the statements are carried out.

In the below example, you can see the range 1-4 and 5-8 defined by the SWITCH statement. Observe that break statement is present only at the end of the Range we need.

Example:

class SwitchCaseRange
{
  public static void main(String args[])
  {
    int a=2;
    switch(a)
    {
      case 1: ;
      case 2: ;
      case 3: ;
      case 4: System.out.print("Range: 1-4"); break;
      case 5: ;
      case 6: ;
      case 7: ;
      case 8: System.out.print("Range: 5-8"); break;
      default: System.out.println("UNKNOWN");
    }
  }
}
//OUTPUT
//Range: 1-4

Switch Case INPUT Types

A Switch case does not work on all data type values. boolean, float, double and long data type constants are not allowed. Starting with JDK 7 or JAVASE 7, String type constants are supported.

The switch case works with the following Java literals or constants.

  • byte
  • short
  • char
  • int
  • enum
  • String (As of JDK 7)

Example: Switch with Strings

class SwitchStrings
{
 public static void main(String args[])
 {
    String name = "JAVA";
    switch(name)
    {
      case "java": System.out.println("java");break;
      case "JAVA": System.out.println("JAVA");break;    
      default: System.out.println("Unknown");
    }
 }
}
//OUTPUT
//JAVA

Example: Switch with enums

class SwitchEnums
{
  static enum CAR {JEEP, KIA, JAGUAR}

  public static void main(String[] args)
  {
    CAR car1 = CAR.JEEP;
    switch(car1)
    {
      case KIA: System.out.println("KIA");break;
      case JEEP: System.out.println("JEEP");break;
      case JAGUAR: System.out.println("JAGUAR");break;
      default: System.out.println("UNKNOWN");
    }
  }
}
//OUTPUT
//JEEP

Switch Case Errors in Java

Most possible switch case error is about the Case Constant used. One such error is “case expressions must be constant expressions“. Simply add the “final” keyword before the variables used as Case Constants.

Example:

class SwitchErrors
{
 public static void main(String[] args) {
  final String name = "JAVA";
  switch(name)
  {
    case name: System.out.println("java");break;
  }
 }
}
//OUTPUT
//java

Switch Case Constant Rules in Java

  1. Variables are not allowed as constants
  2. “final” type variables are allow
  3. Duplicate case constants are not allowed
  4. “break” statement is optional
  5. “default” statement is optional
  6. Only JDK 7 and above supports String constants
  7. Conditional statements are not allowed.
  8. Case constants are checked only for equality condition against Switch INPUT
  9. More than and Less than conditions cannot be checked.
  10. Nesting of Switch cases is allowed to any depth.
  11. Allowed case constant types are byte, short, char, int, enum and String

Switch Case Vs IF-ELSE-IF Ladder Performance

In comparison to an IF-ELSE-IF ladder, a switch case performs well. A JUMP Table is produced by the compiler following the compilation of a Java programm. This Jump table aids in regulating the execution path at Runtime without further verification. All checks are performed at runtime when using IF-ELSE. IF-ELSE is therefore a bit slow.

Nesting of Switch in java

You can happily nest one switch statement inside another switch statement. Nested Switch is usually kept at Case Statements section followed by a break statement to avoid fall through. We have used multiple cases inside the outer switch and inner switch.

Example:

class NestedSwitch
{
 public static void main(String[] args) {
  char type = "B";
  int model = 4;
  switch(type)
  {
    case 'B': System.out.println("B");
              switch(model)
              {
                case 4: System.out.println("FOUR");
              }
              break;
    case 'C': System.out.println("C"); break;
    default: System.out.println("UNKNOWN");
  }
 }
}
//OUTPUT
//B
//FOUR

This is how a Switch works in Java.

[WpProQuiz 153]

Switch Case in Java Interview MCQ

1) A SWITCH case statement in Java is a ___ control statement.

A) Iteration

B) Loop

C) Selection

D) Jump

Answer [=] C

2) Which is the alternative to SWITCH in Java language?

A) break, continue

B) for, while

C) if, else

D) goto, exit

Answer [=] C

Explanation:

We can implement a SWITCH statement using IF, ELSE IF and ELSE control statements.

3) What are the keywords used to implement a SWITCH case in Java language?

A) switch, case

B) default

C) break

D) All

Answer [=] D

4) What are the parts of a SWITCH in java?

A) switch input condition

B) case constants

C) case statements

D) All

Answer [=] D

5) A SWITCH statement accepts ___ type of data as input.

A) byte

B) short

C) int

D) All

Answer [=] D

6) A switch statement in Java accepts ___ as input data.

A) enum

B) String

C) enum and String

D) long

Answer [=] C

Explanation:

SWITCH does not support long constants.

7) Choose the correct syntax of SWITCH statement in Java below.

A)

switch(input)
{
  case constant1: //statements; break;
  case constant2: //statements; break;
  default: //statements;
};

B)

switch(input)
{
  case constant1: //statements; break;
  case constant2: //statements; break;
  default: //statements;
}

C)

switch(input)
{
  case constant1: //statements; break;
  case constant2: //statements; break;
  default case: //statements;
};

D)

switch(input)
{
  case constant1: //statements; break;
  case constant2: //statements; break;
  default case: //statements;
}

Answer [=] B

Explanation:

The semicolon (;) after SWITCH {} is not required. So option A is wrong though it compiles fine.

8) Which version of Java did start supporting String as the input data type of a SWITCH?

A) JDK 5

B) JDK 6

C) JDK 7

D) JDK 8

Answer [=] C

9) What is the output of Java program with SWITCH below?

int a=10;
switch(a)
{
case 10: System.out.println("TEN");
}

A) No output

B) TEN

C) Compiler error as there is no BREAK.

D) None

Answer [=] B

Explanation:

“break;” is optional.

10) What is the output of the Java program below?

int b=20;
switch(b)
{
default: System.out.println("LION");
}

A) No output

B) LION

C) Compiler error as there are no CASE statements.

D) None

Answer [=] B

Explanation:

One can write the DEFAULT statement without making any mistakes.

11) What is the output of the Java program below?

String animal = "GOAT";
switch(animal)
{
  break: System.out.println("DOMESTIC");
}

A) No output

B) GOAT

C) DOMESTIC

D) Compiler error

Answer [=] D

Explanation:

Case statements should start with either “case” or “default” only.

12) What is the output of the Java program below?

String college = "OXFORD";
switch("STANFORD")
{
case college: System.out.println("EXAM TIME"); break;
default: System.out.println("UNKNOWN");
}

A) EXAM TIME

B) UNKNOWN

C) STANFORD

D) Compiler error

Answer [=] D

Explanation:

case expressions must be constant expressions.
So, make the variable final.
final String college = "OXFORD";

13) What is the output of Java program with SWITCH?

int num=20;
switch(num)
{
case 10: System.out.println("TEN"); break;
case 20: System.out.println("TWENTY"); break;
case 30: System.out.println("THIRTY");
}

A) TEN

B) TWENTY

C) THIRTY

D) TEN TWENTY

Answer [=] B

14) What is the output of Java program below?

int num=40;
switch(num)
{
case 5: System.out.println("FIVE"); break;
case 35+5: System.out.println("FORTY"); break;
case 20+30: System.out.println("FIFTY");
}

A) FIVE

B) FORTY

C) FIFTY

D) Compiler error

Answer [=] B

Explanation:

Expressions that produce constant values may be used.

15) What is the output of the below Java program?

int persons = 45;
int random = 45;
switch(random)
{
  case persons: System.out.print("CRICKET ");
  default: System.out.println("RUGBY");
}

A) CRICKET

B) CRICKET RUGBY

C) RUGBY

D) Compiler error

Answer [=] D

Explanation:

Error: case expressions must be constant expressions
So, make the variable final.
final int persons = 45;
//Then, output will be
CRICKET

16) What is the output of the below Java program?

switch(15)
{
  case 5*2: System.out.println("TEN");break;
  case 5*4-5:System.out.println("FIFTEEN");break;
  case 60/4+5: System.out.println("TWENTY");
}

A) TEN

B) FIFTEEN

C) TWENTY

D) Compiler error

Answer [=] B

Explanation:

A “case constant” can be any expression that yields a Constant.

17) A SWITCH fall through occurs in Java only in the absence of ___.

A) case keyword

B) break keyword

C) default keyword

D) None

Answer [=] B

18) What is the purpose of designing a SWITCH logic with a fall-through in Java?

A) To define ranges.

B) To define additions

C) To improve switch block performance

D) None

Answer [=] A

19) Does the following Java code-snippet compile?

switch(45)
{
  case 10: ;
}

A) NO

B) YES

Answer [=] B

Explanation:

You can specify dummy statements in java using a Semicolon (;).

20) What is the output of the below Java program with a SWITCH statement?

int points=6;
switch(points)
{
  case 6: ;
  case 7: System.out.println("PASS");break;
  case 8: ;
  case 9: System.out.println("Excellent");break;
  case 10: System.out.println("Outstanding"); break;
  default: System.out.println("FAIL");
}

A) PASS

B) Excellent

C) Outstanding

D) FAIL

Answer [=] A

Explanation:

This is the way we define ranges in a SWITCH construct.

21) Choose TRUE or FALSE. A SWITCH can be used to compare values for high or low.

A) FALSE

B) TRUE

Answer [=] A

Explanation:

A Java SWITCH statement cannot be used to check More or Less conditions.

22) State TRUE or FALSE. It is allowed to use duplicate case constants inside a Java SWITCH statement.

A) FALSE

B) TRUE

Answer [=] A

Explanation:

SWITCH case constants must be unique.

23) State TRUE or FALSE. SWITCH works faster than the IF-ELSE ladder in Java.

A) FALSE

B) TRUE

Answer [=] B

Explanation:

The compiler (JIT-Just In Time) creates a JUMP-TABLE for switch case branchings. So, it does not take time during Runtime. So, the SWITCH statement is fast.

24) Choose the correct statement about Java SWITCH statements.

A) A SWITCH can contain another SWITCH statement.

B) Switch case statements are allowed inside IF-ELSE ladders.

C) Inside of loops like for, while, and do while, switch statements are permitted.

D) All

Answer [=] D

25) What is the output of the below Java program?

int hours = 10;
switch(hours)
{
  case 10: System.out.println("TEN");break;
  case 10: System.out.println("TEN AGAIN"); break;
  default: System.out.println("TEN AS USUAL");
}

A) TEN

B) TEN AGAIN

C) TEN AS USUAL

D) Compiler error

Answer [=] D

Explanation:

Case constant 10 is duplicate. So, it causes compiler error.

26) What is the output of the below Java program?

char grade = 'B';
switch(grade)
{
  case 'A': System.out.print("GRADE-A ");break;
  case 'B': System.out.print("GRADE-B ");
  case 'C': System.out.print("GRADE-C ");
}

A) GRADE-A

B) GRADE-B

C) GRADE-B GRADE-C

D) Compiler error

Answer [=] C

Explanation:

A Java SWITCH statement may contain data of the “char” data type. CASE-C is started when a break statement is absent.

27) What is the output of the below Java program with SWICH and ENUM?

static enum ANIMAL {GOAT, TIGER, CAMEL}
public static void main(String args[])
{
  ANIMAL ani = ANIMAL.CAMEL;
  switch(ani)
  {
    case GOAT: System.out.println("GOAT");break;
    case CAMEL: System.out.println("CAMEL");break;
    case TIGER: System.out.println("TIGER");break;
  }

}

A) CAMEL

B) GOAT

C) TIGER

D) Compiler error

Answer [=] A

Explanation:

A SWITCH in java works well with enum constants. CASE Constants are defined without enum type.

28) What is the output of the below Java program with SWITCH and Strings?

String phone = "APPLE";
switch(phone)
{
case "Apple": System.out.println("Apple");break;
case "APPLE": System.out.println("APPLE");break;
case "SAMSUNG": System.out.println("SAMSUNG");
}

A) Apple

B) APPLE

C) SAMSUNG

D) Compiler error

Answer [=] B

Explanation:

Apple and APPLE are different strings.

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.