Assignment 12
Assignment 12
Assignment 12
PROGRAMMING IN JAVA
Assignment 12
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 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
Correct Answer:
a. iii, iv, v, vi
Detailed Solu�on:
Op�on iii, iv, v and vi are syntac�cally correct for declara�on of an array.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
a. “finally”
b. “excep�on finished”
d. Compila�on fails
Correct Answer:
Detailed Solu�on:
The program is syntac�cally correct and here for two try blocks, there is one catch block.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
a. java
b. ava
c. java - nptel
d. with
Correct Answer:
a. java
Detailed Solu�on:
str.charAt(17) returns ‘j’ and str.substring(18,21) returns ‘ava’ and they are printed
together.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
If you run this program the how many threads will be executed altogether?
class MyProgram {
public static void main(String[] args) {
NPTEL t = new NPTEL();
t.start();
}
}
Correct Answer:
Detailed Solu�on:
Here, two thread objects will be in execu�on: One is the thread due to the execu�on of the main()
method and other is the run() of the object t.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
Which of the following method is used to set a frame, say f with size 300 × 200 pixels?
JFrame f = newJFrame();
a. f.setSize(300, 200);
b. f.setSize(200, 300);
c. f.paint(300, 200);
d. f.setVisible(300, 200);
Correct Answer:
a. f.setSize(300, 200);
Detailed Solu�on:
The setSize(300,200) method is used to do the job. Other are either syntac�cally not valid or not
appropriate.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
a. only ii
b. ii, iii
c. ii,iv
d. i, ii
Correct Answer:
a. only ii
Detailed Solu�on:
Any expression with Boolean or integer variables are valid. The condi�on will evaluate to zero (false) or
no-zero (true) values. Other op�ons are not valid.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
Which of the following options correctly initializes the elements of the numbers array with values 1, 2,
3, 4, and 5?
Correct Answer:
Detailed Solu�on:
numbers = new int[]{1, 2, 3, 4, 5}; is the correct answer because it ini�alizes the
numbers array with values 1, 2, 3, 4, and 5 using array ini�alizer syntax.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Which of the following options correctly extracts and prints the word "World" from the str string?
a. System.out.println(str.substring(7, 12));
b. System.out.println(str.subString(7, 12));
c. System.out.println(str.extract(7, 12));
d. System.out.println(str.substr(7, 13));
Correct Answer:
a. System.out.println(str.substring(7, 12));
Detailed Solu�on:
QUESTION 9:
a. true false
b. false true
c. true true
d. false false
Correct Answer:
a. true false
Detailed Solu�on:
str1 and str2 are string literals and will be interned to the same memory loca�on, so str1 ==
str2 will be true. However, str3 is created using the new keyword, so it will be stored in a different
memory loca�on, leading str1 == str3 to be false.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
a. Compila�on ERROR
d. Run�me ERROR
Correct Answer:
Detailed Solu�on:
The division by zero will throw an ArithmeticException, which will be caught in the catch block.
Then, the finally block will be executed.