0

programming languages Online Quiz - 173

Description: programming languages Online Quiz - 173
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

try{ File f = new File("a.txt"); }catch(Exception e){ }catch(IOException io){ } Is this code create new file name a.txt ?

  1. True

  2. False

  3. Compile error

  4. none


Correct Option: C

AI Explanation

To answer this question, let's go through the code snippet and analyze it step by step:

try {
    File f = new File("a.txt");
} catch (Exception e) {
} catch (IOException io) {
}

In this code, a try block is used to enclose the code that may throw an exception. If an exception occurs within the try block, it will be caught by one of the catch blocks that follow.

However, there is a compile error in this code. The reason is that the catch blocks are not in the correct order. In Java, when catching exceptions, the order of the catch blocks matters. More specific exception types should be caught before more general exception types.

In this case, the catch block for IOException should come before the catch block for Exception. This is because IOException is a subclass of Exception. Therefore, the correct ordering should be:

try {
    File f = new File("a.txt");
} catch (IOException io) {
} catch (Exception e) {
}

With the correct ordering, the code would compile without errors. However, the code snippet itself does not create a new file named "a.txt". It simply creates a File object representing the file "a.txt" but does not perform any file creation operations.

Therefore, the correct answer is C) Compile error.

What is the output for the below code ? public class A { int k; boolean istrue; static int p; public void printValue() { System.out.print(k); System.out.print(istrue); System.out.print(p); } } public class Test{ public static void main(String argv[]){ A a = new A(); a.printValue();

  1. 0 false 0

  2. 0 true 0

  3. 0 0 0

  4. Compile error - static variable must be initialized before use.


Correct Option: A
Explanation:

To solve this question, the user needs to know about Java classes, instance variables, static variables, and method invocation.

The given program has two classes: A and Test. The A class defines three instance variables: k (of type int), istrue (of type boolean), and a static variable p (of type int). Class A also defines a method named printValue() that prints the values of k, istrue, and p, in that order.

In the main() method of the Test class, an object of class A is created and its printValue() method is called.

Since int and boolean instance variables are initialized to their default values of 0 and false respectively, and the static variable p is also initialized to 0, the output of the program will be:

A. 0 false 0

Option A is correct because the program will print the values of k (0), istrue (false), and p (0) in that order.

Option B is incorrect because istrue is initialized to false, not true.

Option C is incorrect because k and istrue are both non-static instance variables, and they will be initialized to their default values of 0 and false, respectively. The static variable p will also be initialized to 0.

Option D is incorrect because there is no compile error in the code.

The Answer is: A. 0 false 0

What is the output for the below code ? 1. public class Test { 2. public static void main(String... args) { 3. int [] index = new int[5]; 4. System.out.println(index instanceof Object); 5. } 6. }

  1. Compilation fails with an error at line 4

  2. Compilation fails with an error at line 3

  3. true

  4. false


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Compilation fails with an error at line 4 - This option is incorrect. The code at line 4 is valid and will not cause a compilation error.

Option B) Compilation fails with an error at line 3 - This option is incorrect. The code at line 3 is valid and will not cause a compilation error.

Option C) true - This option is correct. The code at line 4 checks whether the variable "index" is an instance of the Object class. Since arrays in Java are objects, the expression "index instanceof Object" evaluates to true.

Option D) false - This option is incorrect. As mentioned earlier, arrays in Java are objects, so the expression "index instanceof Object" evaluates to true, not false.

The correct answer is C) true. This option is correct because the code will output "true" to the console.

If we do ArrayList lst = new ArrayList(); What is the initial capacity of the ArrayList lst ?

  1. 8

  2. 10

  3. none

  4. 0


Correct Option: B

HashMap can be synchronized by _______ ?

  1. Map m = Collection.synchronizeMap(hashMap);

  2. Map m = Collections.synchronizeMap(hashMap);

  3. Map m = hashMap.synchronizeMap();

  4. none


Correct Option: B

What is the output for the below code ? public class A { public void printValue(){ System.out.println("A"); } } public class B extends A { public void printValue(){ System.out.println("B"); } } 1. public class Test { 2. public static void main(String... args) { 3. A b = new B(); 4. newValue(b); 5. } 6. public static void newValue(A a){ 7. if(a instanceof B){ 8. ((B)a).printValue(); 9. } 10. } 11.}

  1. A

  2. Compilation fails with an error at line 4

  3. B

  4. Compilation fails with an error at line 8


Correct Option: C

AI Explanation

To answer this question, let's go through each line of code to understand what it does:

  1. The Test class is defined.
  2. The main method is defined with the parameter "args" of type String array.
  3. An object "b" is created of type A, but it refers to an instance of class B. This is possible because class B is a subclass of class A.
  4. The "newValue" method is called with the object "b" as an argument.
  5. The main method ends.
  6. The "newValue" method is defined with a parameter "a" of type A.
  7. The "instanceof" operator is used to check if the object "a" is an instance of class B.
  8. If the condition in line 7 is true, the printValue method is called on the object "a" after casting it to type B.
  9. The method ends.
  10. The Test class ends.

Since the object "b" is an instance of class B, the condition in line 7 is true. Therefore, the printValue method of class B is called in line 8, which prints "B" to the console. Hence, the output of the code is:

C) B

What is the output for the below code ? 1. public class Test {2. int i=8; 3. int j=9; 4. public static void main(String[] args){ 5. add(); 6. } 7. public static void add(){ 8. int k = i+j; 9. System.out.println(k); 10. } 11. }

  1. Compilation fails with an error at line 5

  2. Compilation fails with an error at line 8

  3. 17

  4. 0


Correct Option: B

What does it print? public class CleverSwap { public static void main(String[] args) { int x = 1984; int y = 2001; x ^= y ^= x ^= y; System.out.println("x = " + x + "; y = " + y); } }

  1. x = 0; y = 1984

  2. x = 2001; y = 1984

  3. x = 2001; y = 0

  4. x = 1984; y = 2001

  5. x = 0; y = 0

  6. Error


Correct Option: A

What does it print ? /** * Generated by the IBM IDL-to-Java compiler, version 1.0 * from F:\TestRoot\apps\a1\units\include\PolicyHome.idl * Wednesday, June 17, 1998 6:44:40 o'clock AM GMT+00:00 */ public class Test { public static void main(String[] args) { System.out.print("Hell"); System.out.println("o world"); } }

  1. Hello world

  2. Won't even Compile

  3. Run time error

  4. None of these


Correct Option: B

What does this program do ? public class BrowserTest { public static void main(String[] args) { System.out.print("iexplore:"); http://www.google.com; System.out.println(":maximize"); } }

  1. Compilation Error

  2. iexplore::maximize

  3. Runtime Error

  4. None of these


Correct Option: B

What does it print ? import java.util.*; public class NameGame { public static void main(String args[]) { Map m = new IdentityHashMap(); m.put("Mickey", "Mouse"); m.put("Mickey", "Mantle"); System.out.println(m.size()); } }

  1. Exception

  2. Stack Overflow

  3. 2

  4. 1


Correct Option: D
  1. EXE is created for the called program and DLL is created for the calling program

  2. EXE is created for the calling program and DLL is created for the called program

  3. EXE is created for the both, called program and calling program

  4. DLL is created for the both, called program and calling program


Correct Option: B

Following are the main core modules

  1. HC9999M1WC3

  2. HC9999M1

  3. HC9999M2

  4. HC9999R0

  5. HC9999CD

  6. All of the above


Correct Option: F

For a M0 program , main input can be

  1. A File with .txt extension

  2. A File with .dat extension

  3. A File with .xml extension

  4. Tables in the SQL database

  5. All of these

  6. None of these


Correct Option: E

Main Output of a M0 program will be

  1. An Index File

  2. A Sequential File

  3. It creates tables in the database

  4. None of these


Correct Option: B

Main tasks of the M1 processing are

  1. Application of WC3 components

  2. Householding based Sorting

  3. Report Generation

  4. PDF generation


Correct Option: A,B

Main output of M1 program is

  1. CON File

  2. Invalid File

  3. Member-File

  4. Thincard feed

  5. K1DBloader File


Correct Option: C
- Hide questions