MauryaHub PYQ Practice

Programming Concepts using Java · Quiz 2 · Jan 2026

Programming Concepts using Java · Quiz 2 · Jan 2026

← Course papers · Start practice / exam

Questions and published explanations below are available without starting a test. Some questions may not have a published solution yet.

15 / 15 published solutions

Question 2 MCQ · 6.0 marks

Consider the Java code given below.
Source diagram or notation
What will the output be?
6406536627042.Source diagram or notation
6406536627043.Source diagram or notation
6406536627044.Source diagram or notation
6406536627045.Source diagram or notation

Published solution

**1. Trace insertion and accumulation:** The first TEMP01 reading creates a total of 1500. HUMID02 creates 2000 and AIRQ03 creates 1200. The final TEMP01 reading uses its existing value and adds 800, giving 2300. **2. Identify the ordering rule:** LinkedHashMap preserves insertion order in the constructor mode shown. Updating the value of an existing key does not move it to a new insertion position. **3. Print the entries:** The totals therefore appear in first-seen order: TEMP01=2300, HUMID02=2000, AIRQ03=1200, with the spaces and separators produced by println. **Answer: B — Lines: TEMP01 = 2300; HUMID02 = 2000; AIRQ03 = 1200.** A: **Incorrect:** It replaces the repeated TEMP01 total with the last reading 800 instead of adding it to 1500. B: **Correct:** It correctly accumulates TEMP01 to 2300 and retains first-insertion ordering. C: **Incorrect:** This reorders the entries rather than preserving the shown LinkedHashMap insertion sequence. D: **Incorrect:** This also changes the insertion order even though updating a value does not reorder keys.

Report a correction for Q2

Question 3 MCQ · 6.0 marks

Consider the Java code given below that prints the credit points of courses. From among the options, identify the appropriate function header for the function
Source diagram or notation
that takes as input a list of courses and prints their credit points.
Source diagram or notation
Choose the correct option.
6406536627050.Source diagram or notation
6406536627051.Source diagram or notation
6406536627052.Source diagram or notation
6406536627053.Source diagram or notation

Published solution

**1. Identify the two argument types:** The caller passes List<MathCourse> and List<LinguisticsCourse>. Both element classes extend Course, so their elements support getCredits(). **2. Apply generic invariance:** List<MathCourse> is not a subtype of List<Course>, and neither typed list is a List<Object>. A parameter declared with either of those types would reject the calls. **3. Use an upper-bounded wildcard:** List<? extends Course> accepts lists of Course or its subclasses while allowing values read from the list to be treated as Course. The method can therefore iterate and call getCredits(), printing 3.0,4.0,2.5,3.5. **Answer: C — public static void printCredits(List<? extends Course> lst).** A: **Incorrect:** Java’s generic invariance prevents passing List<MathCourse> or List<LinguisticsCourse> as List<Course>. B: **Incorrect:** This accepts the linguistics list but not the maths list. C: **Correct:** The upper bound accepts both subtype lists and provides Course methods for reading their elements. D: **Incorrect:** List<Object> does not accept arbitrary lists of subtypes, and Object itself has no getCredits method.

Report a correction for Q3

Question 4 MCQ · 6.0 marks

Consider the two Java files given below.
Source diagram or notation
Source diagram or notation
Source diagram or notation
Source diagram or notation
Choose the correct option.
6406536627058.Source diagram or notation
and
Source diagram or notation
generate compilation errors.
6406536627059.
Only
Source diagram or notation
generates compilation error.
6406536627060.Source diagram or notation
and
Source diagram or notation
generate compilation errors.
6406536627061.
No compilation errors.

Published solution

**1. Check the package boundary:** Word belongs to linguistics while LinguistTest belongs to analysis. LinguistTest is not declared as a subclass of Word. **2. Check each access:** showSyllables is public, so LINE 1 may call it; that method can legally call Word’s own private getSyllables internally. getLanguage is protected and is not accessible through w from this unrelated external-package class, so LINE 2 fails. **3. Check the last line:** getText is public and LINE 3 is valid. The single access violation is therefore LINE 2. **Answer: B — Only LINE 2 generates a compilation error.** **Convention note:** The placeholder constructor comment does not declare a constructor. The displayed public Word class therefore has its implicit public no-argument constructor, consistent with new Word(). A: **Incorrect:** LINE 1 is a public call; the private helper is called inside Word, where it is accessible. B: **Correct:** Only LINE 2 directly accesses the protected method from an unrelated class in another package. C: **Incorrect:** LINE 3 calls a public method and does not create a second error. D: **Incorrect:** The protected access at LINE 2 is not legal in this context.

Report a correction for Q4

Question 5 MCQ · 6.0 marks

Consider the Java code given below about controlling robot hardware values.
Source diagram or notation
Choose the correct option.
6406536627066.Source diagram or notation
and
Source diagram or notation
generate a compilation error because method
Source diagram or notation
is already defined with the same signature.
6406536627067.Source diagram or notation
and
Source diagram or notation
generate a compilation error because method
Source diagram or notation
is already defined with the same signature.
6406536627068.
Only
Source diagram or notation
generates a compilation error because method
Source diagram or notation
is already defined with the same signature.
6406536627069.
This program generates output:
Source diagram or notation

Published solution

**1. Compare the bounded methods:** LINE 1 declares <T extends Number> process(T), and LINE 2 declares <U extends Number> process(U). Renaming a type parameter does not create a different method signature. Both erase to process(Number). **2. Compare the unbounded method:** LINE 3 declares <T> process(T), whose erased parameter type is Object. process(Object) is distinct from process(Number) and can coexist with one bounded declaration. **3. Identify the error:** The second bounded declaration duplicates the first. The class does not compile as written, so the proposed runtime output is not produced. **Answer: C — Only LINE 2 is the duplicate method declaration.** A: **Incorrect:** LINE 3 erases to Object and is distinct; the duplicate is LINE 2 versus LINE 1. B: **Incorrect:** LINE 1 and LINE 3 have different erased parameter types and are not the duplicate pair. C: **Correct:** LINE 2 duplicates the already declared Number-bounded method despite using U instead of T. D: **Incorrect:** Compilation fails before any call in main can produce output.

Report a correction for Q5

Question 6 MCQ · 6.0 marks

Consider the Java code given below.
Source diagram or notation
Identify the appropriate option to fill in place of
Source diagram or notation
such that the output is:
Source diagram or notation
6406536627090.Source diagram or notation
6406536627091.Source diagram or notation
6406536627092.Source diagram or notation
6406536627093.Source diagram or notation

Published solution

**1. Use the static element type:** SArr is declared Object[], so SArr[i] has compile-time type Object. Object has no process() method, even though the actual values implement Service. **2. Cast to their shared interface:** Both CheckIn and Boarding implement Service. Casting each element to Service makes process available and remains valid for both runtime objects. **3. Follow dispatch:** At index zero, CheckIn.process prints “Check-In Service”. At index one, Boarding.process prints “Boarding Service”. A cast to only one concrete class would fail on the other object. **Answer: D — ((Service) SArr[i]).process();.** A: **Incorrect:** process() cannot be called directly on the compile-time Object reference. B: **Incorrect:** The first element is a CheckIn, so casting it to Boarding throws ClassCastException. C: **Incorrect:** The second element is a Boarding, so casting every element to CheckIn fails there. D: **Correct:** Both objects implement Service and their overridden methods are selected at runtime.

Report a correction for Q6

Question 7 MCQ · 6.0 marks

Consider the code given below.
Source diagram or notation
Choose the correct option.
6406536627094.Source diagram or notation
generates compilation error because method
Source diagram or notation
cannot be invoked on
Source diagram or notation
.
6406536627095.
This code generates the output followed by runtime Error at
Source diagram or notation
due to ambiguity in
Source diagram or notation
.
6406536627096.
This code generates the output:
Source diagram or notation
6406536627097.
This code generates the output:
Source diagram or notation

Published solution

**1. Resolve inherited apply:** The reference has type MatrixOp and points to SparseMatrixOp. MatrixOp inherits public apply() from Operation, so ob.apply() is accessible and prints “General linear operation”. **2. Resolve compute:** MatrixOp defines compute(), and SparseMatrixOp does not override it. The next call prints “Matrix computation”. **3. Resolve overridden describe:** SparseMatrixOp overrides describe(), so dynamic dispatch selects “Sparse Matrix description”. The superclass reference does not force the superclass implementation. **Answer: C — Lines: General linear operation; Matrix computation; Sparse Matrix description.** A: **Incorrect:** MatrixOp inherits the public apply method, so LINE 1 compiles. B: **Incorrect:** A legal override is resolved by dynamic dispatch and does not create runtime ambiguity. C: **Correct:** The output follows inherited apply, inherited compute and the runtime object’s overridden describe. D: **Incorrect:** The final call selects SparseMatrixOp.describe rather than MatrixOp.describe.

Report a correction for Q7

Question 8 MCQ · 7.0 marks

Consider the Java code given below.
Source diagram or notation
Choose the correct option to be filled in place of
Source diagram or notation
so that the output is:
Source diagram or notation
6406536627046.Source diagram or notation
6406536627047.Source diagram or notation
6406536627048.Source diagram or notation
6406536627049.Source diagram or notation

Published solution

**1. Identify superclass initialization:** CelestialBody has a constructor taking a String and no zero-argument constructor. Planet must explicitly initialize that superclass portion with the provided name. **2. Choose constructor syntax:** super(n) invokes CelestialBody(String) and must appear before the body statements that initialize Planet’s own type field. It is not an ordinary method call. **3. Follow printing:** super.toString() produces the name text, and Planet.toString adds the type. Printing the CelestialBody reference dispatches to Planet’s override, yielding “Name: Kepler-452b, Type: Super-Earth”. **Answer: B — super(n);.** A: **Incorrect:** this(n) would require a one-argument Planet constructor, which is not defined. B: **Correct:** super(n) invokes the existing parent constructor and initializes its private name field. C: **Incorrect:** A constructor is not invoked as an ordinary CelestialBody(n) method. D: **Incorrect:** super.CelestialBody(n) is not Java’s superclass-constructor invocation syntax.

Report a correction for Q8

Question 9 MCQ · 7.0 marks

The
Source diagram or notation
function is defined as: If the specified key is not already associated with a value or is associated with
Source diagram or notation
, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is
Source diagram or notation
. Consider the Java code given below.
Source diagram or notation
Choose the correct option.
6406536627054.Source diagram or notation
generates a compilation error due to duplicate keys.
6406536627055.
The program generates the output:
Source diagram or notation
6406536627056.
The program generates the output:
Source diagram or notation
6406536627057.
The program generates the output:
Source diagram or notation

Published solution

**1. Copy the first map:** The initial merged map contains Andromeda=88, Betelgeuse=76 and Cygnus=92. put assigns values by key and does not create a duplicate-key compilation error. **2. Apply merge to shared keys:** Betelgeuse uses min(76,80)=76. Cygnus uses min(92,85)=85. Draco was absent, so merge inserts its supplied value 70 directly. **3. Read TreeMap order:** Natural string-key order yields {Andromeda=88, Betelgeuse=76, Cygnus=85, Draco=70}. The operation chooses minima, not sums or maxima. **Answer: B — {Andromeda=88, Betelgeuse=76, Cygnus=85, Draco=70}.** A: **Incorrect:** Map key repetition is handled at runtime by replacement/merge rules; it is not a compilation error. B: **Correct:** It retains or inserts each correct minimum and prints TreeMap’s sorted key order. C: **Incorrect:** It uses larger values for the shared keys rather than the Math.min remapping. D: **Incorrect:** It sums shared values, while the actual remapping function selects the smaller value.

Report a correction for Q9

Question 10 MCQ · 7.0 marks

Consider the following Java code.
Source diagram or notation
Choose the correct option when the class is executed as:
Source diagram or notation
6406536627062.
This code generates output:
Source diagram or notation
6406536627063.
This code generates AssertionError:
Source diagram or notation
6406536627064.
This code generates AssertionError:
Source diagram or notation
6406536627065.
This code generates AssertionError:
Source diagram or notation

Published solution

**1. Enable assertion evaluation:** The execution command includes -ea, so assertions are checked rather than skipped. The constructor receives aperture 150, which passes the positive-aperture assertion. **2. Check the adjustment preconditions:** adjustZoom(300) passes amount>0. But currentZoom+amount is 800+300=1100, greater than maxZoom=1000. **3. Follow failure control flow:** The second method assertion throws AssertionError with message “Zoom exceeds telescope limit” before the assignment and return. Therefore println never receives 1100.0 to print. **Answer: C — AssertionError: Zoom exceeds telescope limit.** A: **Incorrect:** 1100.0 would be reached if these assertions were disabled, but -ea enables the failing limit check. B: **Incorrect:** The increment 300 is positive, so that assertion passes. C: **Correct:** The proposed zoom 1100 exceeds the 1000 limit, causing this AssertionError. D: **Incorrect:** The aperture is 150, so its positivity assertion passes.

Report a correction for Q10

Question 11 MCQ · 7.0 marks

Consider the following Java code.
Source diagram or notation
What will the output be?
6406536627070.Source diagram or notation
Source diagram or notation
6406536627071.Source diagram or notation
6406536627072.Source diagram or notation
6406536627073.Source diagram or notation
Source diagram or notation

Published solution

**1. Apply the length filter:** premchand has nine characters and narayan has seven, so both pass length>6. tagore has six, desai five and bond four, so they are removed. **2. Transform and skip:** map changes the two retained strings to PREMCHAND and NARAYAN. skip(1) removes the first retained stream element, PREMCHAND. **3. Execute the terminal operation:** forEach prints the sole remaining element NARAYAN. No operation reorders this sequential list stream. **Answer: B — NARAYAN.** A: **Incorrect:** PREMCHAND is removed by skip(1), so both names are not printed. B: **Correct:** After filtering and uppercasing, skipping the first element leaves NARAYAN alone. C: **Incorrect:** The surviving second element is NARAYAN, not PREMCHAND. D: **Incorrect:** TAGORE and DESAI fail the length>6 filter before the skip stage.

Report a correction for Q11

Question 12 MCQ · 7.0 marks

Consider the Java code given below.
Source diagram or notation
Choose the correct option to fill in place of
Source diagram or notation
so that the output is:
Source diagram or notation
Please note the following methods from types
Source diagram or notation
and
Source diagram or notation
:
Source diagram or notation
: Removes the object at the top of this stack and returns it.
Source diagram or notation
: Retrieves and removes the head of this deque, or returns null if empty.
Source diagram or notation
: Retrieves, but does not remove, the head of this deque, or returns null if empty.
6406536627074.Source diagram or notation
6406536627075.Source diagram or notation
6406536627076.Source diagram or notation
6406536627077.Source diagram or notation

Published solution

**1. Compare opposite traversal directions:** Pushing characters to a Stack makes pop read them backward. Adding them to a Deque makes poll read and remove them forward. A palindrome must have equal characters at every paired comparison. **2. Use the rejection condition:** The code must return false when !s.pop().equals(d.poll()) is true. poll advances the front pointer on each comparison, unlike peek. **3. Dry-run the examples:** LRRL yields comparisons L/L,R/R,R/R,L/L, all equal, so true is returned. LRLL compares L/L first, then L/R, so it returns false at the mismatch. **Answer: C — while (!s.isEmpty()) { if (!s.pop().equals(d.poll())) return false; }.** A: **Incorrect:** It rejects equality and repeatedly peeks at the same front character, violating the intended test. B: **Incorrect:** It returns false on equal characters rather than on a mismatch. C: **Correct:** It removes both compared characters and rejects only unequal pairs, implementing the palindrome test. D: **Incorrect:** peek never advances the deque, so it repeatedly compares against the original first character.

Report a correction for Q12

Question 13 MCQ · 7.0 marks

Consider the code given below about an aircraft maintenance record.
Source diagram or notation
What will the output be?
6406536627078.Source diagram or notation
6406536627079.Source diagram or notation
6406536627080.Source diagram or notation
6406536627081.Source diagram or notation

Published solution

**1. Map aliases and the clone:** a3 is the same Aircraft object as a1. a2 is a different Aircraft object made by the default shallow clone, but its flightHours field refers to the same underlying int array as a1’s. **2. Modify the shared array:** a2.flightHours[1]=90 changes the shared array element, so a1,a2 and a3 all observe 90 at index one. **3. Reassign the name field:** a3.airline="IndiGo" changes a1’s field because a3 aliases a1. a2 retains its own airline reference “Air India”; assigning a new String field does not mutate the old string. **4. Read all lines:** The output is “IndiGo 90”, “Air India 90”, “IndiGo 90”. **Answer: A — Lines: IndiGo 90; Air India 90; IndiGo 90.** A: **Correct:** It correctly combines the shallowly shared array with the separate cloned object’s airline field. B: **Incorrect:** The shared array means no involved object retains the old index-one value 60. C: **Incorrect:** a3 aliases a1, so a3.airline reassignment changes a1’s airline to IndiGo. D: **Incorrect:** Both the aliased name and shared array have changed, so these old values cannot remain.

Report a correction for Q13

Question 14 MCQ · 7.0 marks

Consider the following Java code that uses chained exceptions in the context of a sports match analysis system.
Source diagram or notation
What can the output of this code be?
6406536627086.Source diagram or notation
6406536627087.Source diagram or notation
6406536627088.Source diagram or notation
6406536627089.Source diagram or notation

Published solution

**1. Process the valid team:** analyzeMatch("Team Tigers") takes the nonempty branch and prints “Analyzing performance for Team Tigers”. **2. Wrap the failing call:** The empty string triggers MatchDataException("Corrupted match data"). The method catches it, creates PerformanceAnalysisException("Analysis failed"), records the first exception with initCause(e), and throws the wrapper. **3. Read the outer catch:** e.getMessage() returns the wrapper message “Analysis failed”. e.getCause().getMessage() returns “Corrupted match data”, so the final line starts “Reason: ” followed by that original message. **Answer: A — Lines: Analyzing performance for Team Tigers; Analysis failed; Reason: Corrupted match data.** A: **Correct:** It includes the first successful analysis and then prints the wrapper message and its stored cause. B: **Incorrect:** It swaps the outer message and cause and omits the first successful output. C: **Incorrect:** It omits both the initial success line and the wrapper’s message. D: **Incorrect:** It has the two exception-related lines but omits the first successful analysis line.

Report a correction for Q14

Question 15 MSQ · 8.0 marks

Consider the Java code given below that should print the names of individuals whose BMI is between 18 and 25 (both inclusive).
Source diagram or notation
Choose the correct option(s) to fill in place of
Source diagram or notation
to obtain the right answer.
6406536627082.Source diagram or notation
6406536627083.Source diagram or notation
6406536627084.Source diagram or notation
6406536627085.Source diagram or notation

Published solution

**1. Identify qualifying people:** The inclusive predicate is bmi>=18 && bmi<=25. It keeps Arjun(22.5), Meera(18.0) and Sara(24.8), while excluding Rahul(27.3), Imran(30.2) and David(17.5). **2. Preserve Person objects:** filter keeps matching Person elements for the later p.name access. map with a Boolean predicate instead transforms each Person into a Boolean, which has no name field. **3. Compare valid filtering forms:** One filter containing both conditions and two successive filters each containing one condition produce the same intersection. Both retain the original encounter order and print Arjun,Meera,Sara. **Answer: C, D — C and D — both filter-based implementations.** A: **Incorrect:** map produces Boolean elements, so the following p.name access does not compile. B: **Incorrect:** Its second stage also maps each retained Person to Boolean, invalidating p.name. C: **Correct:** The single filter expresses the correct inclusive interval and retains Person elements. D: **Correct:** Successive lower-bound and upper-bound filters implement the same inclusive interval.

Report a correction for Q15

Question 16 MSQ · 7.0 marks

Consider the code given below.
Source diagram or notation
maps each element of
Source diagram or notation
to its frequency of occurrence in list.
Source diagram or notation
Identify the appropriate option(s) to fill in the blank at
Source diagram or notation
, such that the output is:
Source diagram or notation
6406536627098.Source diagram or notation
6406536627099.Source diagram or notation
6406536627100.Source diagram or notation
6406536627101.Source diagram or notation

Published solution

**1. Trace safe counting:** With an initially empty map and input [1,1,2,3], each absent key must first receive count one. The second occurrence of one then increments its count to two. **2. Check C and D:** getOrDefault(i,0)+1 handles both first and later occurrences. The containsKey branch in D explicitly performs the same initialization-or-increment logic. Both give mappings 1→2,2→1,3→1. **3. Reject the other operations:** A passes an integer expression where merge needs a remapping function. B calls m.get(i)+1 on an absent key, causing null unboxing and NullPointerException. **4. Interpret printed order:** The mappings are correct, but HashMap does not specify iteration order. The sample order 1,2,3 is common for these small integer keys in the runtime used, not a contractual ordering guarantee. **Answer: C, D — C and D — correct frequency maps.** **Convention note:** These options compute the required counts. Exact HashMap iteration order is unspecified; sorted output would require TreeMap or explicit key sorting, and first-insertion order would require LinkedHashMap. A: **Incorrect:** merge’s third argument must be a compatible remapping function, not the integer m.get(i)+1; the snippet does not compile. B: **Incorrect:** The first get on a missing key returns null, whose unboxing for +1 throws NullPointerException. C: **Correct:** getOrDefault supplies zero before the first occurrence and increments existing counts afterward. D: **Correct:** containsKey distinguishes initialization from incrementing and produces the correct frequency map.

Report a correction for Q16