0

programming languages Online Quiz - 113

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

If we extends class A in Class B they are sharing which relationship?

  1. Class A 'is-a' Class B

  2. Class B 'is-a' Class A

  3. Class A 'has-a' Class B

  4. Class B 'has-a' Class A


Correct Option: B

Match the mobile development platforms with programming languages a. Android i. Java b. Blackberry ii.C c. iPhone iii. Java (RIM APIs) d. BREW iv. Objective C

  1. a-i,b-iii,c-iv,d-ii

  2. a-ii,b-i,c-iv,d-iii

  3. a-iii,b-i,c-ii,d-iv

  4. a-i,b-iv-c-ii,d-iii


Correct Option: A

Match the mobile development platforms with programming languages a. Android i. Java b. Blackberry ii.C c. iPhone iii. Java (RIM APIs) d. BREW iv. Objective C

  1. a-i,b-iii,c-iv,d-ii

  2. a-ii,b-i,c-iv,d-iii

  3. a-iii,b-i,c-ii,d-iv

  4. a-i,b-iv-c-ii,d-iii


Correct Option: A

Given the following code: public class Demo { public int method(int x) { int r = 1; r += x; if ((x > 4) && (x < 10)) { r += 2 * x; } else (x <= 4) { r += 3 * x; } else { r += 4 * x; } r += 5 * x; return r; } public static void main(String [] args) { Demo ob = new Demo(); System.out.println("OF(11) is: " + ob.method (11)); } } What is the result?

  1. OF(11) is: 45

  2. OF(11) is: 56

  3. Compilation fails.

  4. Runtime Exception


Correct Option: C

AI Explanation

To determine the result of the given code, let's analyze the code step by step.

The code defines a class named "Demo" with a method named "method". The method takes an integer parameter "x" and returns an integer.

Inside the "method" method, there is a variable "r" initialized to 1. The variable "r" is then incremented by the value of "x" using the "+=" operator.

Next, there is an if-else statement. The condition is (x &gt; 4) &amp;&amp; (x &lt; 10). If the condition is true, the code inside the if block is executed, otherwise, the code inside the else block is executed.

Inside the if block, the variable "r" is incremented by 2 times the value of "x" using the "+=" operator.

Inside the else block, there is another else statement. This is a syntax error because an else statement should not have a condition. Therefore, the code will fail to compile.

Since the code fails to compile, the correct answer is C) Compilation fails.

import java.io.*; class Master { String doFileWork() throws FileNotFoundException { return "a"; } } class Slave extends Master { public static void main(String[] args) { String s = null; try { s = new Slave().doFileStuff(); } catch ( Exception x) { s = "b"; } System.out.println(s); } //insert a codeline } Choose the correct answers :

  1. String doFileWork() { return "b"; }

  2. String doFileWork() throws IOException ( return "b"; }

  3. String doFileWork(int x) throws IOException { return "b"; }

  4. String doFileWork() throws FileNotFoundException { return "b"; }

  5. String doFileWork() throws NumberFormatException { return "b"; }

  6. String doFileWork() throws NumberFormatException, FileNotFoundException { return "b"; }


Correct Option: A,D,E,F

class Demo { public static void main(String[] args) { String s = "- " ; try { doMath(args[0]); s += "t "; // line 6 } finally { System.out.println(s += "f "); } } public static void doMath(String a) { int y = 7 / Integer.parseInt(a); } } At commandline if the input is java Demo What will be output?

  1. -f and java.ArrayIndexOutOfBoundsException

  2. -f

  3. ArrayIndexOutOfBoundsException

  4. None of these.


Correct Option: A

class Demo { public static void main(String [] args) { int x = 0; // insert code here do { } while (x++ < y); System.out.println(x); } } Which, inserted at line 4, produces the output 12?

  1. int y = x;

  2. int y = 10;

  3. int y = 11;

  4. None of the above will allow compilation to succeed.

  5. int y = 12;

  6. int y = 13;


Correct Option: C

class Demo { static String s = "-"; public static void main(String[] args) { new Demo().method1() ; System.out.println(s); } void method1() { try { method2();} catch (Exception e) { s += "c"; } } void method2() throws Exception { method3(); s += "2"; method3(); s += "2b"; } void method3() throws Exception { throw new Exception(); } } What is the result?

  1. -

  2. -c

  3. -c2

  4. -2c

  5. -c22b

  6. Compilation fails.


Correct Option: B

class Ping extends Utils { public static void main(String [] args) { Utils u = new Ping(); System.out.print(u.getInt(args[0])); } int getInt(String arg) { return Integer.parseInt(arg); } } class Utils { int getInt(String x) throws Exception { return 7; } } And the following three possible changes: L1. Declare that main() throws an Exception. L2. Declare that Ping.getInt() throws an Exception. L3. Wrap the invocation of getInt() in a try / catch block. Which change(s) allow the code to compile? (Choose all that apply.)

  1. Just L1 is sufficient.

  2. Just L2 is sufficient.

  3. Just L3 is sufficient.

  4. Both L1 and L2 are required.

  5. Both L1 and L3 are required.

  6. Both L3 and L2 are required.


Correct Option: A,C

AI Explanation

To determine which changes allow the code to compile, let's go through each option:

Option A) Just L1 is sufficient. If we declare that main() throws an Exception, it means that the main() method will handle any exceptions thrown by the code within it. Since getInt() in the Utils class already throws an Exception, this change allows the code to compile.

Option B) Just L2 is sufficient. If we declare that Ping.getInt() throws an Exception, it means that any code calling getInt() must handle any exceptions thrown by it. However, the main() method in the Ping class does not handle exceptions, so this change would cause a compilation error.

Option C) Just L3 is sufficient. If we wrap the invocation of getInt() in a try/catch block, it means that we are handling any exceptions thrown by getInt() within the catch block. This change allows the code to compile because it ensures that exceptions thrown by getInt() are properly handled.

Option D) Both L1 and L2 are required. As explained in option B, declaring that Ping.getInt() throws an Exception would cause a compilation error since the main() method does not handle exceptions. Therefore, this option is not correct.

Option E) Both L1 and L3 are required. As explained in option A, declaring that main() throws an Exception allows the code to compile. Additionally, wrapping the invocation of getInt() in a try/catch block ensures that exceptions thrown by getInt() are handled. Therefore, this option is correct.

Option F) Both L3 and L2 are required. As explained in option B, declaring that Ping.getInt() throws an Exception would cause a compilation error. Therefore, this option is not correct.

The correct answer is A) Just L1 is sufficient and C) Just L3 is sufficient.

class Demo { public static void main(String[] args) { String s = "-"; switch(TimeZone.CST) { case EST: s += "e"; case CST: s += "c"; case MST: s += "m"; default: s += "X"; case PST: s += "p"; } System.out.println(s); } } enum TimeZone {EST, CST, MST, PST } What is the result?

  1. -c

  2. -X

  3. -cm

  4. -cmp

  5. -cmXp

  6. Compilation fails.


Correct Option: E

class Demo { public static void main(String[] args) { int x = 9; int y = 6; for(int z = 0; z < 6; z++, y--) { if(x > 2) x--; label: if(x > 5) { System.out.print(x + " "}; --x; continue label; } x--; } } } What is the result?

  1. 8

  2. 8 7

  3. 8 7 6

  4. Compilation fails.

  5. An exception is thrown at runtime.


Correct Option: D

public class Demo { public static void main(String[] args) { int i=0; i++=i++;//1 ++i=++i;//2 System.out.println("i ="+i); } }

  1. //1 will create an compilation error

  2. //2 will create an compilation error

  3. //1 and //2 both will create an compilation error

  4. //1 will create an runtime error

  5. //2 will create an runtime error

  6. //1 and //2 both will create an runtime error


Correct Option: C

AI Explanation

To answer this question, let's go through each option and analyze them:

Option A) //1 will create a compilation error - This option is incorrect. The code i++=i++; will not create a compilation error. However, it will result in undefined behavior since it is modifying the same variable twice in a single statement.

Option B) //2 will create a compilation error - This option is incorrect. The code ++i=++i; will not create a compilation error. Similar to option A, it will result in undefined behavior.

Option C) //1 and //2 both will create a compilation error - This option is correct. Both i++=i++; and ++i=++i; will create a compilation error because you cannot assign a value to the result of a pre/post-increment operation.

Option D) //1 will create a runtime error - This option is incorrect. The code i++=i++; will not create a runtime error. It will result in undefined behavior, but it will not cause a runtime error.

Option E) //2 will create a runtime error - This option is incorrect. The code ++i=++i; will not create a runtime error. It will result in undefined behavior, but it will not cause a runtime error.

Option F) //1 and //2 both will create a runtime error - This option is incorrect. Neither i++=i++; nor ++i=++i; will create a runtime error. They will result in undefined behavior, but they will not cause a runtime error.

The correct answer is Option C) //1 and //2 both will create a compilation error. Both statements i++=i++; and ++i=++i; violate the rules of assignment in Java and will not compile.

class Demo { public static void main(String[] args) { int[] x = {7,6,5,4,3,2,1}; // insert code here System.out.print(y + " "); } } } Which, inserted independently at line 4, compiles? (Choose all that apply.)

  1. for(int y : x) {

  2. for(x : Int y) {

  3. int y = 0; for(y : x) {

  4. for(int y=0, z=0; z

  5. for(int y=0, int z=0, int z=0; z

  6. int y = 0; for(int z=0; z


Correct Option: A,F
  1. class Demo { 2. final static int x2 = 7; 3. final static Integer x4 = 8; 4. public static void main(String[] args) { 5. Integer x1 = 5; 6. String s = "a"; 7. if(x1 < 9) s += "b"; 8. switch(x1) { 9. case 5: s += "c"; 10. case x2: s += "d"; 11. case x4: s += "e"; 12. } 13. System.out.println(s); 14. } 15. } What is the result?
  1. abc

  2. abcde

  3. Compilation fails due only to an error on line 7.

  4. Compilation fails due to errors on multiple lines.

  5. Compilation fails due only to an error on line 10.

  6. Compilation fails due only to an error on line 11.


Correct Option: F

Given:class Demo { static String s = ""; public static void main(String[] args) { try { throw new Exception(); } catch (Exception e) { try { try { throw new Exception(); } catch (Exception ex) { s += "ic"; } throw new Exception(); } catch (Exception x) { s += "mc"; } finally { s += "mf"; } } finally { s +="of"; } System.out.println(s);} }What is the result?

  1. -ic of

  2. -mf of

  3. Compilation failure

  4. -ic mf of

  5. -ic mc mf of

  6. -ic mc of mf


Correct Option: E

Given: class Mineral { } class Gem extends Mineral { } class Miner { static int x = 7; static String s = null; public static void getWeight(Mineral m) { int y = 0 / x; System.out.print(s + " "); } public static void main(String[] args) { Mineral[] ma = {new Mineral(), new Gem()}; for(Object o : ma) getWeight((Mineral) o); } } And the command-line invocation: java Miner.java What is the result?

  1. null

  2. null null

  3. A ClassCastException is thrown.

  4. A NullPointerException is thrown.

  5. A NoClassDefFoundError is thrown.

  6. An ArrayIndexOutOfBoundsException is thrown.


Correct Option: B

AI Explanation

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

Option A) null - This option is incorrect because the output is not just "null". The output will contain two values.

Option B) null null - This option is correct. The output will be "null null".

Option C) A ClassCastException is thrown. - This option is incorrect. There is no explicit type casting in the given code, so a ClassCastException will not be thrown.

Option D) A NullPointerException is thrown. - This option is incorrect. Although the variable "s" is initialized to null, a NullPointerException will not be thrown because null can be concatenated with a string in Java without causing an exception.

Option E) A NoClassDefFoundError is thrown. - This option is incorrect. There is no indication in the code that a NoClassDefFoundError will be thrown.

Option F) An ArrayIndexOutOfBoundsException is thrown. - This option is incorrect. There is no array index access in the code, so an ArrayIndexOutOfBoundsException will not be thrown.

The correct answer is B. The output will be "null null" because the method getWeight() is called twice, once for each element in the ma array. In each call, the variable s is concatenated with a space and printed. Since s is initialized to null, the output will be "null null".

class Super { public static int main(int args[]) { System.out.println("Hi"); return 0; } public static void main(String[] args) { System.out.println("Hello"); int[] a={1,2,3,4}; int b=main(a); } } class main extends Super { public static void main(String[] args) { Super ob=new Super(); String[] b={"a","b","c","d"}; ob.main(b); System.out.println("Hi_Hello"); int[] a={1,2,3,4}; int c=main(a); } } what is the result?

  1. Compilation error.

  2. Runtime error.

  3. Hello Hi Hi_Hello Hi

  4. Hi_Hello


Correct Option: C

public class Demo { public static void main(String[] args) { String s1="Hello"; String s2="Hello"; if(s1==s2) System.out.println("Hello...."); else System.out.println("Hi...."); } } what is the result?

  1. Hello....

  2. Hi....

  3. Compilation error

  4. Runtime error


Correct Option: A

AI Explanation

To answer this question, let's go through each option:

Option A) Hello.... - This option is correct because the condition s1 == s2 is true. In Java, when comparing two strings using the == operator, it checks if the two strings are the same object in memory. In this case, both s1 and s2 are assigned the same string literal "Hello", so they refer to the same object in memory. Therefore, the condition is true, and the code will print "Hello....".

Option B) Hi.... - This option is incorrect because the condition s1 == s2 is true, as explained above. Therefore, the code will not execute the else block and will not print "Hi....".

Option C) Compilation error - This option is incorrect. The code will compile without any errors as there are no syntax errors or violations of Java language rules.

Option D) Runtime error - This option is incorrect. The code will run without any errors as there are no issues that would cause a runtime error.

The correct answer is A) Hello....

Given the following code: public class Demo { public int method(int x) { int r = 1; r += x; if ((x > 4) && (x < 10)) { r += 2 * x; } else (x <= 4) { r += 3 * x; } else { r += 4 * x; } r += 5 * x; return r; } public static void main(String [] args) { Demo ob = new Demo(); System.out.println("OF(11) is: " + ob.method (11)); } } What is the result?

  1. OF(11) is: 45

  2. OF(11) is: 56

  3. OF(11) is: 89

  4. OF(11) is: 111

  5. Compilation fails.

  6. Runtime Exception


Correct Option: E
- Hide questions