Senin, 08 Mei 2017

OJP 2

1. What is the output from the following code snippet?
String str1= "java";
String str2=new String("java");
System.out.println( str1==str2 );
System.out.println( str1==str2.intern() );
 Mark for Review
(1) Points


The code will compile and print "false false"


The code will compile and print "true true"


The code does not compile.


The code will compile and print "false true" (*)


The code will compile and print "true false"



Incorrect incorrect. Refer to Section 1 Lesson 1.


2. Examine the following Classes:
Student and TestStudent
What is the output from the println statement in TestStudent?

public class Student {
 private int studentId = 0;

 public Student(){
  studentId++;
 }
 public static int getStudentId(){
 return studentId;
 }
}

public class TestStudent {
 public static void main(String[] args) {
 Student s1 = new Student();
 Student s2 = new Student();
 Student s3 = new Student();
 System.out.println(Student.getStudentId());
 }
} Mark for Review
(1) Points


1


3


TestStudent will throw an exception


No output. Compilation of TestStudent fails (*)



Incorrect incorrect. Refer to Section 1 Lesson 1.


3. What is the output from the following code snippet?  
for (int i = 0; i < 10; i++) {
  if (i == 3) {
    break;
  }
System.out.print(i); Mark for Review
(1) Points


The code will compile and print "123"


The code will compile and print "012" (*)


The code will compile and print "0123"


The code does not compile.



Correct Correct


4. What do Arrays and ArrayLists have in common?
I. They both store data.
II. They can both be traversed in loops.
III. They both can be dynamically re-sized during execution of a program.
 Mark for Review
(1) Points


I only


II only


I and II only (*)


I, II and III only


None of these



Correct Correct


5. When an object is able to pass on its state and behaviors to its children, this is called: Mark for Review
(1) Points


Isolation


Encapsulation


Polymorphism


Inheritance (*)



Correct Correct
6. Which of the following statements is false? Mark for Review
(1) Points


In an Array you need to know the length and the current number of elements stored.


An ArrayList can grow and shrink dynamically as required.


An ArrayList has a fixed length. (*)


An ArrayList can store multiple object types.



Incorrect Incorrect. Refer to Section 1 Lesson 2.


7. Reading great code is just as important for a programmer as reading great books is for a writer. True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct


8. Examine the following code snippet. What is this an example of?

public class Car extends Vehicle {
public Car() {
...
}
} Mark for Review
(1) Points


Inheritance (*)


Polymorphism


Comments


Encapsulation



Correct Correct


9. The main purpose of unit testing is to verify that an individual unit (a class, in Java) is working correctly before it is combined with other components in the system. True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct




Section 2
(Answer all questions in this section)

10. public static void printArray(T[] array){οΎ….
is an example of what? Mark for Review
(1) Points


A generic instance


A generic method (*)


A generic class


A concreate method.



Incorrect Incorrect. Refer to Section 2 Lesson 3.
11. An example of an upper bounded wildcard is. Mark for Review
(1) Points


ArrayList<T>


ArrayList<? extends Animal> (*)


ArrayList<?>


ArrayList<? super Animal>



Correct Correct


12. Wildcards in generics allows us greater control on the types that can be used.
True or False? Mark for Review
(1) Points


True (*)


False



Incorrect Incorrect. Refer to Section 2 Lesson 3.


13. When would an enum (or enumeration) be used? Mark for Review
(1) Points


When you already know all the possibilities for objects of that class. (*)


When you want to be able to create any number of objects of that class.


When you wish to initialize a HashSet.


When you wish to remove data from memory.



Correct Correct


14. Of the options below, what is the fastest run-time? Mark for Review
(1) Points


log(n) (*)


n


n^2


n*log(n)



Correct Correct


15. Big-O Notation is used in Computer Science to describe the performance of Sorts and Searches on arrays. True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct
16. Which of the following is the correct lexicographical order for the conents of the following int array?

{17, 1, 1, 83, 50, 28, 29, 3, 71, 22} Mark for Review
(1) Points


{1, 2, 7, 0, 9, 5, 6, 4, 8, 3}


{1, 1, 3, 17, 22, 28, 29, 50, 71, 83}


{71, 1, 3, 28,29, 50, 22, 83, 1, 17}


{83, 71, 50, 29, 28, 22, 17, 3, 1, 1}


{1, 1, 17, 22, 28, 29, 3, 50, 71, 83} (*)



Correct Correct


17. Binary searches can be performed on sorted and unsorted data.
True or false? Mark for Review
(1) Points


True


False (*)



Incorrect Incorrect. Refer to Section 2 Lesson 6.


18. 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? Mark for Review
(1) Points


True (*)


False



Correct Correct


19. Selection sort is efficient for large arrays.
True or false? Mark for Review
(1) Points


True


False (*)



Correct Correct


20. 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? Mark for Review
(1) Points


True


False (*)



Correct Correct
21. A sequential search is an iteration through the array that stops at the index where the desired element is found. True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct


22. A LinkedList is a type of Stack.
True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct


23. Why can a LinkedList be considered a stack and a queue?(Choose Three) Mark for Review
(1) Points

(Choose all correct answers)


Because you can not add element to the beginning of it.


Because you can remove elements from the end of it. (*)


Because you can add elements to the end of it. (*)


Because you can remove elements from the beginning of it. (*)



Correct Correct


24. Nodes are components of LinkedLists, and they identify where the next and previous nodes are.
True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct


25. FIFO stands for: Mark for Review
(1) Points


Fast In Fast Out


First In First Out (*)


Fast Interface Fast Output


First Interface First Output



Correct Correct
26. Implementing the Comparable interface in your class allows you to define its sort order.
True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct


27. Which of the following is a list of elements that have a first in last out ordering. Mark for Review
(1) Points


Enums


Stacks (*)


HashMaps


Arrays



Correct Correct


28. Which scenario best describes a stack? Mark for Review
(1) Points


A pile of pancakes with which you add some to the top and remove them one by one from the top to the bottom. (*)


A row of books that you can take out of only the middle of the books first and work your way outward toward either edge.


A line at the grocery store where the first person in the line is the first person to leave.


All of the above describe a stack.



Correct Correct


29. What are maps that link a Key to a Value? Mark for Review
(1) Points


ArrayLists


Arrays


HashMaps (*)


HashSets



Correct Correct


30. A downward cast of a superclass to subclass allows you to access a subclass specialized method call.
True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct
31. What is the result of the following code snippet??

1. abstract class AbstractBankAccount {
2. abstract int getBalance ();
3. )
4. public class CreditAccount extends AbstractBankAccount{
5. private int balance;
6. private int getBalance (){
7. return balance;
8. }
9.} Mark for Review
(1) Points


Compilation is successful.


An error at line 6 causes compilation to fail. (*)


An error at line 2 causes compilation to fail.


An error at line 4 causes compilation to fail.



Correct Correct


32. Virtual method invocation requires that the superclass method is defined as which of the following? Mark for Review
(1) Points


A default final method.


A public method. (*)


A private final method.


A public static method.


A public final method.



Correct Correct


33. When line 10 is executed, which method will be called?
1 class Account {
2 public void deposit(int amt, int amt1) { }
3 public void deposit(int amt){ }
4 }
5 public class CreditAccount extends Account {
6 public void deposit() { }
7 public void deposit(int amt) {}
8 public static void main(String args[]){
9 Account account = new CreditAccount();
10 account.deposit(10);
11 }
12 } Mark for Review
(1) Points


line 6


line 7 (*)


line 2


line 3



Correct Correct


34. Upward casting an object instance means you can't access subclass specific methods.
True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct


35. Which two of the following statements are true? (Choose Two) Mark for Review
(1) Points

(Choose all correct answers)


An abstract class can create subclass and be constructed.


An abstract class can define constructor. (*)


An abstract class must be difined by using the abstract keyword. (*)


An abstract class must contain abstrct method.



Correct Correct
36. Which line contains an compilation error?

interface Shape {}
interface InnerShape extends Shape{}
class Circle implements Shape{ }
class InnerCircle extends Circle{}
class Rectangle implements Shape{}
public class Tester {
 public static void main(String[] args) {
 
  Circle c= new Circle();
  InnerCircle ic = new InnerCircle();
  Rectangle  rect   = new Rectangle();
 
  System.out.print(c instanceof InnerCircle); //Line 1
  System.out.print(ic instanceof Circle);     //Line 2
  System.out.print(rect instanceof InnerCircle); //Line 3
  System.out.print(rect instanceof Shape);   //Line 4
 }
} Mark for Review
(1) Points


Line 1


Line 4


Line 2


Line 3 (*)



Correct Correct


37. Which of the following statements can be compiled?(Choose Three) Mark for Review
(1) Points

(Choose all correct answers)


List list = new ArrayList<String>(); (*)


List<String> list = new ArrayList<String>(); (*)


List<?> list = new ArrayList<String>(); (*)


List<Object> list = new ArrayList<String>();



Incorrect Incorrect. Refer to Section 2 Lesson 4.


38. What is the output from the following code snippet?

Integer[] ar = {1, 2, 1, 3};
Set<Integer> set = new TreeSet<Integer>(Arrays.asList(ar));
set.add(4);
for (Integer element : set) {
 System.out.print(element);
} Mark for Review
(1) Points


11234


12134


1234 (*)


1213



Correct Correct


39. A collection is an interface in the Java API.
True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct


40. A HashSet is a set that is similar to an ArrayList. A HashSet does not have any specific ordering.
True or false? Mark for Review
(1) Points


True (*)


False



Correct Correct
41. Which class is an ordered collection that may contain duplicates? Mark for Review
(1) Points


array


set


enum


list (*)



Correct Correct


42. What is a set? Mark for Review
(1) Points


A collection of elements that does not contain duplicates. (*)


A keyword in Java that initializes an ArrayList.


A collection of elements that contains duplicates.


Something that enables you to create a generic class without specifying a type between angle brackets <>.



Correct Correct


43. Which line contains a compilation error?

interface Shape {}
class Circle implements Shape{}

public class Test{
 public static void main(String[] args) {
  List<? extends Shape> ls = new ArrayList<Circle>(); // Line 1
  List<Circle> lc = new ArrayList<Circle>(); // Line 2
  Circle c = new Circle();
  ls.add(c); // Line 3
  lc.add(c); // Line 4
 }
} Mark for Review
(1) Points


Line 4


Line 1


Line 2


Line 3 (*)



Correct Correct


44. You can only implement one interface in a class.
True or False? Mark for Review
(1) Points


True


False (*)



Correct Correct


45. Classes define and implement what? Mark for Review
(1) Points


Some methods with implementations


All methods with implementations


Variables and methods (*)


All method definitions without any implementations


Constants and all methods with implementations



Correct Correct
46. Modeling business problems requires understanding the interaction between interfaces, abstract and concrete classes, subclasses, and enum classes. Mark for Review
(1) Points


True (*)


False



Correct Correct


47. Which one of the following would allow you to define an interface for Animal? Mark for Review
(1) Points


public Animal extends Interface {}


public interface Animal {} (*)


public class Animal implements Interface {}


public class Animal {}



Correct Correct


48. The state of an object differentiates it from other objects of the same class.
True or False? Mark for Review
(1) Points


True (*)


False



Correct Correct


49. Which one of the following statements is true? Mark for Review
(1) Points


A class is a template that defines the features of an object (*)


A class is a Java primitive


A Java program can only contain one super class


A class is an intance of an object



Correct Correct


50. Immutable classes do allow instance variables to be changed by overriding methods.
True or false? Mark for Review
(1) Points


True


False (*)



Correct Correct

Tidak ada komentar:

Posting Komentar