QUESTIONS SELF-TEST JAVA PROGRAMMING 1. Real

advertisement
QUESTIONS SELF-TEST JAVA PROGRAMMING
1.
2.
3.
4.
5.
6.
7.
8.
9.
Real-world objects contain ___ and ___.
A software object's state is stored in ___.
A software object's behavior is exposed through ___.
Hiding internal data from the outside world, and accessing it only through publicly exposed
methods is known as data ___.
A blueprint for a software object is called a ___.
Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword.
A collection of methods with no implementation is called an ___.
A namespace that organizes classes and interfaces by functionality is called a ___.
The term API stands for ___?
10. Which of the following statements about arrays is syntactically wrong?
O (a) Person[] p = new Person[5];
O (b) Person p[5];
O (c) Person[] p [];
O (d) Person p[][] = new Person[2][];
11.Given the following piece of code:
public class Test {
public static void main(String args[]) {
int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ) {
System.out.print(" " + i + " " + j );
}
System.out.print(" " + i + " " + j );
}
}
what will be the result?
O (a) 0 6 1 7 2 8 3 8
O (b) 0 6 1 7 2 8 3 9
O (c) 0 5 1 5 2 5 3 5
O (d) compilation fails
12. Which of the following declarations is correct? (2 answers):
[_] [a] boolean b = TRUE;
[_] [b] byte b = 255;
[_] [c] String s = “null”;
[_] [d] int i = new Integer(“56”);
13.Suppose a class has public visibility. In this class we define a protected method. Which of the
following statements is correct?
O (a) This method is only accessible from inside the class itself and from inside all subclasses.
O (b) In a class, you can not declare methods with a lower visibility than the visibility of the lass
in which it is defined.
O (c) From within protected methods you do not have access to public methods.
O (d) This method is accessible from within the class itself and from within all classes defined
in the same package as the class itself.
14. Given the following piece of code:
public class Company{
public abstract double calculateSalaries();
}
which of the following statements is true?
O (a) The keywords public and abstract can not be used together.
O (b) The method calculateSalaries() in class Company must have a body
O (c) You must add a return statement in method calculateSalaries().
O (d) Class Company must be defined abstract.
15. Given the following piece of code:
public interface Guard{
void doYourJob();
}
abstract public class Dog implements Guard{}
which of the following statements is correct?
O (a) This code will not compile, because method doYourJob() in interface Guard must be
defined abstract.
O (b) This code will not compile, because class Dog must implement method doYourJob()
from interface Guard.
O (c) This code will not compile, because in the declaration of class Dog we must use the
keyword
extends in stead of implements.
O (d) This code will compile without any errors.
16. Given these classes:
public class Person{
public void talk(){ System.out.print("I am a Person ");
}
public class Student extends Person {
public void talk(){ System.out.print("I am a Student ");
}
}
}
what is the result of this piece of code:
public class Test{
public static void main(String args[]){
Person p = new Student();
p.talk();
}
}
O (a) I am a Person
O (b) I am a Student
O (c) I am a Person I am a Student
O (d) I am a Student I am a Person
17. Given the following piece of code:
public class Person{
private String firstName;
public Person(String fn){ firstName = fn; }
}
public class Student extends Person{
private String studentNumber;
public Student(String number) { studentNumber = number; }
}
Which of the following statements is true? (2 answers)
[_] [a] This code will compile if we define in class Person a no-argument constructor.
[_] [b] This code will compile if we define in class Student a no-argument constructor.
[_] [c] This code will compile if we add in the constructor of Student the following line of code
as first statement:
super();
[_] [d] This code will compile if we call the constructor of Person from within the constructor of
Student.
18.Specify the correct characteristics of an enumeration type (2 answers)
[_] [a] enum can define static fields and methods
[_] [b] enum can contain a public constructor
[_] [c] enum can implement interfaces
[_] [d] enum is a reference to a variable set of constants
19. Given the following piece of code:
class Person { public int number; }
public class Test{
public void doIt(int i , Person p){
i = 5;
p.number = 8;
}
public static void main(String args[]){
int x = 0;
Person p = new Person();
new Test().doIt(x, p);
System.out.println(x + " " + p.number);
}
}
What is the result?
O (a) 0 8
O (b) 5 0
O (c) 0 0
O (d) 5 8
20. Given the following piece of code:
class SalaryCalculationException extends Exception{}
class Person{
public void calculateSalary() throws SalaryCalculationException {
//...
throw new SalaryCalculationException();
//...
}
}
class Company{
public void paySalaries(){
new Person().calculateSalary();
}
}
Which of the following statements is correct? (2 answers)
[_] [a] This code will compile without any problems.
[_] [b] This code will compile if in method paySalaries() we return a boolean in stead of
void.
[_] [c] This code will compile if we add a try-catch block in paySalaries()
[_] [d] This code will compile if we add throws SalaryCalculationException in the signature of
method paySalaries().
21. Which of the following statements regarding static methods are correct? (2 answers)
[_] [a] static methods are difficult to maintain, because you can not change their implementation.
[_] [b] static methods can be called using an object reference to an object of the class in which
this method is defined.
[_] [c] static methods are always public, because they are defined at class-level.
[_] [d] static methods do not have direct access to non-static methods which are defined inside
the same class.
22. Given the following piece of code:
class Person{ public void talk(){} }
public class Test{
public static void main(String args[]){
Person p = null;
try{
p.talk();
} catch(NullPointerException e){
System.out.print("There is a NullPointerException. ");
} catch(Exception e){
System.out.print("There is an Exception. ");
}
System.out.print("Everything went fine. ");
}
}
what will be the result?
O (a) If you run this program, the outcome is:
There is a NullPointerException. Everything went fine.
O (b) If you run this program, the outcome is:
There is a NullPointerException.
O (c) If you run this program, the outcome is:
There is a NullPointerException. There is an Exception.
O (d) This code will not compile, because in Java there are no pointers.
23. Which of the following statement about Generics are correct? (2 answers)
[_] [a] Generics are typed subclasses of the classes from the Collections framework
[_] [b] Generics are used to parameterize the collections in order to allow for static type checking
at compile tIme of the objects in the collection.
[_] [c] Generics can be used to perform type checking of the objects in a collection at runtime.
[_] [d] Generics can be used to iterate over a complete collection in an easy way, using the
‘enhanced for’ loop.
24. Which collection class associates values witch keys, and orders the keys according to their
natural order?
O (a) java.util.HashSet
O (b) java.util.LinkedList
O (c) java.util.TreeMap
O (d) java.util.SortedSet
25. Which of the following statements about GUI components is wrong?
O (a) Swing exists since version 1.2 of the jdk.
O (b) AWT stands for Abstract Window Toolkit
O (c) You can not place AWT components on Swing containers.
O (d) The AWT classes are deprecated.
26. Which of the following statements about events are correct? (2 answers)
[_] [a] Event objects are placed on a Queue, where they are fetched by subscribers (objects of
classes which implement the interface Subscriber).
[_] [b] The listener of an event must implement the method public void listen(EventObject obj).
[_] [c] Each event object must be an object of a subclass of EventObject.
[_] [d] Each event listener can investigate about the source of an event by calling the method
getSource() on the event object.
27. How can you serialize an object?
O (a) You have to make the class of the object implement the interface Serializable.
O (b) You must call the method serializeObject() (which is inherited from class Object) on the
object.
O (c) You should call the static method serialize(Object obj) from class Serializer, with as
argument the object to be serialized.
O (d) You don’t have to do anything, because all objects are serializable by default.
28. Which statements about IO are correct (2 answers)?
[_] [a] OutputStream is the abstract superclass of all classes that represent an outputstream of
bytes.
[_] [b] Subclasses of the class Reader are used to read character streams.
[_] [c] To write characters to an outputstream, you have to make use of the class
CharacterOutputStream.
[_] [d] To write an object to a file, you use the class ObjectFileWriter.
29. Given the following piece of code:
public class MyThread extends Thread{
public String text;
public void run(){
System.out.print(text);
}
}
public class Test{
public static void main(String args[]){
}
}
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
System.out.print("three ");
t1.text = "one ";
t2.text = "two ";
Which of the following statements is true?
O (a) If you execute this program, the result is always one two three
O (b) If you execute this program, the result is always three one two
O (c) The result of this program is undetermined.
O (d) Compilation will fail.
30. Which one of these lists contains only Java programming language keywords?
A.class, if, void, long, Int, continue
B. goto, instanceof, native, finally, default, throws
C. try, virtual, throw, final, volatile, transient
D.strictfp, constant, super, implements, do
E. byte, break, assert, switch, include
31. Which is a reserved word in the Java programming language?
A.method
B. native
C. subclasses
D.reference
E. array
32.Which is a valid keyword in java?
A.interface
C. Float
B. string
D.unsigned
33. public interface Foo
{
int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
1. final int k = 4;
2. public int k = 4;
3. static int k = 4;
4. abstract int k = 4;
5. volatile int k = 4;
6. protected int k = 4;
A.1, 2 and 3
C. 3, 4 and 5
B. 2, 3 and 4
D.4, 5 and 6
34. Which three are valid declarations of a char?
1.
2.
3.
4.
5.
6.
char c1 = 064770;
char c2 = 'face';
char c3 = 0xbeef;
char c4 = \u0022;
char c5 = '\iface';
char c6 = '\uface';
A.1, 2, 4
B. 1, 3, 6
C. 3, 5
D.5 only
35. Which is the valid declarations within an interface definition?
A.public double methoda();
B. public final double methoda();
C. static void methoda(double d1);
D.protected void methoda(double d1);
36. Which one is a valid declaration of a boolean?
A.boolean b1 = 0;
B. boolean b2 = 'false';
C. boolean b3 = false;
D.boolean b4 = Boolean.false();
E. boolean b5 = no;
37. What is the numerical range of a char?
A.-128 to 127
C. 0 to 32767
B. -(215) to (215) - 1
D.0 to 65535
38. public class Test { }
What is the prototype of the default constructor?
A.Test( )
B. Test(void)
C. public Test( )
D.public Test(void)
39. What will be the output of the program?
class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}
A.ok
B. dokey
C. ok dokey
D.No output is produced
E. Compilation error
40. What will be the output of the program?
class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}
void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}
A.7 7
C. 14 0
41. What will be the output of the program?
class Two
{
byte x;
}
B. 7 14
D.14 14
class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
Two fix(Two tt)
{
tt.x = 42;
return tt;
}
}
A.null null 42
C. 0 42 42
B. 0 0 42
D.0 0 0
42. What will be the output of the program?
class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;
void set(boolean [] x, int i)
{
x[i] = true;
++count;
}
public static void main(String [] args)
{
BoolArray ba = new BoolArray();
ba.set(ba.b, 0);
ba.set(ba.b, 2);
ba.test();
}
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}
A.count = 0
C. count = 3
B. count = 2
D.count = 4
43. What will be the output of the program?
public class Test
{
public static void leftshift(int i, int j)
{
i <<= j;
}
public static void main(String args[])
{
int i = 4, j = 2;
leftshift(i, j);
System.out.printIn(i);
}
}
A.2
C. 8
B. 4
D.16
Explain with Short Answers
1. Can a main() method be overloaded? Explain
2. Can a main() method be declared final?
3. Which package is imported by default?
4. What is the access scope of a protected method?
5. What is the purpose of declaring a variable as final?
6. What is the impact of declaring a method as final?
7. I don't want my class to be inherited by any other class. What should i do?
8. What is the importance of static variable?
9. What is an Abstract Class and what is it's purpose?
10. Can a abstract class be declared final?
11. What is use of a abstract variable?
12. Can you create an object of an abstract class?
13. Can a abstract class be defined without any abstract methods?
14. Class C implements Interface I containing method m1 and m2 declarations. Class C
has provided implementation for method m2. Can i create an object of Class C?
15. Can a method inside a Interface be declared as final?
16. Can an Interface implement another Interface?
17. Can a Class extend more than one Class?
18. What is a local, member and a class variable?
19. What is an abstract method?
20. What is the return type of a program's main() method?
21. If a variable is declared as private, where may the variable be accessed?
22. Is null a keyword?
23. Is the ternary operator written x : y ? z or x ? y : z ?
24. Does a class inherit the constructors of its superclass?
25. What are the legal operands of the instanceof operator?
26. To what value is a variable of the boolean type automatically initialized?
27. What restrictions are placed on method overriding?
28. What is the difference between an if statement and a switch statement?
29. What is the difference between the Boolean & operator and the && operator?
30. To what value is a variable of the String type automatically initialized?
31. How are this() and super() used with constructors?
32. What is a transient variable?
33. What is the difference between the >> and >>> operators?
34. Is sizeof a keyword?
Write a program for the following:
35. Show two ways to concatenate the following two strings together to get the string
"Hi, mom.":
String hi = "Hi, ";
String mom = "mom.";
36. Write a program that computes your initials from your full name and displays them.
37. An anagram is a word or a phrase made by transposing the letters of another word
or phrase; for example, "parliament" is an anagram of "partial men," and "software"
is an anagram of "swear oft." Write a program that figures out whether one string is
an anagram of another string. The program should ignore white space and
punctuation.
Download