Find Output of Java Programs - Set 2 (Mixed Topics)

Find Output of Java program – 2: This section contains programs/code snippets on java and with the outputs and explanations; you have to find the output and match with the correct output.
By Abhishek jain Last updated : March 23, 2024

Program 1

class Q1 {

  public static void main(String args[]) {
    int a = 4;
    int b = 8;
    if ((b - a--) >= a) {
      if ((b + a) % 2 == 1)
        System.out.println(a * b);
      else
        System.out.println(b + a);
    }
  }

}

Output

24

Program 2

class Q2 {
  public static void main(String args[]) {
    int[] A = new int[8];
    int i = 0;
    for (i = -1; i < A.length - 1;) {
      A[++i] = i;
    }
    String res = "" + A[2] + (4 % 2) + (5 % 2) + i;
    System.out.print(res);
  }
}

Output

2017

Program 3

class A {
  int x = 10;
}
class B extends A {
  int x = 20;
}

public class Q3 {
  public static void main(String[] args) {
    A a = new B();
    System.out.print(a.x);
  }
}

Output

10

Reason: Derived class constructor first invokes the base class constructor.


Program 4

class Q4 {
  public static void main(String args[]) {
    int i = 3;
    int e = 6;
    int x = 7;

    String s = "";

    if ((i + e) >= (i + x)) {
      i++;
      s = "gg";
      e--;
    } else if ((i + e + x) >= 15) {
      x++;
      s = "wp";
    }

    System.out.print(x + s);
  }
}

Output

8wp

Program 5

class Q5 {
  public static void main(String args[]) {
    String s1 = new String("11001");
    int d = Integer.parseInt(s1, 2);
    System.out.print(d);
  }
}

Output

25

Reason: The function parseInt(string , int) return decimal value according to specified string and base value.


Program 6

class Base {
  Base() {
    System.out.println("Base");
  }
  Base(int n) {
    System.out.println("Base:" + n);
  }
}

class Derive extends Base {
  Derive() {
    super(10);
    System.out.println("Derive");
  }
  Derive(int n, int m) {
    super(n);
    System.out.println("Derive:" + n + "," + m);
  }
}

class Q6 {
  public static void main(String args[]) {
    Derive D1 = new Derive();
    Derive D2 = new Derive(40, 50);
  }
}

Output

Base:10
Derive
Base:40
Derive:40,50

Reason: Super Keyword is use to call the base class parameterized constructor through derive class according to specified parameter.


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.