0% found this document useful (0 votes)
27 views

5 - Selections - Examples

Uploaded by

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

5 - Selections - Examples

Uploaded by

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

A) Java Exercises:

Exercise (1):

Fill in the missing parts to print the values true and false:

isJavaFun = true;
isFishTasty = false;
System.out.println(isJavaFun);
System.out.println(isFishTasty);

Exercise (2):

Fill in the missing parts to print the value false:

int x = 10;
int y = 9;
System.out.println( );

Exercise (3):

Print "Hello World" if x is greater than y.

int x = 50;
int y = 10;
(x y) {
System.out.println("Hello World");
}

Exercise (4):
Print "Hello World" if x is equal to y.

int x = 50;
int y = 50;
(x y) {
System.out.println("Hello World");
}

Exercise (5):

Print "Yes" if x is equal to y, otherwise print "No".

int x = 50;
int y = 50;
(x y) {
System.out.println("Yes");
} {
System.out.println("No");
}

Exercise (6):

Print "1" if x is equal to y, print "2" if x is greater than y, otherwise print "3".

int x = 50;
int y = 50;
(x y) {
System.out.println("1");
} (x > y) {
System.out.println("2");
} {
System.out.println("3");
}

Exercise (7):

Insert the missing parts to complete the following "short hand if...else statement"
(ternary operator):

int time = 20;


String result = time < 18 "Good day."
"Good evening.";
System.out.println(result);

Exercise (8):

Insert the missing parts to complete the following switch statement.

int day = 2;
switch ( ) {
1:
System.out.println("Saturday");
break;
2:
System.out.println("Sunday");
;
}

Exercise (9):

Complete the switch statement, and add the correct keyword at the end to specify some
code to run if there is no case match in the switch statement.

int day = 4;
switch ( ) {
1:
System.out.println("Saturday");
break;
2:
System.out.println("Sunday");
;
:
System.out.println("Weekend");
}

*************************************************************

B) What do these expressions evaluate to? (Assume x has the value 15


and y has the value 10)
1. 3 == 3
2. 3 != 3
3. x >= y
4. ! (x < y)
5. x % 2 == 0 ? "even" : "odd"
6. x > y ? x : y
7. x > y ? x-y : y-x //replace second ? with :

C) What is the output of the following fragments:


1)
public static void main(String[] args) {
int number1 = 15;
int number2 = 7;
if (number1 >= number2)
System.out.println(2*number1);
else
System.out.println(2*number2);
}
………………………………………………………………………………………………
2)
public static void main(String[] args) {
int x = 9;
if (x == 10) {
System.out.println("x is 10");
} else if (x == 9) {
System.out.println("x is 9");
} else if (x == 8) {
System.out.println("x is 8");
}
}
………………………………………………………………………………………………
3)
public static void main(String[] args) {
int temperature= 75;
if (temperature > 70) {
System.out.println("Wear shorts.");
} else {
System.out.println("Wear long pants.");
}
System.out.println("Get some exercise outside.");
}
………………………………………………………………………………………………
………………………………………………………………………………………………
3)
public static void main(String[] args) {
int b = 5;
int a = (b < 10) ? 1 : 2;
System.out.println(a);
}
………………………………………………………………………………………………
4)
public static void main(String[] args) {
boolean c = true;
if (c == true) {
System.out.println("The variable is set to true.");
} else {
System.out.println("The variable is set to false.");
}
}
………………………………………………………………………………………………

5)
public static void main(String[] args) {
int weight = 700;
System.out.println(weight >= 500);
char gender = 'm';
System.out.println(gender <= 'f');
double colorWaveLength = 1.630;
System.out.println(colorWaveLength > 1.621);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
6)
public static void main(String[] args) {
int a = 5;
int b = 7;
boolean condition = (b > a) && (a + b < a * b);
System.out.println(condition);
System.out.println('B' == 'A' + 1);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
*************************************************************
D) Find the errors and correct them:
1)
public class Summation {
public static void main(String[] args) {
int Sum;
Sum = 25;
double res = Sum + 10.5;
System.out.println(" The result = " + Sum);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
2)
public class Summation {
public static void main(String[] args) {
int num = 34;
if (num >=20){
int num = 17;
System.out.println("The num = " + num);
} else
System.out.println("The num = " + num);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
3)
public class Summation {
public static void main(String[] args) {
x=60;
x= x +20;
System.out.println("X:" + x);
}
………………………………………………………………………………………………
………………………………………………………………………………………………
4)
public class Summation {
public static void main(String[] args) {
int x=70;
if(x>60)
System.out.println("Pass");
else
System.out.println("Fail");
else
System.out.println("Equal");
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
5)
public class Summation {
public static void main(String[] args) {
int x=1;
double y=3.5;
if(x>2)
x=y;
else
x++;
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………

D) Write a Java program:


1. Write a program in JAVA to read any Month Number in integer and
display the number of days for this month.
2. Write a program to act as calculator. Number operator number.
3. Write a program in Java to read any day number in integer and display
day name in the word?

4. Write a complete Java program that asks for a number. If the number
is less than 5, it is written out, but if it is greater than or equal to 5,
twice that number is written out.

5. Write a Java program to implements the followings:


- Reads string value (name), which represents the item name to be
bought
- Reads a float value (qty), which represents the quantity of the item
to be bought
- Reads an integer value (c), which represents the category of the
item be bought
- Tests if the item category is 1, 2 or 3 and determines the price
according to the following table:

Category Price
1 100
2 150
3 200

- The program should compute and print the total price of the item
bought.
- The program should ask the user whether he want to enter another
item. If the user enters ‘y’ the program should repeat again the
above steps. If he enters ‘n’ the program ends.
Hint: The total price is computed as: (qty * price)

You might also like