Interview Questions and Answers - Order By Rating Q2611. Can a lock be acquired on a class? How is it different than the object level lock ?
Ans. Yes, a lock can be acquired on the class. Class level lock is applicable on the whole class and hence on all objects of the class whereas the object level lock is applicable on the respective object. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  synchronization  class level lock  object level lock Q2612. Difference between Yielding and Sleeping ?
Ans. When a task invokes its yield method, it returns to the ready state. When a task invokes its sleep method, it returns to the waiting state. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  multithreading  threads  yielding  sleeping  thread states Q2613. What is the use of Runtime Class ?
Ans. This class is used to provide access to the Java runtime system Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  runtime class  jre  java runtimeFrequently asked. Q2614. What should a class do if its implementing an interface ? Core Java
Ans. It should either implement all interface methods or declare unimplemented methods as abstract. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  interfaces Asked in 1 Companies Basic   frequent Try 1 Question(s) Test Q2615. What should a class do if its extending an abstract class ? Core Java
Ans. It should either implement the abstract methods or re-declare them abstract. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  abstract class Asked in 2 Companies Basic   Frequent Q2616. What is a compilation unit in Java ?
Ans. Java Source Code File Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  compilation unit in java Q2617. What is Associativity while evaluating a Java statement ? Core Java
Ans. Associativity determines whether an expression is evaluated left-right or right-left. When an expression has two operators with the same precedence, the expression is evaluated according to its associativity. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  associativity  expression evaluation  operator Q2618. What is a package and what are its advantages ? Core Java
Ans. Package is a namespace that organizes a set of related classes.
Advantages of Packages
1. Better Organization of classes.
2. Saves from the problem of duplicate names as duplicate class names are allowed across different packages. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  package  advantages of packages Asked in 1 Companies Basic a. Constructor without parameters declared by user b. Constructor provided by Java if no constructor is declared c. Constructor with empty body d. All of the aboveAns.b. Constructor provided by Java if no constructor is declared
Q2620. How can we create objects if we make the constructor private ? Core Java
a. We can't create objects if constructor is private b. We can only create objects if we follow singleton pattern c. We can only create one object d. We can create new object through static method or static blockAns.d. We can create new object through static method or static block
Q2621. What will be the output of following ?
String str1 = new String("String1");
String str2 = new String("String1");
System.out.print(str1 == str2);
System.out.print(str1.equals(str2));
str1 = str2;
System.out.print(str1 == str2); Core Java
a. falsetruetrue b. truetruetrue c. truetruefalse d. falsetruefalseAns.a. falsetruetrue
Q2622. What will be the output of following code ?
String str1 = "String1";
String str2 = "String1";
System.out.print(str1 == str2);
System.out.print(str1.equals(str2));
str1 = str2;
System.out.print(str1 == str2); Core Java
a. falsetruetrue b. falsefalsetrue c. truetruetrue d. falsefalsefalseAns.c. truetruetrue
Q2623. Which method needs to be implemented if a class is implementing comparable interface ? Core Java
a. comp b. compare c. compareTo d. compareEqualsAns.c. compareTo
Q2624. Which of the following is false ? Core Java
a. A Class cannot override both hashcode and equals method. b. A class can override both hashcode and equals method. c. A Class must override hashCode method if its overridding equal method. d. A Class can override hashCode even if its not overridding equals method.Ans.a. A Class cannot override both hashcode and equals method.
a. interface b. abstract class c. final class d. util classAns.d. util class
Q2626. Which of the following can throw ClassCastException ? Core Java
a. UpCasting b. DownCasting c. Casting to incompatible data type d. Casting to StringsAns.b. DownCasting
Q2627. x instanceOf y returns false .. Core Java
a. if x is an instance of y class b. if x is an instance of class implementing y interface c. if x is an instance of class extending y class d. if x is an instance of Class which is a parent of Y classAns.d. if x is an instance of Class which is a parent of Y class
a. java.lang.Object b. java.lang.Thread c. java.lang.Runnable d. java.lang.ImplementAns.a. java.lang.Object
Q2629. if classes B and C extends Class A, Which of the following initialization is correct ? Core Java
a. B b = new C(); b. C c = new B(); c. B b = new A(); d. A a = new B();Ans.d. A a = new B();
Q2630. If X implements Y and Y extends Z, Which of the following initialization is correct ? Core Java
a. X x = new Y(); b. X x = new Z(); c. Z z = new Y(); d. Z z = new X();Ans.d. Z z = new X();
Q2631. If class A implements X interface, and Class B implements Y interface, and X is the parent of Y, Which of the following initialization is correct ? Core Java
a. A a = new B(); b. Y y = new A(); c. X x = new B(); d. Y y = new A();Ans.c. X x = new B();
Q2632. What will be the output of executing following class ?
public class BuggyBread {
static {
System.out.println("Static Block");
}
{
System.out.println("Initialization Block");
}
BuggyBread(){
System.out.println("Constructor");
}
public static void main(String[] args){
System.out.println("Main Method");
}
}
Core Java
a. Static Block
Main Method b. Static Block
Instance Initialization Block
Main Method c. Static Block
Constructor
Main Method d. Static Block
Instance Initialization Block
Constructor
Main MethodAns.a. Static Block
Main Method
Q2633. What will be the output upon executing following class ?
public class BuggyBread {
static {
System.out.println("Static Block");
}
{
System.out.println("Instance Initialization Block");
}
BuggyBread(){
System.out.println("Constructor");
}
public static void main(String[] args){
System.out.println("Main Method");
new BuggyBread();
}
} Core Java
a. Instance Initialization Block
Constructor
Static Block
Main Method b. Static Block
Instance Initialization Block
Constructor
Main Method c. Main Method
Static Block
Instance Initialization Block
Constructor d. Static Block
Main Method
Instance Initialization Block
ConstructorAns.d. Static Block
Main Method
Instance Initialization Block
Constructor
Q2634. With the following code, Which is a valid way to initialize ?
public class BuggyBread {
private String element1;
private String element2;
private BuggyBread(String element1, String element2){
this.element1 = element1;
this.element2 = element2;
}
public static class Builder {
private String element1;
private String element2;
Builder(BuggyBread buggybread){
element1 = buggybread.element1;
element2 = buggybread.element2;
}
Builder withElement1(String element1){
this.element1 = element1;
return this;
}
Builder withElement2(String element2){
this.element2 = element2;
return this;
}
BuggyBread build(){
BuggyBread buggybread = new BuggyBread(element1,element2);
return buggybread;
}
}
} Core Java
a. BuggyBread buggybread = new BuggyBread(); b. BuggyBread buggybread = new BuggyBread("element1","element2"); c. BuggyBread.Builder builder = new BuggyBread.Builder(); d. BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");Ans.d. BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");
Q2635. What will be the output of following code ?
public class BuggyBread {
private int x;
private int y;
BuggyBread(int x,int y){};
public static void main(String[] args){
BuggyBread buggybread = new BuggyBread();
System.out.println(buggybread.x);
}
} Reference Core Java
a. 0 b. null c. compilation error due to uninitialized element d. compilation error due to constructorAns.d. compilation error due to constructor
Q2636. What will be the output of following ?
public class BuggyBread {
private int x;
private Integer y;
BuggyBread(int x,int y){};
public static void main(String[] args){
BuggyBread buggybread = new BuggyBread(1,2);
System.out.println(buggybread.x);
System.out.println(buggybread.y);
}
} Reference Core Java
a. 0 0 b. 0 null c. null 0 d. null nullAns.b. 0 null
Q2637. What will be the output of following code ?
public class BuggyBread {
private int x;
private Integer y;
private BuggyBread(int x,int y){};
public static void main(String[] args){
BuggyBread buggybread = new BuggyBread(1,2);
System.out.println(buggybread.x);
}
} Core Java
a. compilation error due to private constructor b. compilation error due to uninitialized elements c. 0 null d. 0 0Ans.c. 0 null
Q2638. What will be the output of following code ?
public class BuggyBread {
private int x;
private BuggyBread(int x){
x = x;
};
public static void main(String[] args){
BuggyBread buggybread = new BuggyBread(5);
System.out.println(buggybread.x);
}
}
Core Java
a. compilation error b. undefined c. 0 d. 5Ans.c. 0
Q2639. What will be the output of exceuting main method ?
public static void main(String[] args){
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);
} Core Java
a. 1,2,3 b. Order cannot be determined c. compilation error d. 3,2,1Ans.a. 1,2,3
Q2640. What will be the output upon executing main method ?
public static void main(String[] args){
Set set = new HashSet();
set.add(1);
set.add(2);
set.add(3);
System.out.println(set);
} Core Java
a. 1,2,3 b. Order cannot be determined c. Compilation error d. 3,2,1Ans.b. Order cannot be determined