Java and Perl Programming Quiz

Test your knowledge of Java programming concepts including interfaces, inheritance, exceptions, arrays, and basic Perl language features

20 Questions Published

Questions

Question 1 Multiple Choice (Multiple Answers)

12 You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java //Base.java package Base; class Base{ protected void amethod(){ System.out.println("amethod"); }//End of amethod }//End of class base package Class1; //Class1.java public class Class1 extends Base{ public static void main(String argv[]){ Base b = new Base(); b.amethod(); }//End of main }//End of Class1

  1. a. Compile Error: Methods in Base not found
  2. b. Compile Error: Unable to access protected method in base class
  3. c. Compilation followed by the output "amethod"
  4. d. Compile error: Superclass Class1.Base of class Class1.Class1 not found
Question 2 Multiple Choice (Multiple Answers)

13 Which of the following statements are true?

  1. a. The elements in a Java array can only be of primitive types, not objects
  2. b. Arrays elements are initialized to default values wherever they are created using the keword new
  3. c. An array may be dynamically resized using the setSize method
  4. d. You can find out the size of an array using the size method
Question 3 Multiple Choice (Multiple Answers)

14 Which of the following are legal statements?

  1. a float f=1/3;
  2. b int i=1/3;
  3. c float f=1.01;
  4. d double d=999d;
Question 4 Multiple Choice (Multiple Answers)

15 Which of the following are valid statements?

  1. A System.out.println(1+1);
  2. B int i=2+'2';
  3. C String s="on"+'one';
  4. D byte b=255;
Question 5 Multiple Choice (Multiple Answers)

16 Which of the following statements are true?

  1. a. All of the variables in an interface are implicitly static
  2. b. All of the variables in an interface are implicitly final
  3. c. All of the methods in an interface are implicitly abstract
  4. d. A method in an interface can access class level variables
Question 6 Multiple Choice (Multiple Answers)

17 Which of the following statements are true?

  1. a. An interface can only contain method and not variables
  2. b. Interfaces cannot have constructors
  3. c. A class may extend only one other class and implement only one interface
  4. d. Interfaces are the Java approach to addressing its lack of multiple inheritance, but require implementing classes to create the functionality of the Interfaces.
Question 7 Multiple Choice (Multiple Answers)

18 You have a public class called myclass with the main method defined as follows public static void main(String parm[]){ System.out.println(parm[0]); } If you attempt to compile the class and run the program as follows java myclass hello What will happen?

  1. a. Compile time error, main is not correctly defined
  2. b. Run time error, main is not correctly defined
  3. c. Compilation and output of java
  4. d. Compilation and output of hello
Question 8 Multiple Choice (Multiple Answers)

19 Read this piece of code carefully if("String".toString() == "String") System.out.println("Equal"); else System.out.println("Not Equal");

  1. a. the code will compile an print "Equal".
  2. b. the code will compile an print "Not Equal".
  3. c. the code will cause a compiler error.
  4. d.
Question 9 Multiple Choice (Multiple Answers)

20 Read this piece of code carefully if(" String ".trim() == "String") System.out.println("Equal"); else System.out.println("Not Equal");

  1. a. the code will compile an print "Equal".
  2. b. the code will compile an print "Not Equal".
  3. c. the code will cause a compiler error
  4. d.
Question 10 Multiple Choice (Single Answer)

Given: 11. public static void main(String[] args) { 12. try { 13. args=null; 14. args[0] = “test”; 15. System.out.println(args[0]); 16. } catch (Exception ex) { 17. System.out.println(”Exception”); 18. } catch (NullPointerException npe) { 19. System.out.println(”NullPointerException”); 20. } 21. } What is the result?

  1. test
  2. Exception
  3. Compilation fails.
  4. NullPointerException
Question 11 Multiple Choice (Single Answer)

Given: 11. static classA { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(”B “); } 16. } 17. public static void main(String[] args) { 18.A a=new B(); 19. a.process(); 20.} What is the result?

  1. B
  2. The code runs with no output.
  3. An exception is thrown at runtime.
  4. Compilation fails because of an error in line 15.
  5. Compilation fails because of an error in line 18.
  6. Compilation fails because of an error in line 19.
Question 12 Multiple Choice (Multiple Answers)

Given: 8. public class test { 9. public static void main(String [] a) { 10. assert a.length == 1; 11. } 12. } Which two will produce an AssertionError? (Choose two.)

  1. java test
  2. java -ea test
  3. java test file1
  4. java -ea test file1
  5. java -ea test file1 file2
  6. java -ea:test test file1
Question 13 Multiple Choice (Single Answer)

Given: 11. public static Iterator reverse(List list) { 12. Collections.reverse(list); 13. return list.iterator(); 14. } 15. public static void main(String[] args) { 16. List list = new ArrayList(); 17. list.add(” 1”); list.add(”2”); list.add(”3”); 18. for (Object obj: reverse(list)) 19. System.out.print(obj + “,”); 20. } ‘What is the result?

  1. 3,2, 1,
  2. 1, 2, 3,
  3. Compilation fails.
  4. The code runs with no output.
  5. An exception is thrown at runtime.
Question 14 Multiple Choice (Single Answer)

Given: 11. public static void main(String[] args) { 12. String str = “null’; 13. if (str == null) { 14. System.out.println(”null”); 15. } else (str.length() == 0) { 16. System.out.println(”zero”); 17. } else { 18. System.out.println(”some”); 19. } 20. } ‘What is the result?

  1. null
  2. zero
  3. some
  4. Compilation fails.
  5. An exception is thrown at runtime.
Question 15 Multiple Choice (Single Answer)

Given: 11. public class Bootchy { 12. int bootch; 13. String snootch; 14. 15. public Bootchy() { 16. this(”snootchy”); 17. System.out.print(”first “); 18. } 19. 20. public Bootchy(String snootch) { 21. this(420, “snootchy”); 22. System.out.print(”second “); 23. } 24. 25. public Bootchy(int bootch, String snootch) { 26. this.bootch = bootch; 27. this.snootch = snootch; 28. System.out.print(”third “); 29. } 30. 31. public static void main(String[] args) { 32. Bootchy b = new Bootchy(); 33. System.out.print(b.snootch +“ “ + b.bootch); 34. } 35. } What is the result?

  1. snootchy 420 third second first
  2. snootchy 420 first second third
  3. first second third snootchy 420
  4. third second first siiootchy 420
  5. third first second snootchy 420
  6. first second first third snootchy 420
Question 16 Multiple Choice (Multiple Answers)

Given: 10. interface Foo {} 11. class Alpha implements Foo { } 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?

  1. Alpha a = x;
  2. Foo f= (Delta)x;
  3. Foo f= (Alpha)x;
  4. Beta b = (Beta)(Alpha)x;
Question 17 Multiple Choice (Multiple Answers)

Given That: 10. interface Foo {} 11. class Alpha implements Foo { } 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?

  1. Alpha a = x;
  2. Foo f= (Delta)x;
  3. Foo f= (Alpha)x;
  4. Beta b = (Beta)(Alpha)x;
Question 18 Multiple Choice (Single Answer)

Perl was develped by

  1. Larry floor
  2. Larry wall
  3. Denise Ritche
  4. Micheal Jackson
Question 19 Multiple Choice (Single Answer)

Perl is

  1. An interpreter language
  2. A compiler language
  3. Partial compiler and interpreter language
  4. An Indian language
Question 20 Multiple Choice (Single Answer)

File extension for a perl program is

  1. .pl
  2. .per
  3. .perl
  4. .prl