Text Box: Share on
Facebook
Google+
Linked in
Email
Copyright © 3S Technologies

Email: info@3s-technologies.com

Best viewed in Internet Explorer ver. 9.0 - 1024 X 768 resolution.

Java Objectives - Part 4

The reason for documenting this topic is to provide important technical questions and answers on Java language.† These questions can appear in an aptitude test, organized by different recruiting IT organizations.

 

Some questions and answers on Inheritance:

 

1) What is the difference between superclass & subclass?

Ans: A super class is a class that is inherited whereas subclass is a class that does the inheriting.

 

2) Which keyword is used to inherit a class?

Ans: extends

 

3) Subclasses methods can access superclass members/ attributes at all times. True/False ?

Ans: False

4) When can subclasses not access superclass members?

Ans: When superclass is declared as private.

 

5) Which class does begin Java class hierarchy?

Ans: Object class

 

6) Object class is a superclass of all other classes. True/False?

Ans: True

 

7) Java supports multiple inheritance. True/False ?

Ans: False

 

8) What is inheritance?

Ans: Deriving an object from an existing class. In the other words, Inheritance is the process of inheriting all the features from a class.

 

9) What are the advantages of inheritance?

Ans: Reusability of code and accessibility of variables and methods of the superclass by subclasses.

 

10) Which method is used to call the constructors of the superclass from the subclass?

Ans:† super(argument)

 

11) Which is used to execute any method of the superclass from the subclass?

Ans: super.method-name(arguments)

 

12) Which methods are used to destroy the objects created by the constructor methods?

Ans: finalize()

 

13) What are abstract classes?

Ans: Abstract classes are those for which instances canít be created.

 

14) What must a class do to implement an interface?

Ans: It must provide all of the methods in the interface and identify the interface in its implements clause.

 

15) Which methods in the Object class are declared as final?

Ans: getClass(), notify(), notifyAll(), and wait()

 

16) Final methods can be overridden. True/False ?

Ans: False

 

17) Declaration of methods as final results in faster execution of the program. True/False ?

Ans: True

 

18) Final variables should be declared in the beginning. True/False ?

Ans: True

 

19) Can we declare variable inside a method as final variables? Why?

Ans: Cannot because, local variable cannot be declared as final variables.

 

20) Can an abstract class may be final?

Ans: An abstract class may not be declared as final.

 

21) Does a class inherit the constructors of it's super class?

Ans: A class does not inherit constructors from any of it's super classes.

 

22) What restrictions are placed on method overloading?

Ans: Two methods may not have the same name and argument list but different return types.

 

23) What restrictions are placed on method overriding?

Ans: Overridden methods must have the same name , argument list , and return type. The overriding method may not limit the access of the method it overridees.The overriding method may not throw any exceptions that may not be thrown by the overridden method.

 

24) What modifiers may be used with an inner class that is a member of an outer class?

Ans: a (non-local) inner class may be declared as public, protected, private, static, final or abstract.

 

25) How this() is used with constructors?

Ans: this() is used to invoke a constructor of the same class.

 

26) How super() used with constructors?

Ans: super() is used to invoke a super class constructor.

 

27) Which of the following statements correctly describes an interface?

a) It's a concrete class

b) It's a superclass

c) It's a type of abstract class

Ans: c

 

28) An interface contains __ methods

a) Non-abstract

b) Implemented

c) unimplemented

Ans: c

Some question and answers on String Handling:

 

1) Which package defines String and StringBuffer classes?

Ans: java.lang package.

 

2) Which method can be used to obtain the length of the String?

Ans: length( ) method.

 

3) How do you concatenate Strings?

Ans: By using " + " operator.

 

4) Which method can be used to compare two strings for equality?

Ans: equals( ) method.

 

5) Which method can be used to perform a comparison between strings that ignores case differences?

Ans: equalsIgnoreCase( ) method.

 

6) What is the use of valueOf( ) method?

Ans: valueOf( ) method converts data from its internal format into a human-readable form.

 

7) What are the uses of toLowerCase( ) and toUpperCase( ) methods?

Ans: The method toLowerCase( ) converts all the characters in a string from uppercase to

lowercase.

The method toUpperCase( ) converts all the characters in a string from lowercase to

uppercase.

 

8) Which method can be used to find out the total allocated capacity of a StrinBuffer?

Ans: capacity( ) method.

 

9) Which method can be used to set the length of the buffer within a StringBuffer object?

Ans: setLength( ).

 

10) What is the difference between String and StringBuffer?

Ans: String objects are constants, whereas StringBuffer objects are not.

String class supports constant strings, whereas StringBuffer class supports growable, modifiable strings.

 

11) What are wrapper classes?

Ans: Wrapper classes are classes that allow primitive types to be accessed as objects.

 

12) Which of the following is not a wrapper class?

a) String

b) Integer

c) Boolean

d) Character

Ans: a.

 

13) What is the output of the following program?

public class Question {

public static void main(String args[]) {

String s1 = "abc";

String s2 = "def";

String s3 = s1.concat(s2.toUpperCase( ) );

System.out.println(s1+s2+s3);

}

}

 

a) abcdefabcdef

b) abcabcDEFDEF

c) abcdefabcDEF

d) None of the above

Ans: c.

 

14) Which of the following methods are methods of the String class?

a) delete( )

b) append( )

c) reverse( )

d) replace( )

Ans: d.

 

15) Which of the following methods cause the String object referenced by s to be changed?

a) s.concat( )

b) s.toUpperCase( )

c) s.replace( )

d) s.valueOf( )

Ans: a and b.

 

16) String is a wrapper class.

a) True

b) False

Ans: b.

 

17) If you run the code below, what gets printed out?

String s=new String("Bicycle");

int iBegin=1;

char iEnd=3;

System.out.println(s.substring(iBegin,iEnd));

 

a) Bic

b) ic

c) icy

d) error: no method matching substring(int,char)

Ans: b.

 

18) Given the following declarations

String s1=new String("Hello")

String s2=new String("there");

String s3=new String();

 

Which of the following are legal operations?

a) s3=s1 + s2;

b) s3=s1 - s2;

c) s3=s1 & s2

d) s3=s1 && s2

Ans: a.

 

19) Which of the following statements are true?

a)The String class is implemented as a char array, elements are addressed using the stringname[] convention

b) Strings are a primitive type in Java that overloads the + operator for concatenation

c) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type

d) The size of a string can be retrieved using the length property.

Ans: b.

 

Some question and answers on Java.Lang package:

 

1) java.lang package is automatically imported into all programs.

a) True

b) False

Ans : a

 

2) What are the interfaces defined by java.lang?

Ans : Cloneable, Comparable and Runnable.

 

3) What are the constants defined by both Flaot and Double classes?

Ans : MAX_VALUE,

MIN_VALUE,

NaN,

POSITIVE_INFINITY,

NEGATIVE_INFINITY and

TYPE.

 

4) What are the constants defined by Byte, Short, Integer and Long?

Ans : MAX_VALUE,

MIN_VALUE and

TYPE.

 

5) What are the constants defined by both Float and Double classes?

Ans : MAX_RADIX,

MIN_RADIX,

MAX_VALUE,

MIN_VALUE and

TYPE.

 

6) What is the purpose of the Runtime class?

Ans : The purpose of the Runtime class is to provide access to the Java runtime system.

 

7) What is the purpose of the System class?

Ans : The purpose of the System class is to provide access to system resources.

 

8) Which class is extended by all other classes?

Ans : Object class is extended by all other classes.

 

9) Which class can be used to obtain design information about an object?

Ans : The Class class can be used to obtain information about an objectís design.

 

10) Which method is used to calculate the absolute value of a number?

Ans : abs( ) method.

 

11) What are E and PI?

Ans : E is the base of the natural logarithm and PI is the mathematical value pi.

 

12) Which of the following classes is used to perform basic console I/O?

a) System

b) SecurityManager

c) Math

d) Runtime

Ans : a

 

13) Which of the following are true?

a) The Class class is the superclass of the Object class.

b) The Object class is final.

c) The Class class can be used to load other classes.

d) The ClassLoader class can be used to load other classes.

Ans : c and d

 

14) Which of the following methods are methods of the Math class?

a) absolute( )

b) log( )

c) cosine( )

d) sine( )

Ans : b

 

15) Which of the following are true about the Error and Exception classes?

a) Both classes extend Throwable.

b) The Error class is final and the Exception class is not.

c) The Exception class is final and the Error is not.

d) Both classes implement Throwable.

Ans : a

 

16) Which of the following are true?

a) The Void class extends the Class class.

b) The Float class extends the Double class.

c) The System class extends the Runtime class.

d) The Integer class extends the Number class.

Ans : d

 

17) Which of the following will output -4.0 ?

a) System.out.println(Math.floor(-4.7));

b) System.out.println(Math.round(-4.7));

c) System.out.println(Math.ceil(-4.7));

d) System.out.println(Math.Min(-4.7));

Ans : c

 

18) Which of the following are valid statements ?

a) public class MyCalc extends Math

b) Math.max(s);

c) Math.round(9.99,1);

d) Math.mod(4,10);

e) None of the above.

Ans : e

 

19) What will happen if you attempt to compile and run the following code?

Integer ten=new Integer(10);

Long nine=new Long (9);

System.out.println(ten + nine);

int i=1;

System.out.println(i + ten);

 

a) 19 followed by 20

b) 19 followed by 11

c) Error: Can't convert java lang Integer

d) 10 followed by 1

Ans : c

 

3S Technologies

Online Education for all