You are on page 1of 4

Q1

How many static variables, local variables and instance variables are present in the
code given below?
public class Car {
private String vehicleNumber;
private String color;
private static int numberOfCars = 0;

public Car(String vehicleNumber, String color) {


this.vehicleNumber = vehicleNumber;
this.color = color;
}

public void display(String name) {


System.out.println("Owner's name: " + name);
System.out.println("Vehicle number: " + vehicleNumber);
System.out.println("Color: " + color);
}
}

Options :-

static variables: 1; instance variables: 2; local variables: 1


static variables: 3; instance variables: 0; local variables: 2
static variables: 1; instance variables: 2; local variables: 3
static variables: 3; instance variables: 2; local variables: 3

Q2 Which of the following is/are correct?

1. Static members can only be accessed by static methods.


2. Non-static members can be accessed by static methods.
3. Static blocks get executed only once.
4. Static variables and methods can be accessed without creating an object of the
class.

Q3 Choose the correct option based on the code given below.


class Person {
private String name;
private static int age;
static {
name = "john"; // line 1
}

Person() {
age = 20; // line 2
}

public int getAge() {


return age; // line 3
}
}

public class Account {


public static void main(String[] args) {
System.out.println(new Person().getAge());
}
}

1) Compilation error in Line 1 as a non-static member cannot be accessed inside a


static block.

2) Compilation error in Line 2 as a static member cannot be accessed inside a


constructor.

3) Compilation error in Line 3 as age is a static variable and cannot be accessed


inside a non-static method.

4) Code executes successfully and displays 20.

Q4 What will be the output of the code given below?

class Computer {
private static int id;
private static int counter = 0;

public Computer() {
id = ++counter;
}

public int getId() {


return id;
}

// Rest of the methods


}

public class Tester {


public static void main(String[] args) {
Computer comp1 = new Computer();
Computer comp2 = new Computer();
Computer comp3 = new Computer();
System.out.println(comp1.getId() + " " + comp2.getId() + " "
+ comp3.getId());
}
}

123
333
012
000

Q5
Predict the output of the code given below.
class Printer {
static {
System.out.println("Static block in Printer class");
}

public static void display(String message) {


System.out.println(message);
}
}

public class Tester {


public static int sampleVariable = 1;

static {
System.out.println("Static block in Tester class");
}

public static void main(String[] args) {


sampleVariable++;
Printer p = null;
System.out.println("In main");
p.display("The value of sample variable is: " + sampleVariable);
}
}

Static block in Printer class; Static block in Tester class; In main; The value of
sample variable is: 2
Static block in Tester class; Static block in Printer class; In main; The value of
sample variable is: 2
Static block in Tester class; In main; Static block in Printer class; The value of
sample variable is: 2
Static block in Tester class; In main; The value of sample variable is: 2; Static
block in Printer class

Q6
What will be the output of the below code ?
public class Tester {
public int var;

Tester(int var) {
System.out.println(this.var);
}

public static void main(String args[]) {


Tester tester = new Tester(20);
System.out.println(this.var);
}
}

20 20
00
compilation error: cannot use 'this' inside main
20 0

You might also like