Static in Java

Static in Java

Professional Development

8 Qs

quiz-placeholder

Similar activities

6. Method

6. Method

Professional Development

10 Qs

Java Programming RBVRRIT

Java Programming RBVRRIT

University - Professional Development

10 Qs

2.13.2022 | Introduction to Java Review

2.13.2022 | Introduction to Java Review

Professional Development

8 Qs

Kotlin Quiz 4

Kotlin Quiz 4

Professional Development

12 Qs

Java Test 2

Java Test 2

Professional Development

10 Qs

Punteros y sobrecarga

Punteros y sobrecarga

Professional Development

8 Qs

Java Programming Quiz #1

Java Programming Quiz #1

Professional Development

10 Qs

Java Fundamentals

Java Fundamentals

Professional Development

11 Qs

Static in Java

Static in Java

Assessment

Quiz

Computers

Professional Development

Hard

Created by

DEVAKI AMIR

Used 2+ times

FREE Resource

8 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

  1. class Test1 {

    public

        static void main(String[] args)

        {

            int x = 20;

            System.out.println(x);

        }

        static

        {

            int x = 10;

            System.out.print(x + " ");

        }

    }

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

  1. class overload

  2. {int x;

  3. int y;

  4. void add(int a){

  5. x = a + 1;

  6. }

  7. void add(int a, int b){

  8. x = a + 2;

  9. }}

  10. class Overload_methods{

  11. public static void main(String args[]){

  12. overload obj = new overload();

  13. int a = 0;

  14. obj.add(6);

  15. System.out.println(obj.x);

  16. }

  17. }

5

6

7

8

7.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

  1. class overload{

  2. int x;

  3. double y;

  4. void add(int a , int b){

  5. x = a + b;}

    void add(double c , double d){

  6. y = c + d;}

  7. overload(){

  8. this.x = 0;

  9. this.y = 0;

  10. }

  11. }

  12. class Overload_methods{

  13. public static void main(String args[]){

  14. overload obj = new overload();

  15. int a = 2;

  16. double b = 3.2;

  17. obj.add(a, a);

  18. obj.add(b, b);

  19. System.out.println(obj.x + " " + obj.y);

  20. }

  21. }

6 6

6.4 6.4

6.4 6

4 6.4

8.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

  1. class A

  2. {

  3. int i;

  4. public void display()

  5. {

  6. System.out.println(i);

  7. }

  8. }

  9. class B extends A

  10. {

  11. int j;

  12. public void display()

  13. {

  14. System.out.println(j);

  15. }

  16. }

  17. class Dynamic_dispatch

  18. {

  19. public static void main(String args[])

  20. {

  21. B obj2 = new B();

  22. obj2.i = 1;

  23. obj2.j = 2;

  24. A r;

  25. r = obj2;

  26. r.display();

  27. }

  28. }

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.