0% found this document useful (0 votes)
4 views3 pages

Class 9 Computer Applications: Java Basics

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Class 9 Computer Applications: Java Basics

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class 9 | Computer Application | Chapter 7,8,9

Full Marks-20
Section A (5 Marks)
Q1. Answer all questions. Each question carries 1 mark.
(a) What is the output of the following switch statement?
Java
int code = 2;
switch(code) {
case 1: [Link]("If");
case 2: [Link]("Else");
case 3: [Link]("Switch");
break;
case 4: [Link]("Loop");
}
(i) Else (ii) ElseSwitch (iii) Switch (iv) Error
(b) Which loop is known as an exit-controlled loop? (i) for loop (ii) while loop (iii) do-while loop
(iv) if-else
(c) How many times will the word "Test" be printed?
Java
for(int i = 1; i <= 4; i++) {
for(int j = 1; j <= 3; j++) {
[Link]("Test");
}
}
(i) 4 (ii) 3 (iii) 7 (iv) 12
(d) The continue statement is used to: (i) Terminate the entire program. (ii) Terminate the loop
immediately. (iii) Skip the rest of the current iteration and move to the next. (iv) Call a method.
(e) What is the purpose of [Link](0);? (i) To terminate the current loop. (ii) To terminate the
program. (iii) To skip an iteration. (iv) To return a value from a method.

Section B (15 Marks)


Q2. Answer all questions. Each question carries 2 marks. (7 x 2 = 14 marks)
(a) Give two differences between a switch statement and an if-else-if ladder.
(b) Rewrite the following for loop using a while loop.
Java
for (int x = 5; x <= 20; x += 5) {
[Link](x);
}
(c) What is the output of the following code snippet?
Java
for(int i = 1; i <= 10; i++) {
if (i % 3 == 0) {
continue;
}
if (i == 8) {
break;
}
[Link](i + " ");
}
(d) [PYQ] Write a Java code snippet (only the logic) that inputs an integer n. Check if the number
is a "Buzz Number" and print "Buzz" or "Not Buzz". > A number is a Buzz Number if it either ends
with 7 or is divisible by 7.
(e) [PYQ] Write a Java code snippet using nested for loops to display the following pattern:
1
12
123
1234
(f) Write a switch-case block for a menu-driven program. The user enters a char variable choice.
• If choice is 'S', print "Square".
• If choice is 'C', print "Circle".
• For any other input, print "Invalid Choice".
(g) [PYQ] Write a Java code snippet using nested for loops to display the following rectangular
pattern:
*****
*****
*****
Q3. Answer the following question. (1 x 1 = 1 mark)
(a) What is a "fall through" condition in a switch statement?

Common questions

Powered by AI

The equivalent 'while' loop is: int x = 5; while (x <= 20) { System.out.println(x); x += 5; } The conversion involves initializing 'x' before the loop, checking the condition before each iteration, and updating 'x' within the loop body .

Two main differences are: 1) A switch statement evaluates a single expression and executes code based on case values, usually constants, while an if-else-if ladder can evaluate more complex conditions with expressions. 2) A switch does not support relational expressions, whereas an if-else-if ladder can have complex, compound conditions using logic operators .

The 'continue' statement is used to skip the rest of the current iteration and proceed with the next iteration of the loop. In contrast, the 'break' statement terminates the loop entirely and transfers control outside the loop block .

The 'do-while' loop is considered an exit-controlled loop because it evaluates its condition at the end of each iteration, ensuring the loop body executes at least once before any condition is checked. This is in contrast to 'for' and 'while' loops, which check conditions before executing the loop body, making them entry-controlled loops .

'System.exit(0);' is used to terminate the entire program execution immediately, whereas 'break' merely exits the nearest loop or switch block. Unlike 'break', 'System.exit(0);' will stop all operations and end the Java process .

The word "Test" will be printed 12 times. The outer loop runs 4 times (i = 1 to 4), and for each iteration of the outer loop, the inner loop runs 3 times (j = 1 to 3). Thus, the total number of prints is 4 * 3 = 12 .

The output of the code is "1 2 4 5 7". The loop prints each number within the range 1 to 10, skips numbers that are multiples of 3 because of the 'continue' statement, and stops when 'i' equals 8 due to the 'break' statement, preventing any further numbers from being printed .

Logic for a "Buzz Number": ```Java if (n % 10 == 7 || n % 7 == 0) { System.out.println("Buzz"); } else { System.out.println("Not Buzz"); } ``` The logic checks if the number ends with 7 or is divisible by 7, using the condition 'n % 10 == 7' or 'n % 7 == 0' .

A "fall-through" condition occurs in a switch statement when a case lacks a 'break' statement, causing execution to continue into subsequent case statements until a break is encountered. This can lead to multiple cases executing consecutively, affecting the outcome by producing concatenated results .

The switch statement will output "ElseSwitch" because the 'case 2:' statement falls through to the next case (without a break) until it encounters a break or exits the switch. Since case 2 prints "Else" and falls through to case 3, which prints "Switch", the output is concatenated as "ElseSwitch" .

You might also like