Showing posts with label basic. Show all posts
Showing posts with label basic. Show all posts

Saturday, May 11, 2019

what will be the output of the following code?

Problem statement:
what will be the output of the following code?

  1. class ExecutionEight {
  2.     public int number = 0;
  3. }
  4. public class ExecutionSeven {
  5.     public void doIt(int i, ExecutionEight e) {
  6.         i = 5;
  7.         e.number = 8;
  8.     }
  9.     public static void main(String[] args) {
  10.         int x = 0;
  11.         ExecutionEight e = new ExecutionEight();
  12.         new ExecutionSeven().doIt(x, e);
  13.         System.out.println(x + " " + e.number);
  14.     }
  15. }
Output:
0 8

what will be the console output of the following code? Explain why?

Problem statement:
what will be output and explain why?
  1. public class ExecutionFive {
  2.     public static void main(String[] args) {
  3.         String s1 = "Cat";
  4.         String s2 = "Cat";
  5.         System.out.println(s1 == s2);
  6.     }
  7. }
Output:
true
Explanation - whenever we create an object of String class using 'string literal', string literal means using double quotation mark(" "), then that object stores in a place called string constant pool of heap. So, what JVM does, it checks the object in the string constant pool area, if that object is not available there, then it stores the new object. if the object is already available in string constant pool, then instead of creating new object, it points to the existing object, in this way we are saving the space, time and ultimately good performance.

Below is the memory diagram for string constant pool


what is the output of the of following code? Explain.

Problem statement:
Both the classes ExecutionThree and ExecutionFour are saved in ExecutionThree.java
  1. public class ExecutionThree {
  2.     public static void main(String[] args) {
  3.         System.out.println("ExecutionThree");
  4.     }
  5. }
  6. class  ExecutionFour{
  7.     public static void main(String[] args) {
  8.         System.out.println("ExecutionFour");
  9.     }
  10. }
Output:
Output will depend on how we want to run the class. 
For example:-
If we are running this program in any of the IDE(Integrated Development Environment) without terminal then the IDE will print the output as ExecutionThree

If we are running this program using terminal even in the IDE's terminal, then we must use java tool command.
For it below are the steps:
step-1: javac FileName.java
            javac ExecutionThree.java
step-2: java ClassName
            java ExecutionThree
step-3: output - ExecutionThree
step-4: java ExecutionFour
step-5: output - ExecutionFour

What do you think the output will be for the following code?

Problem statement:
Can you let me know the output of the following code?
  1. public class ExecutionTwo {
  2.     public static void main(String[] args) {
  3.         // primitive data type
  4.         double d1 = 0.8;
  5.         double d2 = 0.8;
  6.         System.out.println(d1 == d2);
  7.         double d3 = 0.8d;
  8.         double d4 = 0.8d;
  9.         System.out.println(d3 == d4);
  10.         double d5 = 0.8D;
  11.         double d6 = 0.8D;
  12.         System.out.println(d5 == d6);
  13.         // Wrapper class
  14.         Double d7 = 0.8;
  15.         Double d8 = 0.8;
  16.         System.out.println(d7.equals(d8));
  17.         Double d9 = 0.8d;
  18.         Double d10 = 0.8d;
  19.         System.out.println(d9.equals(d10));
  20.         Double d11 = 0.8D;
  21.         Double d12 = 0.8D;
  22.         System.out.println(d11.equals(d12));
  23.     }
  24. }
Output:
true
true
true
true
true
true

What do you think the output will be for the following code?

Problem statement:
Can you let me know the output of the following code?
  1. public class ExecutionOne {
  2.     public static void main(String[] args) {
  3.         double d1 = 0.8;
  4.         double d2 = 0.8;
  5.         System.out.println(d1 == d2);
  6.         Double d3 = 0.8;
  7.         Double d4 = 0.8;
  8.         System.out.println(d3 == d4);
  9.     }
  10. }
Output:
true
false

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...