Unit 1 - Practice Quiz

CSE310 61 Questions
0 Correct 0 Wrong 61 Left
0/61

1 Who is known as the father of Java?

History and Features of Java Easy
A. Dennis Ritchie
B. James Gosling
C. Guido van Rossum
D. Bjarne Stroustrup

2 What does JVM stand for?

Understanding JDK, JRE and JVM Easy
A. Java Virtual Module
B. Java Virtual Machine
C. Java Very-fast Machine
D. Java Verified Module

3 What is the correct file extension for a Java source code file?

Java program structure Easy
A. .java
B. .class
C. .jar
D. .exe

4 Which method is the entry point for a Java application?

Writing simple Java class and main() method Easy
A. public static void main(String[] args)
B. public void main()
C. public void start(String[] args)
D. private static int run()

5 Which of these is NOT a primitive data type in Java?

Data In the Cart : Using primitive data types Easy
A. boolean
B. String
C. char
D. int

6 Which of the following is a reserved keyword in Java?

Keywords Easy
A. class
B. System
C. string
D. main

7 Which syntax is correct for declaring an integer variable named score and initializing it to 100?

Variables Easy
A. int score = 100;
B. int score; score = 100
C. score = 100;
D. integer score = 100;

8 What is the result of the arithmetic operation 10 % 3 in Java?

Operators : Working with Bit-wise, arithmetic, logical, and relational operators Easy
A. 3
B. 1
C. 3.33
D. 0

9 Which statement is used to make a decision between two alternatives in Java?

Conditional Statements : Using if/else constructs and switch-case statements Easy
A. while
B. if-else
C. for
D. switch

10 Which access modifier makes a class member accessible only within its own class?

Access modifiers Easy
A. private
B. public
C. protected
D. default

11 Which component is responsible for providing the libraries and the JVM required to run a Java application?

Understanding JDK, JRE and JVM Easy
A. Javac
B. JRE
C. JDK
D. JVM

12 A method or variable declared with which keyword belongs to the class, rather than to any specific instance (object)?

static keyword Easy
A. static
B. private
C. new
D. final

13 Which wrapper class is used to wrap the primitive data type double?

Wrapper class Easy
A. String
B. Float
C. Int
D. Double

14 In the main method public static void main(String[] args), what does args represent?

Command-line arguments Easy
A. A list of available methods
B. An array of strings passed as command-line arguments
C. A keyword for starting the program
D. The name of the Java file

15 What is the process of converting a larger data type to a smaller data type size called?

Type conversion Easy
A. Narrowing (Explicit Casting)
B. Boxing
C. Unboxing
D. Widening (Implicit Casting)

16 Which of the following is NOT a valid identifier in Java?

Identifiers Easy
A. myVariable2
B. $amount
C. 2variable
D. _myVariable

17 Which of the following is the ternary operator in Java?

Unary, assignment and Ternary operator Easy
A. |
B. ?:
C. &&
D. ? :

18 Which relational operator is used to check if two values are equal?

Operators : Working with Bit-wise, arithmetic, logical, and relational operators Easy
A. ==
B. !=
C. <>
D. =

19 In a switch statement, which keyword is used to terminate a case block?

Conditional Statements : Using if/else constructs and switch-case statements Easy
A. exit
B. continue
C. break
D. stop

20 In the Java expression int result = 5 + 2 * 3;, what will be the final value of result?

Operator precedence Easy
A. 13
B. 21
C. 11
D. 9

21 A developer writes a Java application on a Windows machine using JDK 11. They compile it into bytecode. If this bytecode is then moved to a Linux machine that only has JRE 11 installed, what is the expected outcome?

Understanding JDK, JRE and JVM Medium
A. The application will run successfully, as the JRE contains the necessary JVM and libraries to execute platform-independent bytecode.
B. The application will not run because it was compiled on a different operating system.
C. The application will run, but only if the Linux machine also has the JDK 11 installed.
D. The application will fail to run because the JRE does not include a compiler, and the code needs to be re-compiled on Linux.

22 What is the output of the following Java code snippet?
java
public class ConversionTest {
public static void main(String[] args) {
byte b = 127;
b++;
System.out.println(b);
}
}

Type conversion Medium
A. 128
B. -128
C. Compilation Error
D. 0

23 Predict the output of the following Java code:
java
class Counter {
static int staticCount = 0;
int instanceCount = 0;

Counter() {
staticCount++;
this.instanceCount++;
}

public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println(c3.staticCount + " " + c3.instanceCount);
}
}

static keyword Medium
A. 3 1
B. 1 1
C. 3 3
D. 1 3

24 What is the value of the variable result after the following code is executed?
java
int x = 5, y = 10, z = 3;
int result = x + y * z / x - y % z;

Operator precedence Medium
A. 9
B. 4
C. 10
D. 6

25 What will be printed by the following code snippet?
java
int score = 80;
String grade = "";
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} if (score >= 70) { // Note: this is a new if, not an else if
grade = "C";
}
System.out.println(grade);

Conditional Statements : Using if/else constructs and switch-case statements Medium
A. B
B. C
C. Compilation Error
D. BC

26 Consider the following Java program, which is compiled and then run with the command: java Main 10 20
java
public class Main {
public static void main(String[] args) {
String s = args[0] + args[1];
System.out.println(s);
}
}

What is the output?

Command-line arguments Medium
A. The code fails to compile.
B. An ArrayIndexOutOfBoundsException is thrown.
C. 30
D. 1020

27 What is the output of the following code?
java
public class WrapperTest {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
Integer i3 = 128;
Integer i4 = 128;

System.out.println(i1 == i2);
System.out.println(i3 == i4);
}
}

Wrapper class Medium
A. false
true
B. true
false
C. false
false
D. true
true

28 What is the final value of x after this code snippet executes?
java
int x = 13; // binary 1101
int y = 9; // binary 1001
x = x ^ y ^ y;

Operators : Working with Bit-wise, arithmetic, logical, and relational operators Medium
A. 0
B. 9
C. 4
D. 13

29 What is printed to the console?
java
public class UnaryTest {
public static void main(String[] args) {
int a = 5;
int b = 5;
int x = ++a + b++;
System.out.println(x + "," + a + "," + b);
}
}

Unary, assignment and Ternary operator Medium
A. 12,6,6
B. 11,6,5
C. 11,6,6
D. 10,5,6

30 Consider a single Java source file named University.java containing the following two class definitions. What is the result of attempting to compile this file?
java
class Student {
// ... members
}

public class University {
public static void main(String[] args) {
// ... logic
}
}

Java program structure Medium
A. Compilation fails because the Student class is not declared as public.
B. Compilation is successful, but only one file, University.class, is generated, containing the Student class as an inner class.
C. Compilation fails because a single .java file cannot contain more than one class definition.
D. Compilation is successful, and two .class files are generated: Student.class and University.class.

31 What is the output of this Java switch statement code?
java
public class SwitchFallthrough {
public static void main(String[] args) {
int level = 2;
int points = 0;
switch (level) {
case 1:
points += 10;
case 2:
points += 20;
case 3:
points += 30;
break;
default:
points += 5;
}
System.out.println(points);
}
}

Conditional Statements : Using if/else constructs and switch-case statements Medium
A. 30
B. 60
C. 20
D. 50

32 If a class member is declared with the protected access modifier, which of the following statements is true regarding its accessibility?

Access modifiers Medium
A. It is accessible only within the same package.
B. It is accessible only within the same class.
C. It is accessible within the same package and to subclasses in any package.
D. It is accessible within the same class and any subclasses, regardless of the package.

33 What is the output of the following program? It demonstrates logical short-circuiting.
java
public class ShortCircuit {
public static boolean methodA() {
System.out.print("A");
return true;
}
public static boolean methodB() {
System.out.print("B");
return false;
}
public static void main(String[] args) {
if (methodB() && methodA()) {
// do nothing
}
}
}

Operators : Working with Bit-wise, arithmetic, logical, and relational operators Medium
A. BA
B. AB
C. B
D. A

34 Which of the following lines of code will cause a compilation error in Java?

1. int _$;
2. double Double = 1.0;
3. boolean true = false;
4. String 1stName = "Alex";

Keywords, Identifiers, Variables Medium
A. Only line 3
B. Lines 3 and 4
C. Only line 4
D. Lines 2, 3, and 4

35 What is the output of this code snippet?
java
public class PrecisionTest {
public static void main(String[] args) {
double d = 0.1 + 0.1 + 0.1;
float f = 0.3f;
System.out.println(d == f);
}
}

Using primitive data types Medium
A. The code does not compile.
B. false
C. true
D. A runtime exception is thrown.

36 What is the result of compiling and running the following code?
java
public class StaticContext {
int instanceVar = 10;
static int staticVar = 20;

public static void main(String[] args) {
System.out.println(instanceVar); // Line 8
System.out.println(staticVar);
}
}

static keyword Medium
A. The code compiles and throws a NullPointerException at runtime.
B. The code compiles and prints 10 followed by 20.
C. The code fails to compile because a static variable cannot be accessed from a static method.
D. The code fails to compile because of an error at Line 8.

37 Which of the following is NOT a valid declaration for the main method that can serve as the entry point for a Java application?

Writing simple Java class and main() method Medium
A. static public void main(String[] args)
B. public static int main(String[] args)
C. public static void main(String... args)
D. public static void main(String args[])

38 What is the output of the following Java code?
java
public class Test {
public static void main(String[] args) {
int i = 257;
byte b = (byte)i;
System.out.println(b);
}
}

Type conversion Medium
A. 0
B. 257
C. Compilation Error
D. 1

39 A developer states that a key feature of Java is that it is "Robust". Which of the following Java characteristics is the primary reason for this claim?

Features of Java Medium
A. Java's ability to run the same bytecode on any platform with a compatible JVM.
B. Java's strict compile-time type checking and automatic memory management (garbage collection).
C. Java's use of a Just-In-Time (JIT) compiler to improve performance.
D. Java's extensive standard library and rich set of APIs.

40 What is the output of this code snippet which uses the ternary operator?
java
public class Ternary {
public static void main(String[] args) {
int x = 10;
int y = 20;
String result = (x > y) ? "A" : (y > 30) ? "B" : "C";
System.out.println(result);
}
}

Unary, assignment and Ternary operator Medium
A. C
B. B
C. Compilation Error
D. A

41 Analyze the following Java code snippet and predict the final value of z.

java
public class PrecedenceTest {
public static void main(String[] args) {
int x = 5; // 0101
int y = 9; // 1001
int z = x++ & --y | y >>> 2 ^ x;
}
}

Operator precedence Hard
A. 5
B. 7
C. 1
D. 11

42 What is the output of the following Java program?

java
public class Conversion {
public static void main(String[] args) {
byte b = 65;
b = (byte)(b * 2.1);
char c = (char)b;
System.out.println(c);
}
}

Type conversion Hard
A. û
B. An ArithmeticException is thrown at runtime.
C. A compilation error occurs.
D. A

43 Consider the following code snippet. What will be printed to the console?

java
public class WrapperCache {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
Long e = 127L;

System.out.println(a == b);
System.out.println(c == d);
System.out.println(a.equals(e));
System.out.println(a == e);
}
}

Wrapper class Hard
A. true
false
false
A compilation error occurs on the last line.
B. true
true
false
A compilation error occurs on the last line.
C. true
false
true
false
D. true
false
false
false

44 What is the final output of the following Java program?

java
public class StaticFlow {
static int i = initialize("i");
static {
System.out.print("Static Block ");
}
int j = initialize("j");
{
System.out.print("Instance Block ");
}

public StaticFlow() {
System.out.print("Constructor ");
}

public static int initialize(String var) {
System.out.print(var + " ");
return 1;
}

public static void main(String[] args) {
System.out.print("Main ");
new StaticFlow();
}
}

static keyword Hard
A. Main i Static Block j Instance Block Constructor
B. i j Static Block Instance Block Constructor Main
C. i Static Block Main j Instance Block Constructor
D. Static Block Main i j Instance Block Constructor

45 Examine this Java code. What will it print?

java
public class SwitchFallthrough {
public static void main(String[] args) {
final int a = 1, b = 2, c = 3;
int x = 0;
switch (a + b) {
case a:
x += a;
case b:
x += b;
case a + b:
x += c;
case a + b + c:
x += a + b;
default:
x++;
}
System.out.println(x);
}
}

Conditional Statements : Using if/else constructs and switch-case statements Hard
A. 3
B. 10
C. 6
D. 7

46 A Java program Test.java is compiled and then run with the following command:

java Test "a'b c" 'd' ''

What are the values of args.length, args[1], and args[2] inside the main method?

Command-line arguments Hard
A. args.length is 3, args[1] is "'d'", args[2] is "".
B. args.length is 3, args[1] is "d", args[2] is an empty string "".
C. A runtime ArrayIndexOutOfBoundsException because args[2] is not a valid argument.
D. args.length is 4, args[1] is 'd', args[2] is ''.

47 What are the final values of a and b after this code executes?

java
int a = 5;
int b = a++;
b = (b-- > 5) ? a++ : (a = --b + a--);

Unary, assignment and Ternary operator Hard
A. a is 11, b is 10
B. a is 10, b is 11
C. a is 10, b is 10
D. a is 11, b is 11

48 After the execution of the following code snippet, what is the final value of a?

java
int a = 4;
boolean b;
a = (b = (a-- == 4)) ? (a *= 2) : (a += 3);

Unary, assignment and Ternary operator Hard
A. 8
B. 6
C. 3
D. 7

49 Which of the following Java code snippets will fail to compile due to an invalid identifier or keyword usage?

java
// Snippet A
int _ = 5;
String $ = "hello";

// Snippet B
int assert = 10; // Assuming source level > 1.4
boolean strictfp = true;

// Snippet C
final class transient {}
int String = 1;

// Snippet D
Integer Integer = new Integer(10);
Double Double = 20.0;

Keywords, Identifiers, Variables Hard
A. Snippet B
B. Snippet C
C. Snippet A
D. Snippet D

50 Consider the following two Java files in the same directory and package. Why will this code fail to compile?

File A.java
java
public class A {
protected static int VALUE = 10;
}


File B.java
java
class B {
public static void main(String[] args) {
System.out.println(A.VALUE);
}
}
private class C {}

Java program structure Hard
A. The file B.java must contain a public class named B.
B. A file cannot contain a private top-level class like C.
C. The main method in class B must be inside a public class.
D. Class B cannot access the protected member VALUE from class A.

51 A developer creates a self-contained Java application using jlink, which produces a custom runtime image. This image includes the application's modules, necessary platform modules, and the Java Virtual Machine. A user runs this application on their machine which has no other Java installation. Which statement most accurately describes the relationship between the components in this scenario?

Understanding JDK, JRE and JVM Hard
A. The custom runtime image is just the JVM, as it's the only component needed to run bytecode.
B. The user has a JRE installed, and the custom image is just the application code that uses this system-wide JRE.
C. The user's machine is effectively running a full JDK, as jlink is a JDK tool.
D. The custom runtime image is a specialized JRE, containing a JVM, but it lacks development tools like javac and jlink.

52 What is the output of the following code snippet, considering Java's integer overflow behavior?

java
public class Overflow {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
int result = max + 1;
int min = Integer.MIN_VALUE;
System.out.println(result == min);
}
}

Data In the Cart : Using primitive data types Hard
A. true
B. It throws an ArithmeticException.
C. false
D. It does not compile.

53 Consider a class Secret in a package com.example. Which combination of modifier for the data field and access location will result in a compilation error?

java
// In package com.example
public class Secret {
/ modifier / String data = "Top Secret";
}

// In package com.example
class SamePackageAccessor {
void access() {
new Secret().data;
}
}

// In package com.another
import com.example.Secret;
class DifferentPackageAccessor {
void access() {
new Secret().data;
}
}

Access modifiers Hard
A. modifier is protected, accessed from DifferentPackageAccessor
B. modifier is public, accessed from DifferentPackageAccessor
C. modifier is private, accessed from SamePackage-Accessor
D. modifier is (default), accessed from SamePackageAccessor

54 What happens when you try to compile and run the following Java code?

java
class Main {
static public void main(String... args) {
System.out.println("Success");
}
}
public class AnotherClass {
// Empty
}

Writing simple Java class and main() method Hard
A. It compiles successfully, but running java Main throws a NoSuchMethodError because main must be in a public class.
B. Compilation fails because the main method is not in the public class AnotherClass.
C. It compiles successfully. Running java Main prints "Success", and running java AnotherClass throws a NoSuchMethodError.
D. Compilation fails because a file cannot have a public class and another non-public class.

55 The 'Write Once, Run Anywhere' (WORA) principle of Java is primarily enabled by the interplay of two specific components/concepts. Which option best describes this relationship?

History and Features of Java Hard
A. The Garbage Collector and the Just-In-Time (JIT) compiler, which optimize memory and performance on any OS.
B. The javac compiler's ability to cross-compile for different architectures and the Java Runtime Environment (JRE).
C. Strict type checking at compile-time and the Java Class Library (JCL) which provides a standard API.
D. Platform-independent bytecode and a platform-specific Java Virtual Machine (JVM).

56 What is the result of the following Java expression?

( (5.0 / 0.0) == Double.POSITIVE_INFINITY ) && ( -1.0 / 0.0 == Double.NEGATIVE_INFINITY ) && ( (0.0 / 0.0) != Double.NaN )

Operators : Working with Bit-wise, arithmetic, logical, and relational operators Hard
A. false
B. true
C. It causes a compilation error.
D. It throws an ArithmeticException at runtime.

57 What is printed by the following code?

java
public class IfElseMystery {
public static void main(String[] args) {
boolean a = true, b = false, c = true;
if (a = b || (c = false)) {
System.out.print("A");
}
if (c) {
System.out.print("B");
}
if ((a = false) || b) {
System.out.print("C");
}
System.out.print(a + " " + b + " " + c);
}
}

Conditional Statements : Using if/else constructs and switch-case statements Hard
A. AC false false false
B. A false false false
C. A false true false
D. C false false false

58 Predict the output of the following Java code snippet, paying close attention to operator precedence and short-circuiting logic.

java
public class LogicalMystery {
public static void main(String[] args) {
boolean a = false, b = false, c = false;
if ((a = true) || (b = true) && (c = true)) {
System.out.print("A");
}
System.out.print(a + " " + b + " " + c);
}
}

Conditional Statements : Using if/else constructs and switch-case statements Hard
A. true false false
B. Atrue true true
C. Atrue true false
D. Atrue false false

59 What is printed to the console when the following code is executed?

java
public class TrickyConversion {
public static void main(String[] args) {
short s = -1;
char c = (char)s;
int i = c;
System.out.println(i);
}
}

Type conversion Hard
A. 1
B. 65535
C. A compilation error occurs.
D. -1

60 Which of the following lines of code will result in a compilation error?

java
// Assume these imports are present
import java.util.ArrayList;
import java.util.List;

public class GenericsAndWrappers {
public void test() {
List<Integer> list = new ArrayList<>();
// Line 1
Short s = 10;
// Line 2
list.add(s);
// Line 3
Integer i = 20;
// Line 4
list.add(i + s);
}
}

Wrapper class Hard
A. Line 1
B. Line 2
C. Line 4
D. Line 3

61 Given int x = 3;, which of the following expressions evaluates to false?

A: x > 2 && x < 5
B: !(x < 3 || x > 3)
C: x = 2 != x
D: x++ == 3 || ++x == 3

Operator precedence Hard
A. Expression B
B. Expression C
C. Expression A
D. Expression D