Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Studies, Class 8, ICSE
Explain the use of if control structure.
The if control structure is used to execute a statement or block of statements if the condition is true, otherwise it ignores the condition. The general syntax of if statement is as follows:
if (Boolean expression)
{
//Java statements
..
..
}
What are the unique features of for loop?
for loop is an entry-controlled loop. Its general syntax is as follows:
for (initialization; conditional; increment/decrement)
{
//Java Statements
..
..
}
Explain the switch statement with an example.
switch statement in a program, is used for multi-way branch. It compares its expression to multiple case values for equality and executes the case whose value is equal to the expression of switch. If none of the cases match, default case is executed. If default case is absent and no case values match then none of the statements from switch are executed. The below program makes use of the switch statement to print the value of a number if the number is 0, 1 or 2:
class SwitchExample {
public static void demoSwitch(int number) {
switch (number) {
case 0:
System.out.println("Value of number is zero");
break;
case 1:
System.out.println("Value of number is one");
break;
case 2:
System.out.println("Value of number is two");
break;
default:
System.out.println("Value of number is greater than two");
break;
}
}
}
What is the importance of break and default statements in switch?
Give one difference between while and do while loop.
while | do-while |
---|---|
It is an entry-controlled loop. | It is an exit-controlled loop. |
It is helpful in situations where number of iterations are not known. | It is suitable when we need to display a menu to the user. |
Radhika is writing a program in Java to print her name 10 times. Suggest her the appropriate condition for the same.
Radhika can print her name using a for loop in Java like this:
for (int i = 1; i <= 10; i++) {
System.out.println("Radhika");
}
Naina has written a program in which she has entered one number. She wants to check whether the entered number is divisible by 2 or not. Suggest her the appropriate condition for the same.
Naina can use the below if condition:
if (number % 2 == 0)
System.out.println("Number is divisible by 2");
else
System.out.println("Number is not divisible by 2");
The Selection statements cause the program control to be transferred to a specific location depending upon the outcome of the conditional expression.
The Step value determines the increment or decrement of the loop control variable unless the test condition becomes false.
Java provides three basic looping control structures.
The for loop is also called an Entry controlled loop.
The do while first executes the block of statements and then checks the condition.
The order in which statements are executed in a running program is called the Flow of control.
Write a program in Java using for loop to print all the odd and even number upto 30 terms.
public class KboatEvenOdd
{
public static void main(String args[]) {
System.out.println("Odd numbers: ");
for (int i = 1; i <= 60; i += 2) {
System.out.print(i + " ");
}
System.out.println();
System.out.println("Even numbers: ");
for (int i = 2; i <= 60; i += 2) {
System.out.print(i + " ");
}
}
}
Write a program to print all the factors of 20.
public class KboatFactors
{
public static void main(String args[]) {
int n = 20;
System.out.println("Factors of 20 are:");
for (int i = 1; i <= n; i++) {
if (n % i == 0)
System.out.println(i);
}
}
}
Write a program using while loop to generate the first 10 natural numbers and their sum.
public class KboatNaturalNumbers
{
public static void main(String args[]) {
int n = 1;
int sum = 0;
while (n <= 10) {
System.out.println(n);
sum = sum + n;
n = n + 1;
}
System.out.println("Sum = " + sum);
}
}
Program to check whether the product of two numbers is a buzz number or not.
[A number that ends with 7 or is divisible by 7, is called a buzz number]
public class KboatBuzzCheck
{
public static void buzzNumCheck(int a, int b) {
int p = a * b;
System.out.println("Product = " + p);
if (p % 10 == 7 || p % 7 == 0)
System.out.println("Product is a Buzz Number");
else
System.out.println("Product is not a Buzz Number");
}
}
Generate the following series upto 10 terms:
a) 1, 8, 27, 64, 125 ........
public class KboatSeries
{
public static void main(String args[]) {
int a = 1;
for (int i = 1; i <= 10; i++) {
int t = a * a * a;
System.out.print(t + " ");
a++;
}
}
}
b) 0, 3, 8, 15, 24, 35 .....
public class KboatSeries
{
public static void main(String args[]) {
for (int i = 1; i <= 10; i++) {
int t = i * i - 1;
System.out.print(t + " ");
}
}
}
c) 1, 4, 7,10 ......
public class KboatSeries
{
public static void main(String args[]) {
int t = 1;
for (int i = 1; i <= 10; i++) {
System.out.print(t + " ");
t += 3;
}
}
}
Write a program to print all the prime numbers between 1 and 100.
public class KboatPrime
{
public static void main(String args[]) {
for (int i = 1; i <= 100; i++) {
int c = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0)
c++;
}
if (c == 2)
System.out.println(i);
}
}
}
Write a Java program using the switch case to print the corresponding days of numbers.
For example: 1= Monday.... 7 = Sunday
public class KboatDayName
{
public static void dayName(int n) {
switch (n) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Incorrect day.");
}
}
}
Write a program to print the largest of three numbers.
public class KboatLargestNumber
{
public static void largestNumber(int a, int b, int c) {
System.out.println("The three numbers are "
+ a + ", " + b + ", " + c);
System.out.print("Largest Number: ");
if (a > b && a > c)
System.out.println(a);
else if (b > a && b > c)
System.out.println(b);
else
System.out.println(c);
}
}
Write a program to print the Fibonacci series upto 10 terms.
[A series of numbers in which each number is the sum of the two preceding numbers. For example: 0,1,1,2,3, ............... n].
public class KboatFibonacci
{
public static void main(String args[]) {
int a = 0;
int b = 1;
System.out.print(a + " " + b);
/*
* i is starting from 3 below
* instead of 1 because we have
* already printed 2 terms of
* the series. The for loop will
* print the series from third
* term onwards.
*/
for (int i = 3; i <= 10; i++) {
int term = a + b;
System.out.print(" " + term);
a = b;
b = term;
}
}
}
Create a program to display whether the entered character is in uppercase or lowercase.
public class KboatLetterCheck
{
public static void checkLetter(char ch) {
System.out.println("Entered character: " + ch);
if (ch >= 65 && ch <= 90)
System.out.println("Uppercase");
else if (ch >= 97 && ch <= 122)
System.out.println("Lowercase");
else
System.out.println("Not a letter");
}
}
The .......... is a logical situation where either of the two actions are to be performed depending on certain condition.
Name the expression that is used to initialize a loop variable.
The unusual execution of more than one case at a time is termed as
How many times the following body of loop will execute?
for(int a = 1; a <= 10; a++)
{
System.out.println(a);
}
How many times the following message will be printed?
do
{
System.out.println("Hello");
}
ch++;
while(ch <= 1);
If none of the case matches, the compiler executes the statements written in the .......... case.
Which control structure is used when there is a requirement to check multiple conditions in a program?
The break statement takes the flow of control out of the switch statement.
True
To execute the while loop, the condition must be false in the beginning.
False
Default is the first statement of the switch case.
False
While writing programs, the statements should be indented properly for better readability.
True
The number of iteration refers to the number of times the condition is met.
True