Output of Java program | Set 26
Last Updated :
23 Jul, 2024
Ques1: What is the output of this program?
Java
class A {
public int i;
private int j;
}
class B extends A {
void display()
{
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}
class inheritance {
public static void main(String args[])
{
B obj = new B();
obj.i = 1;
obj.j = 2;
obj.display();
}
}
a) 2 2 b) 3 3 c) Runtime Error d) Compilation Error
Answer: d
Explanation:
Class A contains a private member variable j, this cannot be inherited by subclass B. So in class B we can not access j. So it will give a compile time error.
Ques2: What is the output of this program?
Java
class selection_statements {
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
options:a) 1 b) 2 c) 3 d) 4
Answer: b
Explanation:
In "If" statement, first var2 is initialized to 1 and then condition is checked whether var2 is equal to var1. As we know var1 is 5 and var2 is 1, so condition will be false and
else part will be executed
.
Ques3: What is the output of this program?
Java
class comma_operator {
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
options: a) 5 b) 6 c) 14 d) compilation error
Answer: b
Explanation:
Using comma operator, we can include more than one statement in the initialization and iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed i gets the value : 0, 1, 2, 3, and j gets the values : 0, 1, 2, 3, 4, 5.
Ques4. What will be the output of the program?
Java
class Geeks {
public static void main(String[] args)
{
Geeks g = new Geeks();
g.start();
}
void start()
{
long[] a1 = { 3, 4, 5 };
long[] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long[] fix(long[] a3)
{
a3[1] = 7;
return a3;
}
}
options: a) 12 15 b) 15 15. c) 3 4 5 3 7 5 d) 3 7 5 3 7 5
Answer: b
Explanation:
The reference variables a1 and a3 refer to the same long array object. When fix() method is called, array a1 is passed as reference. Hence the value of a3[1] becomes 7 which will be reflected in a1[] as well because of
call by reference. So the a1[] array become {3, 7, 5}. WHen this is returned to a2[], it becomes {3, 7, 5} as well. So Output: 3 + 7 + 5 + " " + 3 + 7 + 5 = 15 15
Ques5. What will be the output of the program?
Java
class BitShift {
public static void main(String[] args)
{
int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}
options: a) -2147483648 and 1 b) 0x80000000 and 0x00000001 c) -2147483648 and -1 d) 1 and -2147483648
Answer: a
Explanation:
Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this:
Before: 1000 0000 0000 0000 0000 0000 0000 0000
After: 0000 0000 0000 0000 0000 0000 0000 0001
Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown.
Similar Reads
Output of Java program | Set 27
Ques1. What is the output of the following? Java import java.util.*; public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } } Options : A. The program has a compile error because the size of the array wasn't specified whe
3 min read
Output of Java program | Set 28
Question 1. What is the output of the following question? JAVA class Test { int a = 10; static int b = 20; public static void main(String[] args) { Test t1 = new Test(); t1.a = 100; t1.b = 200; Test t2 = new Test(); System.out.println("t1.a =" + t1.a + " t1.b =" + t1.b); System.o
4 min read
Output of Java program | Set 5
Predict the output of following Java Programs.Program 1: Java // Main.java public class Main { public static void gfg(String s) { System.out.println("String"); } public static void gfg(Object o) { System.out.println("Object"); } public static void main(String args[]) { gfg(null);
3 min read
Output of Java programs | Set 29
Question 1. What is the output of the following question? JAVA class Test1 { public static void main(String[] args) { int String = 65; int Runnable = 97; System.out.print(String + " : " + Runnable); } } Option A) Error B) A : a C) 65 : 97 D) None Output: C Explanation : We can use all pred
2 min read
Output of Java Program | Set 3
Predict the output of the following Java Programs: Example1: Java // filename: Test.java class Test { // Declaring and initializing integer variable int x = 10; // Main driver method public static void main(String[] args) { // Creating an object of class inside main() Test t = new Test(); // Printin
3 min read
Output of Java Programs | Set 32
Prerequisite : Arrays in Java Question 1. What is the output of following program? JAVA class ArrayDemo { public static void main(String[] args) { int arr1[] = { 1, 2, 3, 4, 5 }; int arr2[5] = { 1, 2, 3, 4, 5 }; for (int i = 0; i < 5; i++) System.out.print(arr1[i] + " "); System.out.pri
3 min read
Output of Java Program | Set 7
Difficulty level : Intermediate Predict the output of following Java Programs. Program 1 : Java public class Calculator { int num = 100; public void calc(int num) { this.num = num * 10; } public void printNum() { System.out.println(num); } public static void main(String[] args) { Calculator obj = ne
4 min read
Output of Java Program | Set 9
Difficulty level : Intermediate Predict the output of following Java Programs. Program 1: Java class Gfg { // constructor Gfg() { System.out.println("Geeksforgeeks"); } static Gfg a = new Gfg(); //line 8 public static void main(String args[]) { Gfg b; //line 12 b = new Gfg(); } } Output: G
3 min read
Output of Java Programs | Set 30
QUE.1 What is the output of this program ? JAVA public class Prg { public static void main(String args[]) { System.out.print('A' + 'B'); } } OPTION a) AB b) 195 c) 131 d) Error Answer: c Explanation: Here, âAâ and âBâ are not strings they are characters. âAâ and âBâ will not concatenate. The ASCII o
2 min read
Output of Java Programs | Set 31
Prerequisite : Arrays in Java 1. What will be the output of the following program? Java public class Test { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; System.out.println(arr); } } Options: 1. 1 2. Compile-time error 3. 1 2 3 4 5 4. [I@Hashcode_in_Hexadecimal Output: The
3 min read