Interview Questions and Answers - Order By Rating Q181. What will the following code print upon executing main method of Main class
public class BaseClass {
BaseClass(){
this(2);
System.out.println("Hello I am in Base Class Constructor");
}
BaseClass(int i){
System.out.println("Hello I am in Base Class int argument Constructor");
}
}
public class DerivedClass extends BaseClass{
DerivedClass(){
super(1);
System.out.println("Hello I am in Derived Class Constructor");
}
}
public class Main {
public static void main(String[] args) {
DerivedClass derivedClass = new DerivedClass();
}
} Core Java
Ans. Hello I am in Base Class int argument Constructor
Hello I am in Base Class Constructor
Hello I am in Derived Class Constructor Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor Q182. Why line 4 in this code gives error saying "Cannot be referenced from a static context" ?
public class OuterClass {
public static void main(String[] args){
InnerClass innerClass = new InnerClass();
}
class InnerClass {
}
} Core Java
Ans. InnerClass is a non static inner class and hence can only be instantiated using instance of the outer class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner class Q183. What will the following code print upon executing main method of Main class ?
public class BaseClass {
BaseClass(int x){
System.out.println("Hello I am in Base Class Constructor");
}
}
public class DerivedClass extends BaseClass{
DerivedClass(){
super(1);
System.out.println("Hello I am in Derived Class Constructor");
}
}
public class Main {
public static void main(String[] args) {
DerivedClass derivedClass = new DerivedClass();
}
} Core Java
Ans. Hello I am in Base Class Constructor
Hello I am in Derived Class Constructor Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor Q184. What will the following code print upon executing main method of Main class ?
public class BaseClass {
BaseClass(int x){
System.out.println("Hello I am in Base Class Constructor");
}
}
public class DerivedClass extends BaseClass{
DerivedClass(){
System.out.println("Hello I am in Derived Class Constructor");
}
}
public class Main {
public static void main(String[] args) {
DerivedClass derivedClass = new DerivedClass();
}
} Core Java
Ans. Compilation error as there is no default constructor available in BaseClass. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor Q185. What will the following code print upon executing main method of Main class ?
public class BaseClass {
BaseClass(){
System.out.println("Hello I am in Base Class Constructor");
}
}
public class DerivedClass extends BaseClass{
DerivedClass(){
System.out.println("Hello I am in Derived Class Constructor");
super();
}
}
public class Main {
public static void main(String[] args) {
DerivedClass derivedClass = new DerivedClass();
}
}
Core Java
Ans. There will be compilation error within constructor of Derived Class as "super must be the first statement in constructor body". Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor Q186. What will the following print upon executing main method of Main class ?
public class BaseClass {
BaseClass(){
System.out.println("Hello I am in Base Class Constructor");
}
}
public class DerivedClass extends BaseClass{
DerivedClass(){
System.out.println("Hello I am in Derived Class Constructor");
}
}
public class Main {
public static void main(String[] args) {
DerivedClass derivedClass = new DerivedClass();
}
} Core Java
Ans. Hello I am in Base Class Constructor
Hello I am in Derived Class Constructor Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor Q187. Will it print the message in Base Class constructor if we execute main method of Main class ? why ?
public class BaseClass {
BaseClass(){
System.out.println("Hello I am in Base Class Constructor");
}
}
public class DerivedClass extends BaseClass{
}
public class Main {
public static void main(String[] args) {
DerivedClass derivedClass = new DerivedClass();
}
} Core Java
Ans. Yes.
When the Derived class constructor is initialized , a no argument super will be called intrinsically. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor Q188. Isnt the use of HashTable and ConcurrentHashMap the same, i.e providing synchronized map collection ? Core Java
Ans. Both provide the thread safety but the actual difference come when talk about performance. CHM gives best performance when no of writer threads are less in number. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  synchronized collections  hashtable  ConcurrentHashMap Asked in 1 Companies Q189. Can we use try within catch block ? Core Java
Ans. Yes Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q190. Is java fully platform independent ? Core Java
Ans. yes, Java is a class-based and object-oriented programming language. It is a platform-independent language i.e. the compiled code can be run on any java supporting platform. It runs on the logic of “Write once, run anywhere”. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q191. Can you write immutable class without final? Core Java
Ans. By making the constructor private Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q192. Does Java SE has Immutable collections ? Core Java
Ans. Yes wef from Java 9 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java 9  java9  immutable  immutability collections Q193. Where to use interface and abstract class ? Core Java
This question was recently asked at 'Exterro,Equator Business Solutions'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 2 Companies Q194. What are basic functions of OOPS ? Core Java
This question was recently asked at 'Zillious'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q195. Write a java program to find the maximum profit in day trading Core Java
This question was recently asked at 'Capital One'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q196. What is the biggest Design problem in Java ? Core Java
This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Ans. Assigning a value of one type to a variable of another type is known as Type Casting.
Example :
int x = 10;
byte y = (byte)x;
In Java, type casting is classified into two types, Widening Casting(Implicit) widening-type-conversion and Narrowing Casting (Explicitly done) narrowing-type-conversion.
Widening or Automatic type converion - Automatic Type casting take place when,the two types are compatible and the target type is larger than the source type
Example :
public class Test {
public static void main(String[] args) {
int i = 100;
long l = i; //no explicit type casting required
float f = l;//no explicit type casting required
System.out.println("Int value " i);
System.out.println("Long value " l);
System.out.println("Float value " f);
}
}
Narrowing or Explicit type conversion - When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting.
Example :
public class Test{
public static void main(String[] args) {
double d = 100.04;
long l = (long)d; //explicit type casting required
int i = (int)l;//explicit type casting required
System.out.println("Double value " d);
System.out.println("Long value " l);
System.out.println("Int value " i);
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  typecasting  type casting Asked in 23 Companies Q198. Why doesn't this code give compile time error when we are clearly trying to add an integer element to an array of Strings
Object[] strArray = new String[2];
strArray[0] = 5; Core Java
Ans. Because the reference is of Object Array and the resolution of what it holds is identified at the runtime.
String[] strArray = new String[2];
strArray[0] = 5;
would have given the compile time array as the reference is of String array. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays Q199. Will this give compile time error ?
Object[] strArray = new String[2];
strArray[0] = 5; Core Java
Ans. No. But It will throw ArrayStoreException at runtime. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  ArrayStoreException  arrays Q200. What is the problem with this code
String[] strArray = new String[2];
strArray[0] = 5; Core Java
Ans. We are trying to add an integer element to an array of String. Will give compile time error. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays Q201. If arrays cannot be resized , Why is this code valid
String[] strArray = new String[2];
strArray = new String[5]; Core Java
Ans. We are not resizing the first array here but assigning the reference strArray to a new Array with size 5.
So after line 2, We have 2 arrays in memory, one with size 2 and other with size 5 with strArray referring to second array with size 5. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  array initialization Q202. Is this code valid
String[] strArray = new String[2];
strArray.length = 5; Core Java
Ans. It will give compile time error saying "The final field array.length cannot be assigned"
Arrays once initialized cannot be resized. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  array initialization Q203. Is this array initialization correct ? If Yes, What will be the size of array ?
String[] strArray = new String[3]{"Buggy","Bread"}; Core Java
Ans. No. It will result in error saying "Cannot define dimension expressions when an array initializer is provided" Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  array initialization Q204. Is this array initialization correct ? If Yes, What will be the size of array ?
String[] strArray = new String[]{"Buggy","Bread"}; Core Java
Ans. Yes, size of the array will be 2. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  array initialization Q205. Is this array declaration correct ? If not , Why ?
String[] strArray = new String[]; Core Java
Ans. No, We haven't specified the size of array to be initialized. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  arrays  array initialization Q206. Which memory segment holds the byte code ? Core Java
Ans. Code Segment Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Memory Management  Code Memory Segment  Code Segment Memory  Code Segment Q207. What is meant by "unreachable code" ? Can you write a code segment explaining this ? Core Java
Ans. Unreachable code is a code segment that can never be executed in any case. For example -
int x = 5;
return;
System.out.println(x);
In this code , 2nd line will return the control out of the method and hence 3rd line will never get executed and hence is unreachable code. This will be caught at compile time only. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  unreachable code Q208. Can we call a static method using reference currently pointing to null. Core Java
Ans. yes, we can.
exa: public class Java{
public static void main(String... args) {
JAva j= null;
j.greeting(); // call with null reference
}
public static void greeting() {
System.out.println("Hello World");
}
} // output: Hello World Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  static methods   static Asked in 1 Companies Q209. How to run jar file using command prompt Core Java
This question was recently asked at 'CIGNEX Datamatics'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  jar  running jar using command line  cron entry   setting java app as cron Asked in 1 Companies Q210. Diff between Runtime.getRunTime().gc() and System.gc() Core Java
This question was recently asked at 'CIGNEX Datamatics'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies