Assignment 12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur

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?

i. float[] = new float(3);


ii. float f1[] = new float[];
iii. float[] f2 = new float[3];
iv. float f3[] = new float[3];
v. float f4[]= { 1.0f, 2.0f, 2.0f };
vi. float f5[] = new float[] { 1.0f, 2.0f, 3.0f};

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:

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

public class NPTEL {


public static void aMethod() throws Exception {
try {
throw new Exception();
} finally {
System.out.print("finally ");
}
}

public static void main(String args[]) {


try {
aMethod();
} catch (Exception e) {
System.out.print("exception ");
}
System.out.print("finished ");
}
}

a. “finally”

b. “excep�on finished”

c. “finally excep�on finished”

d. Compila�on fails

Correct Answer:

c. “finally excep�on finished”

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:

What is the output of the following program?

public class NPTEL {


public static void main(String[] args) {
String str = "Programming with java - nptel.";
System.out.println(str.charAt(13+4) + str.substring(13+5, 13+8));
}
}

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 NPTEL extends Thread {


public void run() {
System.out.println("I am from Run…");
}
}

class MyProgram {
public static void main(String[] args) {
NPTEL t = new NPTEL();
t.start();
}
}

a. One thread only.

b. Two threads only.

c. Three threads only.

d. No thread will run in this case.

Correct Answer:

b. Two threads only.

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:

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

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?

public class NPTEL {


public static void main(String[] args) {
int[] numbers = new int[5];
// #1 : Missing code block
System.out.println("First element: " + numbers[0]);
}
}

a. numbers = {1, 2, 3, 4, 5};

b. for (int i = 1; i < numbers.length; i++) {


numbers[i] = i;
}

c. numbers[] = {1, 2, 3, 4, 5};

d. numbers = new int[]{1, 2, 3, 4, 5};

Correct Answer:

d. numbers = new int[]{1, 2, 3, 4, 5};

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?

public class StringExample {


public static void main(String[] args) {
String str = "Hello, World!";
//#1 Missing code block
}
}

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:

System.out.println(str.substring(7, 12)); is the correct answer because the


substring() method extracts a substring from the str string star�ng from index 7 (inclusive) to
index 12 (exclusive), which results in the substring "World".
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 9:

What will be the output of this program?

public class NPTEL {


public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.print((str1 == str2) + " ");
System.out.print(str1 == str3);
}
}

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:

What will be the output of this program?

public class NPTEL {


public static void main(String[] args) {
try {
int num = 10 / 0;
System.out.println(num);
} catch (ArithmeticException e) {
System.out.println("Arithmetic exception occurred");
} finally {
System.out.println("Finally block executed");
}
}
}

a. Compila�on ERROR

b. “Finally block executed”

c. “Arithme�c excep�on occurred


Finally block executed”

d. Run�me ERROR

Correct Answer:

c. “Arithme�c excep�on occurred


Finally block executed”

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.

You might also like