1.] Output of below code .
public class Main {
public static void main(String []arg){
System.out.println(print(1));
}
static Exception print(int i){
if (i>0){
return new Exception();
}
else{
throw new RuntimeException();
}
}
}
- "java.lang.Exception"
- It will show a stack trace with a runtime exception.
- It will not compile.
- It will run and throw an exception.
2.] Which code would you use to tell of "schwiffy" is of type String?
- "schwiffy".getType().equals("String")
- "schwiffy".getClass().getSimpleName() == "String"
- "schwiffy" instanceof String
- "schwiffy".getType() == String
3.] Given the string "strawberries" saved in a variable cal fruit, what would fruit.substring(2,5) return?
- awb
- raw
- rawb
- traw
4.] What will this program print out to the console when executed?
public class Main {
public static void main(String []arg){
LinkedList<Integer> list = new LinkedList();
list.add(5);
list.add(1);
list.add(10);
System.out.println(list);
}
}
- [10, 5, 1]
- [10, 1, 5]
- [5, 1, 10]
- [1, 5, 10]
5.] How do yu write a foreach loop that will iterate over ArrayList pencilCase?
- for (int i=0; i<pencilCase.size(); i++){}
- for (pencilCase.next()) {}
- Iterator iterator = pencilCase.iterator() for (iterator.next())
- for (Pencil pencil: pencilCase) {}
6.] What is the output of this code?
public class Main {
public static void main(String []arg){
String message = "Hello world!";
String newMessage = message.substring(6, 12)+message.substring(12, 6);
System.out.println(newMessage);
}
}
- The code does not compile.
- "world!!world"
- "world!world!"
- A runtime exception is thrown
7.] Give this code, which command will output "2"?
public class Main {
public static void main(String []arg){
System.out.println(arg[2]);
}
}
- java Main 1 "2" "3 4" 5
- java Main 1 "2" "2" 5
- java Mian.class 1 "2" 2 5
- java main 1 2 "3 4" 5
8.] What does this code print?
System.out.println("apple".compareTo("banana"));
- 0
- a negative number
- a positive number
- false
9.] What can you use to create new instances in Java?
- another instance
- private method
- constructor
- field
10.] What is the output of this code?
public class Main {
public static void main(String []arg){
int a =123451234512345;
System.out.println(a);
}
}
- '123451234512345'
- '12345100000'
- Nothing--this will not compile.
- a negative integer number
11.] The runtime system start your program by calling which function first?
- iterative
- hello
- main
12.] How can you achieve runtime polymorphism in Java?
- method overriding
- method overloading
- method calling
- method overrunning
13.] Which for loop is invalid?
- for(int x=1; x<-10; x++){}
- for(i=3; i<=3; i++){}
- for(;;){}
- for(int i; i==2; i++){}
14.] What letters will print when this code is run ?
public class Main {
public static void main(String []arg){
try{
System.out.println("A");
bedMethod();
System.out.println("B");
}
catch(Exception ex){
System.out.println("C");
}finally{
System.out.println("D");
}
}
public static void bedMethod() {
throw new Error(); //To change body of generated methods, choose Tools | Templates.
}
}
- A,C and D
- C and D
- A and D
- A, B, and D
15.] Which is the most up-to-date way to instantiate the current date?
- LocalDate.now()
- Calender.getInstance().getTime()
- new SimpleDateFormat("yyyy-MM-dd").format(new Date())
- new Date(System.currentTimeMillis())