Halaman

Tuesday, July 21, 2020

Midterm Exam Java Programming 2019 Learner - English

Midterm Exam

Section 1
(Answer all questions in this section)

What is the output from the following code snippet?

int i=0,j=0;
i=++i;
j=i++;
System.out.println("i=" + i + " " + "j=" + j);


The code will compile and print "i=2 j=2"


The code will compile and print "i=2 j=1" (*)


The code does not compile.


The code will compile and print "i=1 j=1"


The code will compile and print "i=1 j=2"



Examine the partial class declaration below:
class Foo{
  public Foo(String s,int i ){
    this(i);
  }
  public Foo(int i){
  }
  public Foo(int i,int j){
  }
}

Which of following statements can not be used to create a instance of Foo?



Foo f=new Foo(1,2);


Foo f=new Foo(1);


Foo f=new Foo("java",1);


Foo f=new Foo(); (*)



Which two statements prevent a method from being overriden? (Choose Two)

(Choose all correct answers)


Void final act() {}


Static void act() {}


Final void act() {} (*)


Static final void act() {} (*)


Final abstract void act() {}



What is the output from the following code snippet?

boolean status=false;
int i=1;
if( (++i>1)  && (status=true))
  i++;
if( (++i>3)  ||  (status=false))
   i++;
System.out .println (i);


4


3


5 (*)


6



What is the output from the following code?
int x=0;
int y=5;
do{
   ++x;
   y--;
}while(x<3);
System.out.println(x + " " + y);


2 3


3 2 (*)


2 2


3 3


When you instantiate a subclass, the superclass constructor will be also invoked. True or False?


True (*)


False



Class Object is the root of the Java class hierarchy. True or False?


True (*)


False



Section 2
(Answer all questions in this section)

What is the result from creating the following try-catch block?

1.try {

2.} catch (Exception e) {
   
3.} catch (ArithmeticException a) {
   
4.}


Compile fails at Line 2


Compile fails at Line 1


The code compiles


Compile fails at Line 3 (*)



In what order do multiple catch statements execute?


The order they are declared in ( most specific first). (*)


They all execute at the same time.


The order they are declared in (most general first).


None of them execute since you cannot have multiple catch statements.



What is one step you must do to create your own exception?


Exceptions cannot be created. They are only built in to Java.


Declare the primitive data type Exception.


Create a new class that extends Exception. (*)


Create a new class that implements Exception.



What symbol(s) is used to separate multiple exceptions in one catch statement?


A single vertical bar | (*)


None, multiple exceptions can't be handled in one catch statement.


&&


(==) (equals equals)



Which statement is true for the class java.util.ArrayList?


The elements in the collection are ordered. (*)


The elements in the collection are synchronized.


The elements in the collection are immutable.


The elements in the collection are accessed using key.



When an object is able to pass on its state and behaviors to its children, this is called:


Inheritance (*)


Isolation


Polymorphism


Encapsulation



Which of the following statements about arrays and ArrayLists in Java are true?
I. An Array has a fixed length.
II. An Array can grow and shrink dynamically as required.
III. An ArrayList can store multiple object types.
IV. In an ArrayList you need to know the length and the current number of elements stored.



I and III only (*)


II and IV only


I, II, and III only


I, II, III and IV


None of these



Virtual method invocation occurs:


When the method of a subclass is used on a subclass reference.


When the method of a superclass is used on a superclass reference.


Not part of polymorphism.


When the method of a subclass is used on a superclass reference. (*)


A downward cast of a superclass to subclass allows you to access a subclass specialized method call.
True or false?


True (*)


False



The instanceof operator can find subclass objects when they are passed to method which declare a superclass type parameter.
True or false?


True (*)


False



In general, classes can be made immutable by placing a final key word before the class keyword.
True or false?


False


True (*)



An interface can implement methods.
True or False?


True


False (*)



The state of an object differentiates it from other objects of the same class.
True or False?


True (*)


False


Section 3
(Answer all questions in this section)

An example of an upper bounded wildcard is.


ArrayList (*)


ArrayList


ArrayList


ArrayList



What is the correct definition of Enumeration (or enum)?


A keyword that specifies a class whose objects are defined inside the class. (*)


A bounded generic class


A list of elements that is dynamically stored.


Code that initializes an ArrayList



Which of the following correctly initializes an object named cell of the class Telephones whose generic type is Cellular?


Telephones cell = new Telephones(Cellular c);


Telephones(Cellular) cell = new Telephones(Cellular);


Telephones<> cell = new Telephones<>(Cellular c);


Telephones cell = new Telephones(); (*)


None of the these.



< ? extends Animal > is an example of a bounded generic wildcard.
True or False?


True (*)


False



Generic methods are required to be declared as static.
True or False?


True


False (*)


The Comparable interface includes a method called compareTo.
True or false?


True (*)


False



The Comparable interface defines the compareTo method.
True or false?


True (*)


False



Which of the following is a list of elements that have a first in last out ordering.


Enums


Arrays


Stacks (*)


HashMaps



What are maps that link a Key to a Value?


Arrays


ArrayLists


HashMaps (*)


HashSets



Implementing the Comparable interface in your class allows you to define its sort order.
True or false?


True (*)


False


Binary searches can be performed on sorted and unsorted data.
True or false?


True


False (*)



Selection sort is a sorting algorithm that involves finding the minimum value in the list, swapping it with the value in the first position, and repeating these steps for the remainder of the list.
True or false?


True (*)


False



Bubble Sort is a sorting algorithm that involves swapping the smallest value into the first index, finding the next smallest value and swapping it into the next index and so on until the array is sorted.
True or false?


True


False (*)



Which of the following is a sorting algorithm that involves repeatedly incrementing through the array and swapping 2 adjacent values if they are in the wrong order until all elements are in the correct order?


Bubble Sort (*)


Selection Sort


Merge Sort


Sequential Search


Binary Search



Which of the following sorting algorithms utilizes a "divide and conquer" technique to sort arrays with optimal speed?


Sequential Search


Merge Sort (*)


Selection Sort


Binary Search


All of the above


None of the above.


ArrayList and Arrays both require you to define their size before use.
True or false?


True


False (*)



What is the output from the following code snippet?

   TreeSett=new TreeSet();

    if (t.add("one"))

    if (t.add("two"))

    if (t.add ("three"))

    t.add("four");

    for (String s : t)

    System.out.print (s);


The code does not compiles.


onetwothreefour


twofouronethree


fouronethreetwo (*)



Which one of the following would initialize a HashSet raffleTickets which will store the class RaffleTicket.


HashSet raffleTickets = new HashSet();


HashSet raffleTickets = new HashSet();


HashSet raffleTickets = new ();


HashSet raffleTickets = new HashSet(); (*)



Sets may contain duplicates.
True or false?


True


False (*)



Section 4
(Answer all questions in this section)

A base case can handle nested conditions.
True or false?


True (*)


False


Forward thinking helps when creating linear recursive methods.
True or false?


True


False (*)



A non-linear recursive method can call how many copies of itself?


1


2 or more (*)


None



A non-linear recursive method is less expensive than a linear recursive method.
True or false?


True


False (*)



Which of the following are true about parsing a String?(Choose Three)

(Choose all correct answers)


It is not possible to parse a string using regular expressions.


It is possible to use a for loop to parse a string. (*)


It is possible to use the String.split() method to parse a string. (*)


It is a way of dividing a string into a set of sub-strings. (*)



Which of the following correctly initializes a StringBuilder?


StringBuilder sb = "This is my String Builder";


StringBuilder sb = StringBuilder(500);


StringBuilder sb = new StringBuilder(); (*)


None of the above.


Which of the following methods can be used to replace a segment in a string with a new string?


remove(String oldString, String newString)


replaceAll(String oldString, String newString) (*)


replaceAll(String newString)


substring(int start, int end, String newString)


None of the above. There is no replaceAll(String newString) method with one argument.



Consider that you are making a calendar and decide to write a segment of code that returns true if the string month is April, May, June, or July. Which code segment correctly implements use of regular expressions to complete this task?


return month.equals("April, May, June, July");


return month.matches("April|May|June|July"); (*)


return month.substring(0,3);


return month.matches("April"|"May"|"June"|"July");


return month.equals("April|May|June|July");


return month.compareTo("April, May, June, July");



In a regular expression, {x} and {x,} represent the same thing, that the preceding character may occur x or more times to create a match.
True or false?


True


False (*)



Which of the following correctly defines Pattern?


A method of dividing a string into a set of sub-strings.


A regular expression symbol that represents any character.


A class in the java.util.regex package that stores the format of a regular expression. (*)


A class in the java.util.regex package that stores matches.



A regular expression is a character or a sequence of characters that represent a string or multiple strings.
True or false?


True (*)


False

No comments:

Post a Comment

Final Exam Java Programming 2019 Learner - English

Final Exam