Tag: programming languages

Questions Related to programming languages

#include int main() { int i=0; for(;i--;); printf("%d",i); }

  1. 0

  2. 1

  3. Infinite loop

  4. -1


Correct Option: D

#include int main() { int main=7; printf("%d",main); }

  1. 777777.........

  2. Compilation Error

  3. 7

  4. None of the above


Correct Option: C

#include int factorial(int n) { (n == 0)? return 1 : return n* factorial(n-1); } int main() { printf("%d",factorial(3)); }

  1. 6

  2. Errror

  3. 1

  4. No output


Correct Option: B

C was given along with which operating system?

  1. DOS

  2. unix

  3. Solaris

  4. Both 1 and 2


Correct Option: B

package testswitch; public class TestSwitch { public static void main(String[] args) { int x = 2; switch (x) { default: System.out.println("Default"); break; case 3: System.out.println("I am in 3"); break; case 2: System.out.println("I am in 2"); break; } } }

  1. I am in 2

  2. Default I am in 2

  3. Default

  4. Compiler Error


Correct Option: A

package testswitch; public class TestSwitch1 { public static void main(String[] args) { int m=100; switch(100) { case m: System.out.println("in m"); default :System.out.println("default"); } } }

  1. in m Default

  2. Compiler Error

  3. in m

  4. default


Correct Option: B

package testswitch; public class TestSwitch3 { public static void main(String[] args) { long p = 10; final long q = 100; switch (p) { case q: System.out.println("in q"); break; case 10: System.out.println("in 10"); break; default: System.out.println("default"); } } }

  1. in 10 Default

  2. Compiler error

  3. in 10

  4. Exception


Correct Option: B

package testswitch; public class TestSwitch4 { public static void main(String[] args) { int x = 2; switch (x) { default: System.out.println("Default"); case 3: System.out.println("I am in 3"); case 1: System.out.println("I am in 1"); } } }

  1. Default I am in 3 I am in 1

  2. Default

  3. No output

  4. Exception


Correct Option: A

package testswitch; public class TestSwitch5 { public static void main(String[] args) { char a = 'a'; switch (a) { default: System.out.println("I am in default"); break; case 'a': System.out.println("I am in a"); break; } } }

  1. I am in a

  2. I am in default

  3. Exception

  4. Compiler Error


Correct Option: A

Given: class Scoop { static int thrower() throws Exception { return 42; } public static void main(String [] args) { try { int x = thrower(); } catch (Exception e) { X++; } finally { System.out.printIn("x = " + ++x); } } } What is the result?

  1. x = 42

  2. x = 43

  3. x = 44

  4. Compilation fails.

  5. The code runs with no output


Correct Option: D

AI Explanation

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

Option A) x = 42 - This option is incorrect because the variable x is not accessible outside the try block, so it cannot be printed in the finally block.

Option B) x = 43 - This option is incorrect for the same reason as Option A. The variable x is not accessible outside the try block, so it cannot be printed in the finally block.

Option C) x = 44 - This option is incorrect for the same reason as Option A and B. The variable x is not accessible outside the try block, so it cannot be printed in the finally block.

Option D) Compilation fails - This option is correct. The code will not compile due to an error. In the catch block, the code tries to increment the variable X, but the variable X is not declared anywhere in the code. Therefore, the code will fail to compile.

Option E) The code runs with no output - This option is incorrect because the code will not compile, as explained in Option D.

The correct answer is D. Compilation fails. This option is correct because the code will fail to compile due to the error in the catch block where the variable X is not declared.