WorksheetsOOP final
Total questions: 59
Worksheet time: 15hrs 45mins
Which is a valid keyword in?
interface
string
Float
unsigned
Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?
final
static
private
protected
Which is a valid declarations of a String?
String s1 = null;
String s2 = 'null';
String s3 = (String) 'abc';
String s4 = (String) '\ufeed';
Which cannot directly cause a thread to stop executing?
Calling the SetPriority() method on a Thread object.
Calling the wait() method on an object
Calling notify() method on an object.
Calling read() method on an InputStream object.
Which statement is true for the following code?
public class Plant { private String name; public Plant(String name) { this.name = name; } public String getName() { return name;} } public class Tree extends Plant { public void growFruit() { } public void dropLeaves() { } }
The code will compile without changes.
The code will compile if public Tree() { Plant(); } is added to the Tree class.
The code will compile if public Plant() { Tree(); } is added to the Plant class.
The code will compile if public Plant() { this("fern"); } is added to the Plant class.
You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?
java.util.Map
java.util.Set
java.util.List
java.util.Collection
Which constructs an anonymous inner class instance?
Runnable r = new Runnable() { };
Runnable r = new Runnable(public void run() { });
Runnable r = new Runnable { public void run(){}};
System.out.println(new Runnable() {public void run() { }});
Which of the following will directly stop the execution of a Thread?
wait()
notify()
notifyall()
exits synchronized code
You can create an ArrayList using
ArrayList()
new ArrayList()
new ArrayList[ ]
new ArrayList[100]
The default value for data field of a boolean type, numeric type, object type is , respectively.
false, 1, null
true, 0, null
true, 1, Null
true, 1, null
false, 0, null
Show the output of the following code:
public class Test { public static void main(String[ ] args){ int[ ] x = {1, 2, 3, 4, 5};increase(x);int[ ] y = {1, 2, 3, 4, 5};increase(y[0]);System.out.println(x[0] + ʺ ʺ + y[0]);}public static void increase(int[ ] x){ for (int i = 0; i < x.length; i++) x[i]++;}public static void increase(int y) { y++; }}
2 1
2 2
1 2
1 1
0 0
Analyze the following fragment:
double sum = 0;double d = 0;while (d != 10.0){d += 0.1; sum += sum + d; }
The program never stops because d is always 0.1 inside the loop.
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9
The program does not compile because sum and d are declared double, but assigned with integer value 0.
Which class or interface defines the wait(), notify(),and notifyAll() methods?
Object
Thread
Runnable
Class
Which will legally declare, construct, and initialize an array?
int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};
Which of the following statements creates an instance of File on Window for the file c:\t.txt?
new File(ʺc://txt.txtʺ)
new File(ʺc:/txt.txtʺ)
new File(ʺc:\\txt.txtʺ)
new File(ʺc:\txt.txtʺ)
Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]?
x.add(ʺChicagoʺ)
x.add(1, ʺChicagoʺ)
x.add(0, ʺChicagoʺ)
x.add(2, ʺChicagoʺ)
Which of the following is the correct header of the main method?
public static void main(String x[ ])
public static void main(String[ ] args)
public static void main(String args[ ])
static void main(String[ ] args)
public static void main(String[ ] x)
When invoking a method with an object argument, _______ is passed.
the reference of the object
a copy of the object
the object is copied, then the reference of the copied object
the contents of the object
To assign a double variable d to a float variable x, you write
x = (int)d;
x=d;
x=(long)d
x=(float)d;
What is the name of the method used to start a thread execution?
int();
start();
run();
resume();
Suppose a method p has the following heading:public static int[ ][ ] p()What return statement may be used in p()?
return int[ ]{1, 2, 3};
return new int[ ][ ]{{1, 2, 3}, {2, 4, 5}};
return new int[ ]{1, 2, 3};
return {1, 2, 3};
return 1;
Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?
java.util.SortedMap
java.util.TreeMap
java.util.TreeSet
java.util.Hashtable
You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
public
protected
private
transient
Which cause a compiler error?
int[ ] scores = {3, 5, 7};
int [ ][ ] scores = {2,7,6}, {9,3,45};
String cats[ ] = {"Fluffy", "Spot", "Zeus"};
boolean results[ ] = new boolean [] {true, false, true};
Which statement is true for the following code?
public class Plant { private String name; public Plant(String name) { this.name = name; } public String getName() { return name; }}public class Tree extends Plant { public void growFruit() { } public void dropLeaves() { }}
The code will compile without changes.
Тhe code will compile if public Tree() { Plant(); } is added to the Tree class.
The code will compile if public Plant() { Tree(); } is added to the Plant class.
The code will compile if public Plant() { this("fern"); } is added to the Plant class.
Which will contain the body of the thread?
run();
start();
stop();
main();
What is the output of the following code:
public class Test { public static void main(String[ ] args){ Object o1 = new Object();Object o2 = new Object();System.out.print((o1 == o2) + ʺ ʺ + (o1.equals(o2)));}}
false true
false false
true true
true false
Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?
java.lang.String
java.lang.Double
java.lang.StringBuffer
java.lang.Character
What is the number of iterations in the following loop:for (int i = 1; i < n; i++){ // iteration }
n + 1
n - 1
2*n
n
Which interface provides the capability to store objects using a key-value pair?
Java.util.Map
Java.util.Set
Java.util.List
Java.util.Collection
What will be the output?
public class Base { public static final String FOO = "foo"; public static void main(String[] args) { Base b = new Base(); Sub s = newSub(); System.out.print(Base.FOO); System.out.print(Sub.FOO); System.out.print(b.FOO); System.out.print(s.FOO); System.out.print(((Base)s).FOO);}} class Sub extends Base { public static final String FOO="bar";}
foofoofoofoofoo
foobarfoobarbar
foobarfoofoofoo
foobarfoobarfoo
barbarbarbarbar
_________ is invoked to create an object.
A method with a return type
The main method
A constructor
A method with the void return type
Object-oriented programming allows you to derive new classes from existing classes. This is called .
generalization
abstraction
encapsulation
inheritance
Analyze the following code fragments that assign a boolean value to the variable even.Code 1:if (number % 2 == 0)even = true;elseeven = false;Code 2:even = (number % 2 == 0) ?true: false;Code 3:even = number % 2 == 0;
Code 2 has a compile error, because you cannot have true and false literals in the conditional expression.
All three are correct, but Code 2 is preferred.
All three are correct, but Code 1 is preferred.
All three are correct, but Code 3 is preferred.
Code 3 has a compile error, because you attempt to assign number to even.
The following code displays.double temperature = 50;if (temperature >= 100) System.out.println(ʺtoo hotʺ);else if (temperature <= 40) System.out.println(ʺtoo coldʺ);else System.out.println(ʺjust rightʺ);
too cold
just right
too hot
too hot too cold just right
Which one is a valid declaration of a boolean?
boolean b1 = 0;
boolean b2 = 'false';
boolean b3 = false;
boolean b4 = Boolean.false();
Which method registers a thread in a thread scheduler?
run();
construct();
start();
register();
What is the printout of the following code:double x = 5.5;int y = (int)x;System.out.println(ʺx is ʺ + x + ʺ and y is ʺ + y);
x is 5.5 and y is 5.0
x is 6 and y is 6
x is 5 and y is 6
x is 5.5 and y is 5
x is 6.0 and y is 6.0
Which is true about a method-local inner class?
It must be marked final.
It can be marked abstract.
It can be marked public.
It can be marked static.
Which is the valid declarations within an interface definition?
public double methoda();
public final double methoda();
static void methoda(double d1);
protected void methoda(double d1);
what is true about methods in a final class
all methods in a final class are implicitly protected
all methods in a final class are implicitly abstract
all methods in a final class are should be explicitly marked as final
all methods in a final class are implicitly final
All classes defined without an explicit extends clause automatically extend
Instance superclass
Nothing
Inheritance class
Object superclass
Which method helps to get the value of a private field?
getter
none of them
setter
both of them
Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?
java.util.ArrayList
java.util.HashSet
java.util.LinkedHashSet
java.util.List
You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?
java.util.List
java.util.Map
java.util.Set
java.util.Collection
Choose right sequence of steps to work with database
Define the connection URL -> Establish the connection -> Process the result -> Execute a query -> Create the Statement object -> Close the connection
Define the connection URL -> Establish the connection ->Execute a query -> Create the Statement object -> Process the result -> Close the connection
Establish the connection -> Define the connection URL -> Create the Statement object -> Execute a query -> Process the result -> Close the connection
Define the connection URL -> Establish the connection -> Create the Statement object -> Execute a query -> Process the result -> Close the connection
Which of the following statements about public access modifier is true?
accessible everywhere
accessible in the same package and by subclasses
accessible only inside the same class
accessible in the same package
These patterns are concerned with how classes and objects can be composed, to form larger structures
Creational
Behavioral
Structural
Which one is a valid declaration of a boolean?
boolean b1 = 0;
boolean b4 = Boolean.false();
boolean b3 = false;
boolean b2 = 'false';
In the following code, what is the printout for list2?class Test { public static void main(String[ ] args){ int[ ] list1 = {1, 2, 3};int[ ] list2 = {1, 2, 3};list2 = list1;list1[0] = 0;list1[1] = 1;list2[2] = 2;for (int i = 0; i < list2.length; i++)System.out.print(list2[i] + ʺ ʺ); } }
0 1 2
1 1 1
0 1 3
1 2 3
You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?
default access
public
protected
private
Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be?
public void getFinished()
public boolean getFinished()
public boolean isFinished()
public void isFinished()
Classes consist of:
Fields, Constructors, Functions
Fields, Constructors, Parameters, Methods
Parameters, Constructors, Methods
Fields, Constructors, Methods
Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?
reeMap
LinkedHashMap
The answer depends on the implementation of the existing instance.
HashMap
Which of the following will not directly cause a thread to stop?
InputStream access
sleep()
wait()
notify()
If one object is-part-of another object e.g. CPU is a part of a PC, then relationship between them is ...
Aggregation
Composition
Association
Inheritance
By usage of operator SUPER we
get an access to any of the superclass members (methods, variables, constructors)
get an access to only superclass methods
get an access to only superclass constructors
get an access to only superclass methods and constructors
What is k after the following block executes?{ int k = 2; nPrint(ʺA messageʺ, k); }System.out.println(k);
1
2
k is not defined outside the block. So, the program has a compile error
0
Classes consist of:
Fields, Constructors, Functions
Fields, Constructors, Parameters, Methods
Parameters, Constructors, Methods
Fields, Constructors, Methods
