100%.
A switch statement, most often has the form:
switch (expression) {
case constant-1:
statements-1
break;
...
}
The value of the expression can be:
i. int
ii. short
iii. byte
iv. Primitive char
v. Enum
vi. String
vii. Real number - c. All, except vi and vii
The following code writes out the name of a day of the week depending on the value of day.
True or False?
String dayName = null;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
,break;
}
System.out.println(dayName); - True
Given the following piece of code:
class CostCalculationException extends Exception{}
class Item {
public void calculateCost() throws CostCalculationException {
//...
throw new CostCalculationException();
//...
}
}
class Company {
public void payCost(){
new Item().calculateCost();
}
}
Which of the following statements is correct? - c. This code will compile if you add a try-catch
block in payCost() Correct
d. This code will compile if you add throws CostCalculationException in the signature of method
payCost().
Given the following piece of code:
class Student { public void talk(){} }
public class Test{
public static void main(String args[]){
Student t = null;
try {
t.talk();
} catch(NullPointerException e){
System.out.print("There is a NullPointerException. ");
} catch(Exception e){
System.out.print("There is an Exception. ");
}
System.out.print("Everything ran fine. ");
}
}
what will be the result? - a. If you run this program, the following is printed: There is a
NullPointerException. Everything ran fine.
, Consider the following code (assume that comments are replaced with real code that works as
specified):
public class TestExceptions {
static void e() {
// Might cause any of the following unchecked exceptions to be
// thrown:
// Ex1, Ex2, Ex3, Ex4
}
static void April() {
try {
e();
} catch (Ex1 ex) {
System.out.println("April caught Ex1");
}
}
static void March() {
try {
April();
} catch (Ex2 ex) {
System.out.println("March caught Ex2");
// now cause exception Ex1 to be thrown
}
}
static void February() {
try {
March();
} catch (Ex1 ex) {
System.out.println("February caught Ex1");
} catch (Ex3 ex) {
System.out.println("February caught Ex3");
}
}
static void a() {
try {
February();
} catch (Ex4 ex) {
System.out.println("January caught Ex4");
// now cause exception Ex1 to be thrown
} catch (Ex1 ex) {
System.out.println("January caught Ex1");
}