OOP QUIZ 2

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/32

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:34 AM on 8/13/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

33 Terms

1
New cards

What is the size of an integer variable?

32 bit

2
New cards

Does garbage collection guarantee that a program will not run out of memory? (True/False)

False

3
New cards

What is the output? int i=0; boolean t=true; boolean f=false,b; b=(t & ((i++)==0)); b=(f & ((i+=2)>0)); System.out.print(i);

3 (bitwise & always evaluates both sides, even when the first operand is false)

4
New cards

What is the output? int i=0; boolean t=true; boolean f=false, b; b=(t && ((i++)==0)); b=(f && ((i+=2)>0)); System.out.print(i);

1 (short-circuit && skips the second expression once f makes it false)

5
New cards

Which of these lines compile without error? 1) boolean i=true, j=false; if(i && j) … 2) boolean b=true; if(b) … 3) int i=2, j=2; if(i==1 | j==2) … 4) int i=0; if(i) …

1, 2, and 3 compile. 4 does NOT — an if-condition must be boolean, not int (unlike C/C++)

6
New cards

Which of these are legal identifiers? _whatavariable, variable2, 2variable, $2, #myvar, for, ___, if, system

Legal: _whatavariable, variable2, $2, ___, system. Illegal: 2variable (starts with digit), #myvar (bad symbol), for and if (reserved keywords)

7
New cards

What is the output? boolean x=true; int a; if(x) a = x ? 1 : 2; else a = x ? 3 : 4; System.out.print(a);

1

8
New cards

What is an applet?

A Java program that runs inside a Web browser

9
New cards

Is it possible to force garbage collection in Java? (True/False)

False — you can only request it with System.gc(), never force it

10
New cards

Primitive variables are stored on the stack. (True/False)

True

11
New cards

What is the output? boolean a[] = new boolean[10]; for(int i=5; i<=10; i++)     System.out.print(a[i]);

error — ArrayIndexOutOfBoundsException at i=10 (valid indices are 0–9 for a size-10 array)

12
New cards

When an object is passed as a parameter to a method, is a copy of the object created? (True/False)

False — a copy of the reference is passed, not the object itself

13
New cards

Local variables are automatically assigned a default value if not explicitly initialized. (True/False)

False — only member/instance variables get automatic defaults

14
New cards

What is the output? int i=5; if(i==5){     int j=10, j+=i; } System.out.println(j);

error — invalid declaration syntax, and j is out of scope outside the if-block anyway

15
New cards

Which of these compiles without warning or error: short s=900000; char c=10;

Only char c=10. short s=900000 is a compile error (900000 exceeds the short range of -32768 to 32767)

16
New cards

What is the output? int temp=1, temp2=0, ans=0, num=5; for(int i=1; i<=num; i++){     ans = temp + temp2;     temp = temp2;     temp2 = ans;     System.out.print(ans + " "); }

1 1 2 3 5

17
New cards

What is the output? int i=1; switch(i) {     default: System.out.println("default");     case 0: System.out.println("zero"); break;     case 1: System.out.print("one");     case 2: System.out.print("two"); }

onetwo (matches case 1, then falls through into case 2 since there's no break)

18
New cards

What size range can a byte hold?

-128 to 127

19
New cards

Which of these is a legal definition of the main method?

public static void main(String args[])

20
New cards

What is the output? int x=0, y=1, z; if(x) z=0; else z=1; if(y) z=2; else z=3; System.out.print(z);

error — int (x, y) cannot be used as a boolean if-condition in Java

21
New cards

It is possible to change the contents of a String object. (True/False)

False — Strings are immutable

22
New cards

Which of these is NOT a Java keyword: public, private, void, Boolean, static

Boolean — it's a class name (wrapper class), not a keyword. The actual keyword is lowercase "boolean"

23
New cards

What happens when you compile and run this? public class Main{     public static void main(String argv){         System.out.println("Hello world");     } }

The code will compile, but will complain at run time that main is not correctly defined (missing the String[] args array signature)

24
New cards

What is the default value of a String variable?

null

25
New cards

What is the size of a double type variable?

64 bits

26
New cards

Can a constructor be declared static? (True/False)

False

27
New cards

Is a default constructor always automatically provided? (True/False)

False — only if the class defines NO constructors at all; defining any constructor removes the automatic default

28
New cards

A class can define two methods with the same name and same arguments as long as the return types are different. (True/False)

False — overloading requires a different parameter list; return type alone is not enough

29
New cards

Member/instance variables are automatically assigned a default value if not explicitly initialized. (True/False)

True

30
New cards

What is the output? class HelloWorld {     private int x=10;     private static int y=6;     public void display(){         System.out.print("x = "+x);         System.out.print("y = "+y);     }     public static void main(String[] args) {         HelloWorld h=new HelloWorld();         h.display();     } }

x = 10y = 6 — compiles and runs fine; private fields are accessible from methods within their OWN class, even instance methods reading them

31
New cards

What is the output? class Dummy {     public static void main(String[] args) {         int x=0,y=1,z;         if(x) z=0; else z=1;         if(y) z=2; else z=3;         System.out.print(z);     } }

error — if(x) uses an int as a condition, which Java does not allow (must be boolean)

32
New cards

What is the output? class HelloWorld {     private int x;     public HelloWorld(int x){         x = x;     }     public void printX(){         System.out.print("x = "+x);     }     public static void main(String[] args) {         HelloWorld h=new HelloWorld(20);         h.printX();     } }

x = 0 — no compile error. The parameter x shadows the field x, so "x = x;" just reassigns the parameter to itself; the field never gets set and keeps its default value 0. Fix: this.x = x;

33
New cards

What is the output? class HelloWorld {     private int x=10;     public static void printX(){         System.out.print("x = "+x);     }     public static void main(String[] args) {         HelloWorld h=new HelloWorld();         h.printX();     } }

error — cannot reference the non-static field x from a static method (static context has no access to instance fields direct