1. What is the output of following program?
- package com.instanceofjava;
- public class B{
- B b= new B();
- public int show(){
- return (true ? null : 0);
- }
- public static void main(String[] args) {
- B b= new B();
- b.show();
- }
- }
Exaplanation:
- Whenever we create the object of any class constructor will be called first and memory allocated for all non static variables
- Here B b= new B(); variable is object and assigned to new object of same class
- B b= new B(); statement leads to recursive execution of constructor will create infinite objects so at run time an exception will be raised
- Exception in thread "main" java.lang.StackOverflowError
- The common cause for a stack overflow exception is a bad recursive call. Typically this is caused when your recursive functions doesn't have the correct termination condition
2. What is the output of following program?
- package com.instanceofjava;
- public class A{
- public static void show(){
- System.out.println("Static method called");
- }
- public static void main(String[] args) {
- A obj=null;
- obj.show();
- }
- }
Exaplanation:
- We can call static methods using reference variable which is pointing to null because static methods are class level so we can either call using class name and reference variable which is pointing to null.
3. What is the output of following program?
- package com.instanceofjava;
- public class A{
- static int a = 1111;
- static
- {
- a = a-- - --a;
- }
- {
- a = a++ + ++a;
- }
- public static void main(String[] args) {
- System.out.println(a);
- }
- }
Explanation:
Top 10 Increment and decrement operators interview Questions
4. What is the output of following program?
- package com.instanceofjava;
- public class A{
- int GetValue()
- {
- return (true ? null : 0);
- }
- public static void main(String[] args) {
- A obj= new A();
- obj.GetValue();
- }
- }
5. What is the output of following program?
- package com.instanceofjava;
- public class A{
- public static void main(String[] args) {
- Integer i1 = 128;
- Integer i2 = 128;
- System.out.println(i1 == i2);
- Integer i3 = 127;
- Integer i4 = 127;
- System.out.println(i3 == i4);
- }
- }
6. What is the output of following program?
- package com.instanceofjava;
- class A
- {
- void method(int i)
- {
- }
- }
- class B extends A
- {
- @Override
- void method(Integer i)
- {
- }
- }
7. Which line will throw compile time error? 8 or 9?
- package com.instanceofjava;
- class A
- {
- public static void main(String [] args)
- {
- Integer i = new Integer(null);
- String s = new String(null);
- }
- }
8.What is the output of following program?
- package com.instanceofjava;
- class A
- {
- public static void main(String [] args)
- {
- String s = "ONE"+3+2+"TWO"+"THREE"+5+4+"FOUR"+"FIVE"+5;
- System.out.println(s);
- }
- }
9.What is the output of following program?
- package com.instanceofjava;
- class A
- {
- static int method1(int i)
- {
- return method2(i *= 11);
- }
- static int method2(int i)
- {
- return method3(i /= 11);
- }
- static int method3(int i)
- {
- return method4(i -= 11);
- }
- static int method4(int i)
- {
- return i += 11;
- }
- public static void main(String [] args)
- {
- System.out.println(method1(11));
- }
- }
10.What is the output of following program?
- package com.instanceofjava;
- class A
- {
- public static void main(String [] args)
- {
- System.out.println(null);
- }
- }
Explanation:
What happens When System.out.println(null)?
- Print prime numbers?
- Java Program Find Second highest number in an integer array
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
Hmm 3. What is the output of following program? rly? mb you try to use ide to see right answer?
ReplyDeletenice suggestion. but we executed all programs before posting actually 3rd program out put will be 2 always even you change the value of a. its a logic based program.
Deletea-- - --a=2 even you change the value of a. try it better anyway thanks for comment
Hello,
ReplyDeletevery Nice and Informative collection of Core Java Interview Coding Questions and Answers and thank you for sharing, I must admit few of these I never encountered.I also ask some of them at one of the am searching same java questions and answers Forum, I found your blog very Interesting . That really help me out in various queries of core java.
Thanks
How it will going to recursive
ReplyDeletepublic class B
{
B b= new B();
public void show()
{
}
public static void main(String[] args)
{
B b1= new B();
b1.show();
System.out.println("done");
}
}
Thankyou ... Was useful
ReplyDeleteuseful....thanks
ReplyDeleteuseful thanks
ReplyDeleteclass A{
ReplyDelete{
System.out.print("compiles without error how?")
}
}
Really good questions.Thanks for sharing.
ReplyDeleteCan u please explain no 7
ReplyDeleteSystem.out.println(i3 == i4);