LIVE Course for free

Rated by 1 million+ students
Get app now
Class 8 Foundation Course
Class 9 Foundation Course
Class 10 Foundation Course
Class 11 Foundation Course
Class 12 Foundation Course
JEE COURSE
NEET COURSE
0 votes
29 views
in Information Technology by (54.3k points)
closed by

What is the output of the given program ?

main ()

{

int ch = 1;

switch (ch)

{

case 1:

printf ("\nGoodbye1");

case 2:

printf ("\nGoodbye2");

}

}


1. Goodbye2
2. Goodbye1
3. Goodbye1Goodbye1
4. Goodbye1Goodybye2

1 Answer

0 votes
by (30.0k points)
selected by
 
Best answer
Correct Answer - Option 4 : Goodbye1Goodybye2

Concept:

A switch statement tests the value of a variable and compares it with multiple cases.

Once the case match is found, a block of statements associated with that particular case is executed.

A switch-case statement can accept int, char, long datatypes.

Syntax:

switch(expression)

{

                case value-1:

                                                Block-1;

                                                Break;

                case value-2:

                                                Block-2;

                                                Break;

                case value-n:

                                                Block-n;

                                                Break;

                default:

                                                Block-1;

                                                Break;

}

Calculation:

main ()

{

int ch = 1;

switch (ch)

{

case 1:

printf ("\nGoodbye1");

case 2:

printf ("\nGoodbye2");

}

}

1. In the given program ch = 1;

The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

2. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

As there is no break statement after case 1, case 2 will also be executed.

3. Hence in the output, we will get:

Goodbye1

Goodybye2

Hence option (4) is the correct answer.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...