• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

Java if else & switch Practice Questions

  • Categories Java, if & switch, if & switch

Below are few questions on Java if else and switch

  1. Convert the following if-else-if construct into switch case:
if(var == 1)
System.out.println("good");
else if(var == 2)
System.out.println("better");
else if(var == 3)
System.out.println("best");
else
System.out.println("invalid");

Ans

switch(var) {
case 1:
System.out.println("good");
break;
case 2:
System.out.println("better");
break;
case 3:
System.out.println("best");
break;
default:
System.out.println("invalid");
}
  1. Find the output in the following code snippet:
int ch=2;
switch(ch) {
case 1: System.out.println("All courses");
Break;
case 2: System.out.println("Simply Coding ");
case 3: System.out.println("Best Study Material");
}

Ans.

Simply Coding
Best Study Material

  1. Rewrite the following program code using suitable ‘if’ command.
switch(m){
case 0:
 x=x+2;
 System.out.println(“X=” x);
 break;
case 1:
 x=x+4;
 System.out.println(“X=” x);
 break;
case 2:
 x=x+6;
 System.out.println(“X=” x);
 break; }

Ans:

if(m==0){
x= x+2;
System.out.println(“X=” +x);
}
else if(m==1){
x= x+4;
System.out.println(“X=” +x);
}
if(m==2){
x= x+6;
System.out.println(“X=” +x);
}
  1. What is wrong in the following program code? Find any 2 errors and explain.
switch (p)
?
case 3:
 int x= 12; int y = 35;
 int z= x* y;
 break;
case 3:
 int a = 20;
 int b = 76;
 break;
 int z=x * y;

Ans.

  • Just after switch statement the question mark (?) symbol should be replaced by open brace (), this shows the beginning of switch block.
  • The constants value for two or more case statements cannot be same so, one should be changed.
  • In case 3 the break statement is present before the last statement z= x* y which is wrong and the error message that appears is “unreachable code”.
  1. Rewrite the following code after corrections.
int a;
if(a=1)
 a==100;
else If(a=2)
 a=1000;

Ans:

int a;
if(a==1)
a=100;
else if(a==2)
a=1000;
  1. Give the output of the following code fragment when

(i) opn = ‘b’
(ii) opn = ‘x’

switch (opn)
{
case ‘a’:
System.out.println(“Simply Coding”);
break;
case ‘b’:
System.out.println(“Online Tutor”);
case ‘c’:
System.out.println(“Online Courses”);
default:
System.out.println(“Invalid Input”);
}

Ans.

i.    Online Tutor

     Online Courses

     Invalid Input

ii.  Invalid Input

  • Share:
author avatar
Simply Coding

Previous post

Java Operators and Operator precedence questions
June 10, 2020

Next post

Java ternary operator examples and problems to solve
June 10, 2020

You may also like

If-else and switch Programs
If-else and switch Programs
22 June, 2021
Java if else
Conditional Statements in Java
16 June, 2021
Crack Java Math Problems
Forming Math Expressions
13 June, 2020

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now