Java and C Programming Quiz

Test your knowledge of Java and C programming fundamentals including object-oriented concepts, exception handling, threading, and pointer arithmetic.

7 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

char* riteshFunc (char *ptr) { ptr += 3; return (ptr); } int main() { char *x, *y; x = "HELLO"; y = riteshFunc(x); printf ("y = %s \n", y); return 0; } What will print when the sample code above is executed?

  1. HELLO
  2. ELLO
  3. LLO
  4. LO
Question 2 Multiple Choice (Single Answer)

What will be the result of compiling and running the following code? (Assume that assertions are enabled at compile time as well as at runtime.) class Test { String f(int i) { switch (i) { case 0: return "A"; case 1: return "B"; case 2: return "C"; default: assert false; } } public static void main(String[] args) { Test t = new Test(); for (int i = 0; i < 4; i++) { System.out.print(t.f(i)); } } }

  1. Prints "ABC" and throws AssertionError
  2. Prints "ABC" and throws AssertionException
  3. Prints "ABC" and exits without any error
  4. Compilation error
  5. Run time exception
  6. None of the above
Question 3 Multiple Choice (Single Answer)

Given the code below, which access modifiers (public, protected, or private) can legally be placed before the myMethod() method on line 3, if no other changes are made to the code? If line 3 is left as it is, which keywords can legally be placed before the myMethod method on line 8? 1. class HumptyDumpty 2. { 3. void myMethod() {} 4. } 5. 6. class HankyPanky extends HumptyDumpty 7. { 8. void myMethod() {} 9. }

  1. . private or nothing (default) on line 3. Nothing (default) or protected or public on line 8.
  2. public or protected on line 3. private or nothing (default) on line 8.
  3. Nothing (default) or protected or public on line 3. private or nothing (default) on line 8.
  4. public on line 3 and private on line8.
Question 4 Multiple Choice (Single Answer)

Which is the most appropriate way to handle invalid method arguments passed to a public method?

  1. Throw AssertionError
  2. Throw IllegalStateException
  3. Throw IllegalArgumentException
  4. Throw InvalidArgumentException
Question 5 Multiple Choice (Single Answer)

What will be printed on standard output if the following class is executed using the command "java Test 1 two 3" ? public class Test { static public void main(String[] args) { try { int k = Integer.parseInt(args[1]); System.out.println(args[k]); } catch (Exception e) { System.out.println(e); } } }

  1. 1
  2. two
  3. NumberFormatException
  4. ArrayIndexOutOfBoundsException
  5. Code does not compile
Question 6 Multiple Choice (Single Answer)

What will be the result of compiling and running the following code? public class MyThread extends Thread { public static void main(String[] args) { new MyThread().start(); } }

  1. Compile-time error
  2. Runtime error
  3. No output
  4. None of these
Question 7 Multiple Choice (Single Answer)

Given the following two classes, which statement is true? ---------- class-1 ---------- package pack; public class Parent { protected void test() { System.out.println("Test"); } } ---------- class-2 ---------- 1. package mypack; 2. import pack.*; 3. class ParentTest extends Parent{ 4. public static void main(String[] args){ 5. new ParentTest().test(); 6. new Parent().test(); 7. } 8. }

  1. The code will compile and run only if line 5 is removed.
  2. The code will compile and run only if line 6 is removed
  3. The code compiles and runs, printing "Test" twice
  4. The code compiles but throws an exception at runtime