class Test1 {
public
static void main(String[] args)
{
int x = 20;
System.out.println(x);
}
static
{
int x = 10;
System.out.print(x + " ");
}
}

Static in Java

Quiz
•
Computers
•
Professional Development
•
Hard
DEVAKI AMIR
Used 2+ times
FREE Resource
8 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
10 20
20 10
10 10
20 20
2.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
class Test1 {
static int x = 10;
public
static void main(String[] args)
{
Test1 t1 = new Test1();
Test1 t2 = new Test1();
t1.x = 20;
System.out.print(t1.x + " ");
System.out.println(t2.x);
}
}
10 10
20 20
10 20
20 10
Answer explanation
static variable is class level variable. if we do update in any reference then automatically all pointing reference value are changed.
3.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
class Test1 {
int x = 3;
public
static void main(String[] args)
{
System.out.println(x);
}
static
{
System.out.print(x + " ");
}
}
3 3
Error
Exception
None
Answer explanation
If we are trying to print the instance variable inside the static block or static method without creating class instance then it will give the error : non-static variable x cannot be referenced from a static context.
4.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
class Test1 {
static int i = 1;
public static void main(String[] args)
{
int i = 1;
for (Test1.i = 1; Test1.i < 10; Test1.i++) {
i = i + 2;
System.out.print(i + " ");
}
}
}
1 3 9
1 2 3 … 9
3 5 7 9 11 13 15 17 19
None
Answer explanation
Here, two different i copies of variable are declared, one is static and other one is local. If we write Test1.i then, static variable is executed and if we write only i, then local variable are executed.
5.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
class Test1 {
static int i = 1;
public static void main(String[] args)
{
for (int i = 1; i < 10; i++) {
i = i + 2;
System.out.print(i + " ");
}
}
}
3 6 9
3 6 9 …. 27
Error
Answer explanation
Here local variables are printed after execution. If we want to execute static variables, then we write Test1.i or we write Test1 object.i.
6.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
class overload
{int x;
int y;
void add(int a){
x = a + 1;
}
void add(int a, int b){
x = a + 2;
}}
class Overload_methods{
public static void main(String args[]){
overload obj = new overload();
int a = 0;
obj.add(6);
System.out.println(obj.x);
}
}
5
6
7
8
7.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
class overload{
int x;
double y;
void add(int a , int b){
x = a + b;}
void add(double c , double d){
y = c + d;}
overload(){
this.x = 0;
this.y = 0;
}
}
class Overload_methods{
public static void main(String args[]){
overload obj = new overload();
int a = 2;
double b = 3.2;
obj.add(a, a);
obj.add(b, b);
System.out.println(obj.x + " " + obj.y);
}
}
6 6
6.4 6.4
6.4 6
4 6.4
8.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
class A
{
int i;
public void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
public void display()
{
System.out.println(j);
}
}
class Dynamic_dispatch
{
public static void main(String args[])
{
B obj2 = new B();
obj2.i = 1;
obj2.j = 2;
A r;
r = obj2;
r.display();
}
}
1
2
3
4
Answer explanation
r is reference of type A, the program assigns a reference of object obj2 to r and uses that reference to call function display() of class B.
Similar Resources on Quizizz
8 questions
HiTech Quiz 5

Quiz
•
Professional Development
8 questions
Algoritmos y Programación C - Funciones y Procedimientos

Quiz
•
Professional Development
10 questions
c programming-Test 1

Quiz
•
Professional Development
10 questions
Collections MQC

Quiz
•
Professional Development
10 questions
Java main() and Scanner

Quiz
•
Professional Development
6 questions
SDA Java Podstawy 1

Quiz
•
Professional Development
8 questions
Punteros y sobrecarga

Quiz
•
Professional Development
11 questions
java 11-10-2023

Quiz
•
Professional Development
Popular Resources on Quizizz
15 questions
Character Analysis

Quiz
•
4th Grade
17 questions
Chapter 12 - Doing the Right Thing

Quiz
•
9th - 12th Grade
10 questions
American Flag

Quiz
•
1st - 2nd Grade
20 questions
Reading Comprehension

Quiz
•
5th Grade
30 questions
Linear Inequalities

Quiz
•
9th - 12th Grade
20 questions
Types of Credit

Quiz
•
9th - 12th Grade
18 questions
Full S.T.E.A.M. Ahead Summer Academy Pre-Test 24-25

Quiz
•
5th Grade
14 questions
Misplaced and Dangling Modifiers

Quiz
•
6th - 8th Grade