0% found this document useful (0 votes)
6 views3 pages

Tricky_Java_Array_Questions

The document contains tricky multiple-choice questions and puzzles related to Java arrays. It includes questions about array initialization, null references, and output results, along with explanations for each answer. Additionally, it presents puzzles for finding missing numbers, detecting duplicates, and calculating maximum products of integers.

Uploaded by

noneedd78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Tricky_Java_Array_Questions

The document contains tricky multiple-choice questions and puzzles related to Java arrays. It includes questions about array initialization, null references, and output results, along with explanations for each answer. Additionally, it presents puzzles for finding missing numbers, detecting duplicates, and calculating maximum products of integers.

Uploaded by

noneedd78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tricky Java Array MCQs and Puzzles

Section 1: Tricky Multiple-Choice Questions

Q1: What is the output?

int[] arr = new int[5];

System.out.println(arr[0]);

A) 0

B) Garbage value

C) Compilation error

D) Runtime error

Answer: A

Explanation: Arrays of int are initialized to 0.

Q2: What will happen?

int[] a = null;

System.out.println(a.length);

A) 0

B) NullPointerException

C) Compilation error

D) 1

Answer: B

Explanation: Accessing any property on null throws NPE.

Q3: Choose correct statement:

int[] a = new int[]{1, 2, 3};

A) Illegal

B) Needs size

C) Correct syntax

D) Use new int[3]{1,2,3}

Answer: C

Explanation: Valid initialization.


Tricky Java Array MCQs and Puzzles

Q4: What is the output?

int[] a = {1,2,3};

int[] b = a;

b[0] = 99;

System.out.println(a[0]);

A) 1

B) 99

C) Compilation error

D) Garbage

Answer: B

Explanation: Arrays are reference types.

Q5: What is the output?

int[][] arr = new int[2][];

System.out.println(arr[0][0]);

Answer: D) NullPointerException

Explanation: arr[0] is null.

Section 2: Array Puzzles with Solutions

Puzzle 1: Missing Number in 1 to n Range

Given array of size n-1 with numbers 1 to n, find missing.

Approach: total = n*(n+1)/2 - sum(arr). O(n) time.

Puzzle 2: Duplicate Number Without Modifying Array

Use Floyd's Cycle Detection to find duplicate.

Time: O(n), Space: O(1).

Puzzle 3: Maximum Product of Two Integers

Find top two max and two min values.


Tricky Java Array MCQs and Puzzles

Return max(min1*min2, max1*max2).

You might also like