For ICSE and ISC Students
Explain the function of the following with its syntax and an example:
a. If – else
In decision making the most common statement used is if..else.
if (test condition)
{
This block is known as if block or true block.
If the condition is true,statement/statements
of this block will be executed.
}
else
{
This block is known as else block or false block
If the condition is false, this block will be executed.
'else' is optional. The control will execute either if block
or else block.
}
example:
void display() throws IOException
{
int num;
System.out.println ("Enter the number:");
num=Integer.parseInt(br.readLine());
if (num >100)
{
System.out.println ("The input value is greater than 100.");
/* This is if block. If the condition is false the control will skip this block.*/
}
else
{
System.out.println ("The value is less than or equal to 100.");
}
The switch statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statement/statements associated with that constant are executed and the control leaves the switch body.
The general form of switch statement is:
switch (variable to be compared with a number of constants)
{
body of switch opens.
case int or char constants:
{
case body opens.’ case' is a keyword. case then space then
the constant value or variable, again space and then:
within the body the statements are kept which are to
be executed when switch variable finds match with
the case constant.
last statement of the case body must be break;
This statement will send the control out of the
switch body.
}
The last case is default: .default is a keyword and
this is optional. If no match is found then the default
body is executed.
void display() throws IOException
{
System.out.println ("Enter the day of the week:-\n");
day=Integer.parseInt(br.readLine());
switch (day)
{
case 1:
System.out.println ("The day is sunday.");
break;
case 2:
System.out.println ("The day is monday.");
break;
case 3:
System.out.println ("The day is tuesday.");
break;
case 4:
System.out.println ("The day is wednesday.");
break;
case 5:
System.out.println ("The day is thursday.");
break;
case 6:
System.out.println ("The day is friday.");
break;
case 7:
System.out.println ("The day is saturday.");
break;
default:
System.out.println ("You have entered a wrong day.");
} // end of switch body
}
Differentiate between isUpperCase() and toUpperCase() function.
Both are String class functions.
isUpperCase()
1. Return type of this function is boolean.
If the invoking string object is in upper
case this function will return true otherwise
it returns false.
toUpperCase()
Return type of this function is String object.
This function returns the upper case version of the invoking object whether the string is in lower case
Using Exception handling we can eliminate the risk of abnormal termination of the program.
using try...finally block we can ensure execution of any specific block of statements
What is a “fall through” situation in switch case statement.
In swich case statement, break statement is compulsory at the end of each case body. If this statement is missing, the
control will go on executing the remaining case body statements apart from the case body where the control finds match.
Example:
int a=0;
swich(a)
{
case 0:
a++;
case 1:
a++;
case 2:
a++;
}
Here the match is found in the case 0, since no break statement is used, control will execute all the case bodies.This is called fall through situation in swich statement.
Write a program which will accept a string and print out the text with the uppercase and lowercase letters reversed, but all other characters should remain same as before.
Example : Input : WelComE TO School
Output : wELcOMe to sCHOOL
import java.io.*;
class St
{
String str1,str2=" ";
char ch;
int len;
BufferedReader br;
public St()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public void take() throws IOException
{
System.out.println("Enter the string:");
str1=br.readLine();
len=str1.length();
for(i=0;i< len;i++)
{
ch=str1.charAt(i);
if (ch >=97 && ch< =122)
ch=ch-32;
else if(ch >=65 && ch< =90)
ch=ch+32;
str2=str2+ch;
}
str2=str2.trim();
System.out.println("Modified string=" + str2);
}
}
This page is on icse sample papers. If you have any doubt in any program, pl. feel free to put your comments. I am here to clear your doubts.
No comments:
Post a Comment