Java Certification Test! Programming Trivia Questions Quiz

95 Questions | Total Attempts: 439

SettingsSettingsSettings
Java Certification Test! Programming Trivia Questions Quiz - Quiz

Are you reading to get your java certification test? There are different things that you need to understand to ensure you do not fail the test properly, and the programming trivia questions quiz below will help you get a clue of what they are. How about you give it a shot and get to see if you actually know enough to sit for the test or need more study hours.


Questions and Answers
  • 1. 
    Given the code fragment: nums1[] =1. Given the code fragment: int nums1[] = new int[3]; int nums2[] = {1,2,3,4,5}; nums1 = nums2; for (int x : nums1) { System.out.print(x + ": "); } What is the result? new int[3]; int nums2[] = {1,2,3,4,5}; nums1 = nums2; for (int x : nums1) { System.out.print(x + ": "); } What is the result?
    • A. 

      An ArrayIndexOutOfBoundsException is thrown in runtime.

    • B. 

      Compilation fails.

    • C. 

      1: 2: 3: 4: 5:

    • D. 

      1: 2: 3:

  • 2. 
    Which three statements describe the object-oriented features of the java language? (Choose three)
    • A. 

      A main method must be declared in every class.

    • B. 

      A package must contain more than one class.

    • C. 

      A subclass can inherit from a superclass.

    • D. 

      Objects cannot be reused.

    • E. 

      Object can share behaviors with other objects.

    • F. 

      Object is the root class of all other objects.

  • 3. 
    Which three are advantages of the Java exception mechanism? (Choose three)
    • A. 

      Improves the program structure because exceptions must be handled in the method in which they occurred.

    • B. 

      Provides a set of standard exceptions that covers all the possible errors.

    • C. 

      Improves the program structure because the error handling code is separated from the normal program function.

    • D. 

      Improves the program structure because the programmer can choose where to handle exceptions.

    • E. 

      Allows the creation of new exceptions that are tailored to the program being created.

  • 4. 
    Given the following class: class CheckingAccount{ public int amount; //line n1 } And given the following main method, located in another class: public static void main (String [] args){ CheckingAccount acct = new CheckingAccount(); //line n2 } Which three pieces of code, when inserted independently, set the value of amount to 100?
    • A. 

      At line n1 insert: public CheckingAccount(){ acct.amount = 100; }

    • B. 

      At line n1 insert: public CheckingAccount(){ this.amount = 100; }

    • C. 

      At line n1 insert: public CheckingAccount(){ amount = 100; }

    • D. 

      At line n2 insert: acct.amount = 100;

    • E. 

      At line n2 insert: amount = 100;

  • 5. 
    public static void main(String[] args) { int [] arr = {1, 2, 3, 4}; int i = 0; do { System.out.print(arr[i] + " "); i++; } while (i<arr.length -1); } Given the code fragment: What is the result?
    • A. 

      1 2 3 4 followed by an ArrayIndexOutBoundsException

    • B. 

      1 2 3

    • C. 

      1 2 3 4

    • D. 

      Compilations fails.

  • 6. 
    public class Test{ void readCard(int cardNo) throws Exception{ System.out.println("Reading Card"); } void checkCard(int cardNo) throws RuntimeException{ // line n1 System.out.println("Checking Card"); } public static void main(String[] args) { Test ex = new Test(); int cardNo = 12344; ex.checkCard(cardNo); //line n2 ex.readCard(cardNo); //line n3 } } Given the code fragment: What is the result?
    • A. 

      Reading Card Checking Card

    • B. 

      Compilation fails only at line n1.

    • C. 

      Compilation fails only at line n3.

    • D. 

      Compilation fails at both line n2 and line n3.

  • 7. 
    Given: class App{ public static void main(String[] args) { int i = 10; int j = 20; int k = j += i / 5; System.out.print(i + " : " + j + " : " + k); } } What is the result?
    • A. 

      10 : 30 : 5

    • B. 

      10 : 22 : 22

    • C. 

      10 : 22 : 20

    • D. 

      10 : 22 : 5

  • 8. 
    Given the code fragment: public static void main(String[] args) { int [][]arr = new int[2][4]; arr [0] = new int[]{1, 3, 5, 7}; arr [1] = new int[]{1, 3}; for (int[] a : arr) { for (int i : a) { System.out.print(i + " "); } System.out.println(); } } What is the result?
    • A. 

      1 3 5 7 1 3

    • B. 

      1 3 1 3

    • C. 

      1 3 1 3 0 0

    • D. 

      Compilation fails.

    • E. 

      1 3 Followed by an ArrayIndexOutOfBoundsException.

  • 9. 
    Given: class Test { public static void main(String[] args) { int x = 1; int y = 0; if(x++ > ++y){ System.out.print("Hello "); } else{ System.out.print("Welcome "); } System.out.print("Log " + x + ": "+ y); } } What is the result?
    • A. 

      Welcome Log 1: 0

    • B. 

      Welcome Log 2: 1

    • C. 

      Hello Log 2: 1

    • D. 

      Hello Log 1: 0

  • 10. 
    Given: MyString.java: package p1; class MyString{ String msg; public MyString(String msg) { this.msg = msg; } } And Test.java: package p1; class Test{ public static void main(String[] args) { System.out.println("Hello " + new StringBuilder("Java SE 8")); System.out.println("Hello " + new MyString("Java SE 8")); } } What is the result?
  • 11. 
    Given: MainTest.java public class MainTest{ public static void main(int[] args) { System.out.println("int main " + args[0]); } public static void main(Object[] args) { System.out.println("Object main " + args[0]); } public static void main(String[] args) { System.out.println("String main " + args[0]); } } And commands: javac MainTest.java java MainTest 1 2 3 What is the result?
    • A. 

      String main 1

    • B. 

      Int main 1

    • C. 

      Object main 1

    • D. 

      Compilation fails.

    • E. 

      An exception is thrown at runtime.

  • 12. 
    Given the code snippet from a compiled Java source file: public class MyFile{ public static void main(String[] args) { String arg1 = args[1]; String arg2 = args[2]; String arg3 = args[3]; System.out.println("Arg is "+ arg3); } } Which command-line arguments should you pass to the program to obtain the following result? Arg is 2
    • A. 

      Java MyFile 0 1 2 3

    • B. 

      Java MyFile 1 3 2 2

    • C. 

      Java MyFile 1 2 2 3 4

    • D. 

      Java MyFile 2 2 2

  • 13. 
    Given the code fragment: import java.util.ArrayList; import java.util.Arrays; import java.util.List; class D { public static void main(String[] args) { String[] arr = {"Hi", "How", "Are", "You"}; List<String> arrList = new ArrayList(Arrays.asList(arr)); if (arrList.removeIf((String s) -> {return s.length() <= 2;})){ System.out.println(s + " removed"); } } } What is the result?
    • A. 

      An UnsupportedOperationException is thrown at runtime.

    • B. 

      Hi removed

    • C. 

      Compilation fails.

    • D. 

      The program compiles, but it prints nothing.

  • 14. 
    Which three statements are true about exception handling? (Choose tree)
    • A. 

      All subclasses of the Exception class except the RuntimeException class are checked exceptions.

    • B. 

      The parameter in a catch block is of Throwable type.

    • C. 

      All subclasses of the RuntimeException class are recoverable.

    • D. 

      All subclasses of the RuntimeException class must be caught or declared to be thrown.

    • E. 

      All subclasses of the Error class are checked exceptions and are recoverable.

    • F. 

      Only Unchecked exceptions can be rethrown.

  • 15. 
    Given: String stuff = "TV"; String res = null; if(stuff.equals("TV")){ res = "Walter"; }else if(stuff.equals("Movie")){ res = "White"; }else{ res = "No Result"; } Which code fragment can replace the if block, and it has the same effect?
    • A. 

      Stuff.equals("TV") ? res = "Walter" : stuff.equals("Movie") ? res = "White": res = "No Result";

    • B. 

      Res = stuff.equals("TV") ? "Walter" else stuff.equals("Movie") ? "White": "No Result";

    • C. 

      Res = stuff.equals("TV") ? stuff.equals("Movie") ? "Walter" : "No Result";

    • D. 

      Res = stuff.equals("TV") ? "Walter" : stuff.equals("Movie") ? "White": "No Result";

  • 16. 
    Given: class Tests2{ int a1; public static void doProduct(int a) { a = a * a; } public static void doString(StringBuilder s) { s.append(" "+ s); } public static void main(String[] args) { Tests2 item = new Tests2(); item.a1 = 11; StringBuilder sb = new StringBuilder("Hello"); Integer i = 10; doProduct(i); doString(sb); doProduct(item.a1); System.out.println(i+ " "+ sb + " " +item.a1); } } What is the result?
    • A. 

      10 Hello Hello 121

    • B. 

      10 Hello 11

    • C. 

      10 Hello 12

    • D. 

      10 Hello Hello 11

  • 17. 
    Given: class Vehicle{ String type ="4W"; int maxSpeed = 100; Vehicle(String type, int maxSpeed) { this.type = type; this.maxSpeed = maxSpeed; } } class Car extends Vehicle{ String trans; Car() { //line n1 this.trans = trans; } public Car(String trans) { super("4W", 150); this.trans = trans; } Car(String type, int maxSpeed,String trans) { super(type, maxSpeed); this.trans = trans; //line n2 } } And given the code fragment: 7. Car c1 = new Car("Auto"); 8. Car c2 = new Car("4W", 150 , "Manual"); 9. System.out.println(c1.type + " " + c1.maxSpeed + " "+ c1.trans); 10. System.out.println(c2.type + " " + c2.maxSpeed + " "+ c2.trans); What is the result?
    • A. 

      4W 150 Manual 4W 150 Auto

    • B. 

      Compilation fails in line n1.

    • C. 

      4W 150 Auto 4W 150 Manual

    • D. 

      Compilation fails in line n2.

  • 18. 
    Given the code fragment: import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; class R { public static void main(String[] args) { LocalDateTime dt = LocalDateTime.of(2014, 7, 31, 1, 1); dt.plusDays(30); dt.plusMonths(1); System.out.println(dt.format(DateTimeFormatter.ISO_DATE)); } } What is the result?
    • A. 

      2014-09-30

    • B. 

      2014-07-31

    • C. 

      07-31-2014

    • D. 

      An exception is thrown at runtime.

  • 19. 
    Given the code fragment: class CCMask{ public static String maskCC(String creditCard) { String x = "XXXX-XXXX-XXXX-"; //line n1 } public static void main(String[] args) { System.out.println(maskCC("1234-5678-9101-1121")); } } You must ensure that the maskCC method returns a string that hides all digits of the credit of except the four last digits (and the hyphens that separate each group of the four digits). Which two code fragments should you use at line n1, independently to achieve this required?
    • A. 

      StringBuilder sb = new StringBuilder(x); sb.append(creditCard, 15, 19); return sb.toString();

    • B. 

      StringBuilder sb = new StringBuilder(creditCard); sb.subString(creditCard, 15, 19); return x + sb;

    • C. 

      return x + creditCard.substring(15, 19);

    • D. 

      StringBuilder sb = new StringBuilder(creditCard); StringBuilder s = sb.insert(0, x); return s.toString();

  • 20. 
    Which two statements are true?
    • A. 

      Error class is unextendible.

    • B. 

      Error is a RuntimeException.

    • C. 

      Error is an Exception.

    • D. 

      Error is a Throwable.

    • E. 

      Error class is extendable.

  • 21. 
    You are asked to create a method that accepts an array of integers and returns the highest value from that array. Given the code fragment: class Test{ public static void main(String[] args) { int numbers[] = {12, 13, 42, 32, 15, 156, 23, 51, 12}; int max = findMax(numbers); } /* line n1*/{ int max = 0; /*code goes here*/ return max; } } Which method signature do you use at line n1?
    • A. 

      Static int[] findMax(int max)

    • B. 

      Static int findMax(int[] numbers)

    • C. 

      Final int[] findMax(int [] a)

    • D. 

      Public int findMax(int[] numbers)

  • 22. 
    Which two statements are true about a Java class?
    • A. 

      The main method can be overloaded.

    • B. 

      A class must be kept in a package to compile and run successfully.

    • C. 

      A class must contain at least an instance variable declaration and an empty constructor.

    • D. 

      The Object class is the root of the class hierarchy.

    • E. 

      A main method must be declared in the class.

    • F. 

      A top-level class can be declared with the access modifiers private, public or protected.

  • 23. 
    Given the code fragment: public static void main(String[] args) { int [] stack = {10,20,30}; int size = 3; int idx = 0; /*line n1*/ System.out.println("The Top element: "+ stack[idx]); } Which code fragment, inserted at line n1, prints the Top element: 30?
    • A. 

      while (idx <= size - 1){ idx++; }

    • B. 

      do { idx++; } while (idx <= size);

    • C. 

      while (idx < size) { idx++; }

    • D. 

      do { idx++; } while (idx >= size);

    • E. 

      do { idx++; } while (idx < size -1);

  • 24. 
    Given: class Equal{ public static void main(String[] args) { String str1 = "Java"; String [] str2 = {"J", "a", "v", "a"}; String str3= ""; for (String str : str2) { str3 = str3 + str; } boolean b1= (str1 == str3); boolean b2= (str1.equals(str3)); System.out.print(b1+ ", "+b2); } } What is the result?
    • A. 

      True, true

    • B. 

      True, false

    • C. 

      False, true

    • D. 

      False, false

  • 25. 
    Given the following class: public class Rectangle{ private double length; private double height; private double area; public void setLength(double length) { this.length = length; } public void setHeight(double height) { this.height = height; } public void setArea() { this.area = length * height; } } Which two changes would encapsulate this class and ensure that the area field is always equals to length * height whenever the Rectangle class is used?
    • A. 

      Change the setArea methods to private.

    • B. 

      Change the area field to public.

    • C. 

      Call the setArea method at the beginning of the setLenght method.

    • D. 

      Call the setArea method at the end of the setLenght method.

    • E. 

      Call the setArea method at the beginning of the setHeight method.

    • F. 

      Call the setArea method at the end of the setHeight method.

Back to Top Back to top