Programming In Java Week 12 NPTEL Assignment Answers 2025

NPTEL Programming In Java Week 12 Assignment Answers 2024

1. Which of the following statements are correct and would NOT cause a compilation error?

a. iii, iv, v, vi
b. i, ii, iii, iv
c. ii, iii, v, vi
d. i, ii, iv, vi

Answer: a
Explanation: Only statements iii, iv, v, and vi are syntactically correct and won’t cause a compilation error. The others contain issues like incorrect syntax or logic that would be caught during compilation.


2. What is the result, if the following program is executed?

a. “finally”
b. “exception finished”
c. “finally exception finished”
d. Compilation fails

Answer: c
Explanation: In Java, even if an exception occurs, the finally block always executes. So, the program prints both "finally" and "exception finished".


3. What is the output of the following program?

a. java
b. ava
c. java – nptel
d. with

Answer: a
Explanation: The program prints "java" because the substring is taken starting from index 0 to 4 (exclusive), which results in "java".


4. If you run this program, then how many threads will be executed altogether?

a. One thread only.
b. Two threads only.
c. Three threads only.
d. No thread will run in this case.

Answer: b
Explanation: The main thread and one child thread (created via Thread) will run. So, two threads will execute in total.


5. Which of the following method is used to set a frame, say f with size 300 × 200 pixels?

a. f.setSize(300, 200);
b. f.setSize(200, 300);
c. f.paint(300, 200);
d. f.setVisible(300, 200);

Answer: a
Explanation: setSize(width, height) is the correct method used in Java to set the size of a frame. So, f.setSize(300, 200); is correct.


6. Which of the following control expressions are valid for an if statement?
    i. Any integer expression.
    ii. Any Boolean expression.
    iii. A String object.
    iv. Any expression with mixed arithmetic.

a. only ii
b. ii, iii
c. ii, iv
d. i, ii

Answer: a
Explanation: In Java, the condition in an if statement must be a boolean expression. Hence, only option ii is valid.


7. Which of the following options correctly initializes the elements of the numbers array with values 1, 2, 3, 4, and 5?

a. int numbers[] = new int[5]{1,2,3,4,5};
b. int[] numbers = {1, 2, 3, 4, 5};
c. int numbers = {1,2,3,4,5};
d. int numbers[] = (1,2,3,4,5);

Answer: b
Explanation: Option b is the correct Java syntax to declare and initialize an array with values.


8. Which of the following options correctly extracts and prints the word “World” from the str string?

a. str.substring(6, 11);
b. str.substring(5, 11);
c. str.substring(6, 10);
d. str.substring(5, 10);

Answer: a
Explanation: In Java, substring(start, end) extracts characters from index start to end-1. "World" starts at index 6 and ends at index 10, so option a is correct.


9. What will be the output of this program?

a. true false
b. false true
c. true true
d. false false

Answer: a
Explanation: The program checks two different conditions. The first condition evaluates to true and the second to false, hence the output is true false.


10. What will be the output of this program?

a. Compilation ERROR
b. “Finally block executed”
c. “Arithmetic exception occurred Finally block executed”
d. Runtime ERROR

Answer: c
Explanation: An arithmetic exception is caught and the message is printed. After that, the finally block is also executed, so both messages are printed.