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? 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 when dec
3 min read
Output of Java programs | Set 29
Question 1. What is the output of the following question? 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 predefine
2 min read
Output of Java Programs | Set 32
Prerequisite : Arrays in Java Question 1. What is the output of following program? 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.println(
3 min read
Output of Java Program | Set 1
Difficulty Level: Rookie Predict the output of the following Java Programs.Program 1: Java Code // filename Main.java class Test { protected int x, y; } class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.x + " " + t.y); } } Output: 0 0 I
3 min read
Output of Java Program | Set 3
Predict the output of the following Java Programs: Example1: Java Code // 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(); // Pr
3 min read
Output of Java Programs | Set 30
QUE.1 What is the output of this program ? 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 of the
2 min read
Output of Java Programs | Set 31
Prerequisite : Arrays in Java 1. What will be the output of the following program? Java Code 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:
3 min read
Output of Java Programs | Set 50
Q 1. What is the output of this program? [GFGTABS] Java class Test { public final int a; } class Example { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.a); } } [/GFGTABS]Option A. 0 B. Garbage value C. Compile time error : variable is not initialized D. Run
3 min read
Output of Java Programs | Set 49
Find the output of below java program Q 1. What is the output of this program? Java Code public class Example { int x = 10; public static void main(String args[]) { Example obj; System.out.println(obj.x); } } Option A. 10 B. 0 C. Compile time error D. Run time error Output: C. Compile time error Exp
3 min read
Output of C++ Program | Set 20
Predict the output below program #include<iostream> using namespace std; int main() { int x = 1 , y = 1, z = 1; cout << (++x || ++y && ++z ) << endl; cout << x << " " << y << " " << z ; return 0; } Explanation:- It is based
2 min read