1/32
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is the size of an integer variable?
32 bit
Does garbage collection guarantee that a program will not run out of memory? (True/False)
False
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)
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)
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++)
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)
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
What is an applet?
A Java program that runs inside a Web browser
Is it possible to force garbage collection in Java? (True/False)
False — you can only request it with System.gc(), never force it
Primitive variables are stored on the stack. (True/False)
True
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)
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
Local variables are automatically assigned a default value if not explicitly initialized. (True/False)
False — only member/instance variables get automatic defaults
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
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)
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
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)
What size range can a byte hold?
-128 to 127
Which of these is a legal definition of the main method?
public static void main(String args[])
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
It is possible to change the contents of a String object. (True/False)
False — Strings are immutable
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"
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)
What is the default value of a String variable?
null
What is the size of a double type variable?
64 bits
Can a constructor be declared static? (True/False)
False
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
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
Member/instance variables are automatically assigned a default value if not explicitly initialized. (True/False)
True
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
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)
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;
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