0% found this document useful (0 votes)
20 views5 pages

Java Code Output Predictions and Explanations

The document contains a series of Java programming questions and code snippets, each asking for the output or explanation of specific code behaviors. It covers topics such as constructors, static variables, logical operators, bitwise operations, and inheritance. The questions require understanding of Java syntax and concepts to predict outputs accurately.

Uploaded by

Mihir Parida
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Java Code Output Predictions and Explanations

The document contains a series of Java programming questions and code snippets, each asking for the output or explanation of specific code behaviors. It covers topics such as constructors, static variables, logical operators, bitwise operations, and inheritance. The questions require understanding of Java syntax and concepts to predict outputs accurately.

Uploaded by

Mihir Parida
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q.1.

Find the output of the code:


class Sample{
Sample(){
[Link]("Sample constructor");
}
Sample(int x){
this();
[Link](x);
}
}
class Demo4{
public static void main(String args[]){
Sample a = new Sample(50);
}
}
Q.2. Predict the output of the following code. Justify it.
class Demo{
int rollno;
static String college="Silicon";
Demo(int r){
rollno=r;
}
static void change(){
college = "SIT";
rollno=5;
}
void display (){
[Link](rollno+" "+college);
}
}
class Test{
public static void main(String args[]){
Demo ob=new Demo(10);
[Link]();
[Link]();
}
}

Q.3. What will be the output of the below code?


class Demo {
static int p;
static {
p = 11;
[Link](p + " ");
}
static{
p++;
[Link](p + " ");
}
public static void main(String[] args) {
p++;
[Link](p);
}
}
Q.4. What will be the output of the following java code?
class Test
{
public static void main(String[] args)
{
int x = 5;
boolean res = (x < 4) & (++x < 10);
[Link]("x="+x);
[Link]("res="+res);
}
}
Q.5. What will be the output of the following java code?
class Test
{
public static void main(String[] args)
{
int x = 5;
boolean res = (x < 4) && (++x < 10);
[Link]("x="+x);
[Link]("res="+res);
}
}

Q.6. Difference between Logical AND(&&) and Bitwise AND(& ) operator.

Q.7. What will be the output of the following java code?


class Test
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
[Link](50>>i);
}
}

Q.8. What will be the output of the following java code?


class Test
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
[Link](10<<i);
}
}
Q.9. What will be the output of the following code?
class Test {
public static void main(String[] args) {
int a = 10;
int b = ++a;
int c = a++;
[Link]("a: %d \nb: %d \nc: %d", a, b, c);
}
}
Q.10. What will be the output of the below code?
public class Test {
public static void main(String args[]) {
int age=20;
boolean flag;
flag=(age>20)?true:false;
[Link]("age=" + flag);
}
}
Q.11. What is the output of the following Java program?
class SIT
{
public static void main (String args[])
{
[Link](10 + 20 + "SILICON");
[Link]("SILICON" + 10 + 20);
}
}
Q.12. What will be the output?

class B{
B(){
[Link]("Hello B");
}
B(double a){
this();
[Link]("Hello");
}
B(int a){
this();
[Link](a);
}
}
class Demo {
public static void main(String []args) {
B b1=new B(25);
[Link]("use of this keyword");
}
}

Q.13.
What will be the output of the below code?
class Demo {
static int a = 10;
}
class Driver {
public static void main(String []args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.a = 20;
[Link]( d2.a);
}
}
Q,14. What will be the output of the below code?
class Example{
static int num;
static {
num = 68;
}
static {
[Link]("Programming");
num = 67;
}
public static void main(String args[]) {
[Link]("Value of num: "+num);
}}
Q.15. What will be the output of the following code if the command line arguments
are provided as:

java Test 10 20 30 40

class Test{
public static void main(String args[]){
[Link](args[2]);
}
}
Q.16. What will be the output of the below program:
class A
{
A(){ [Link](" A class Const"); }
}
class B extends A
{
B(){ [Link](" B class Const"); }
}
class C extends B
{
C(){ [Link](" C class Const"); }
}
class D extends C
{
D(){ [Link](" D class Const"); }
}
class E extends C
{
E(){ [Link](" E class Const"); }
}
class Test{
public static void main( String args[]){
D ob=new D( );
}
}
Q.17. Justify the output of the following code:
class Vehicle{
final void sell(){
[Link]("Sold out");
}
}
class Car extends Vehicle {
void sell() {
[Link]("Only 1 Car left");
}
public static void main(String args[]){
Car obj= new Car();
[Link]();
}
}
Q.18. Find the output for the following code:
class Foo {
public int i;
private int j;
}
class Bar extends Foo {
void display() {
super.j = super.i + 1;
[Link](super.i + " " + super.j);
}
}
class Testo {
public static void main(String args[]) {
Bar obj = new Bar();
obj.i = 1;
obj.j = 2;
[Link]();
}
}

Common questions

Powered by AI

The output of the program is 'Programming \n Value of num: 67'. Static initializers in Java are executed in the order they appear in the class, before the main method is executed. Here, two static blocks first set and then modify the value of 'num', while the main method outputs the latest set value.

The code results in a compilation error because 'j' is private in Foo and cannot be accessed or modified in its subclass Bar, demonstrating that private members are not inherited nor accessible in subclasses. This ensures encapsulation and private adherence despite inheritance.

The output of the code is '50 \n 25 \n 12 \n 6 \n 3'. The bitwise right shift operator '>>' effectively divides the number by 2^i at each iteration, shifting the bits to the right by 'i' positions in each loop iteration.

In the example, 'this()' invokes another constructor of the same class. For class B's constructor with 'this();', it calls the no-argument constructor to print 'Hello B'. For 'B(int a)', it also calls 'B()' first, thus printing 'Hello B25 use of this keyword'. It ensures reusability within constructors.

The output is 'age=false'. The ternary operator evaluates '(age>20)'. Since 'age' is 20, the condition is false, so the operator returns the second value, 'false'. It provides a concise conditional logic for variable assignments.

The output will be '30'. Java command line arguments are passed to the 'args' array in the order they are provided. The index starts at 0, so 'args[2]' refers to the third argument provided, which is '30'.

The output is '30SILICON' and 'SILICON1020'. In Java, the '+' operator concatenates strings with operands, evaluating left-to-right. With '10 + 20' first evaluated as integer sum (30), and in 'SILICON' + '10' + '20', both are treated as strings due to precedence and left-to-right evaluation.

The output of the code would be "A class Const B class Const C class Const D class Const". This demonstrates that in Java, constructors are called in the order of inheritance, from the base class to the derived class. In this case, class D extends C extends B extends A, so the constructors are invoked following this chain.

In the first code using '&', the expression on the right (++x < 10) is evaluated regardless of the left expression (x < 4), resulting in x=6 and res=false. With '&&', the right-side expression is not evaluated because the left side is false, so x remains 5 and res is false. The short-circuit operator '&&' prevents unnecessary evaluation, which is not the case with '&'.

The output is '20'. Static members belong to the class rather than any instance, so changes via one instance (d1.a = 20) affect the static member 'a' for all instances. Thus, d2.a is also 20, illustrating a single copy shared among all class instances.

You might also like