Java Material: Placement Preparations

Java (programming language)Image via Wikipedia

hi friends these are some faq's on java programming as per collected from various years and companies papers . hope this will you in your preparations..


Note: All the programs are tested under JDK
1.3 Java compiler.
1. class ArrayCopy{
public static void main(String[] args){
int ia1[] = { 1, 2 };
int ia2[] = (int[])ia1.clone();
System.out.print((ia1 == ia2) + " ");
ia1[1]++;
System.out.println(ia2[1]);
}
}
Answer:
false 2
Explanation:
The clone function creates a new object with a copy of the original object. The == operator compares for checking if the both refer to the same object and returns false (a boolean value) because they are different objects. When concatenated with a string it prints ‗false‘ instead of 0.
Since ia1 and ia2 are two different array objects the change in the values stored in ia1 array object doesn‘t affect the object ia2.
2. import javautil.StringTokenizer;
class STTest {
public static void main(String args[]) {
String s = "9 23 45.4 56.7";
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens())
System.out.println(st.nextToken());
}
}
Answer: 9
23
45.4
56.7
Explanation:
The StringTokenizer parses the given string to return the individual tokens. Here the String ‗s‘ has four white-spaces that act as a separators, resulting in the printing of those individual tokens.
3. class ConvertTest {
public static void main (String args[]){
String str;
str = "25";
int i = Integer.valueOf(str).intValue();
System.out.println(i);
str = "25.6";
double d = Double.valueOf(str).doubleValue();
System.out.println(d);
}
}
Answer:
25 25.6
Explanation:
This program just explains how the static member funntions of the classes Integer and Double can be used to convert the string values that have numbers to the get primitive data-type values.
4. class StaticTest {
public static void main(String[] args) {
int i = getX();
}
public int getX() {
return 3;
}
}
Answer:
Compiler Error : Cannot access a non-static member
Explanation:
The static method, main(), belongs to the class. However,getX() belongs to an object in the class. The compiler doesn't know on which object it's invoking the getX() method.There are a couple of ways around this problem. You could declare that getX() as static; that is:
public static int getX()
Alternately, you can instantiate an object in the StaticTest class in the main() method and invoke that object's getX() method, like this:
public static void main(String[] args) {
StaticTest st = new StaticTest();
int i = st.getX();
}
5. class Test {
public static void main(String[] args) {
String s1 = new String("Hello World");
String s2 = new String("Hello World");
if (s1 == s2)
System.out.println("The strings are the same.");
else
System.out.println("The strings are different.");
}
}
Answer:
The strings are different.
Explanation:
When used on objects, == tests whether the two objects are the same object, not whether they have the same value.
To compare two objects for equality, rather than identity, you should use the equals() method.
6. class Test {
public static void main(String[] args) {
String s1 = "Hello World";
String s2 = "Hello World";
if (s1 == s2)
System.out.println("The strings are the same");
else
System.out.println("The strings are different");
}
}
Answer:
The strings are the same.
Explanation:
Note that these two are string literals and not Strings. The compiler recognizes that the two string literals have the same value and it performs a simple optimization of only creating one String object. Thus s1 and s2 both refer to the same object and are therefore equal. The Java Language Specification requires this behavior. However, not all compilers get this right so in practice this behavior here is implementation dependent.
7. public class works{
public static int some;
static {
some = 100;
System.out.println("Inside static");
}
public static void main( String args[] ) {
new works();
System.out.println( "Inside main" );
}
works() {
System.out.println( "some = " + some );
}
}
Answer:
Inside static
some = 100
Inside main
Explanation:
Static blocks are executed before the invocation of main(). So at first the ―Inside static‖ is printed. After that the main function is called. It creates the object of the same type. So it leads to the printing of ‗some = 100‘. Finally the println inside the main() is executed to print ‗Inside main‘.
8. public class func{
int g(){
System.out.println("inside g");
int h(){
System.out.println("inside h");
return 1;
}
return 0;
}
public static void main(String[] args){
int c;
c=g();
}
Answer:
error : ";" expected at - int h()
Explanation:
Java doesn‘t allow function declared within a function declaration (nested functions). Hence the error.
9. When an exceptional condition causes an exception to be thrown, that exception is an object derived, either directly, or indirectly from the class Exception: True or False?
Answer:
False.
Explanation:
When an exceptional condition causes an exception to be thrown, that exception is an object derived, either directly, or indirectly from the class Throwable
10. class Test {
public static void main(String[] args) {
Button b;
b.setText("Hello");
}
}
Answer:
Runtime Error : NullPointerException
Explanation:
A NullPointerException is thrown when the system tries to access a object that points to a null value (objects are initalised to null).
This code tries to call setText() on ‗b‘. ‗b‘ does not refrence any object, so the exception is thrown. You must allocate space for the reference to point to an object like this:
Button b = new Button("hello");
// Or
Button b;
b = new Button();
b.setText("hello");
This creates a reference (‗b‘) of type Button, then asssigns it to a new instance of Button (new Button("hello")).
11. class Test {
void Test() {
System.out.println("Testing") ;
}
public static void main(String argv[]) {
Test ex = new Test() ;
}
}
Answer:
Compiler Error : ‗void‘ before Test()
Explanation:
Constructor doesn‘t have any return type; so even void shouldn't be specified as return type.
12. class Test {
int some=10 ;
void Test() {
this(some++) ;
}
void Test(int i) {
System.out.println(some);
}
public static void main( String argv[] ) {
new Test();
}
}
Answer:
Compiler Error : Cannot use ‗this‘ inside the constructor
Explanation:
‗this‘ is a special one that it refers to the same object. But ‗this‘ can be used only after creation of the object. It can‘t be used within a constructor. This leads to the issue of the error.
16. class Test {
public int some;
public static void main(String argv[]) {
int i = new Test().some;
System.out.println(i) ;
}
}
Answer:
0
Explanation:
All member variables are initialised during creation of the object. In the statement:
int i = new Test().some;
a new object of type ‗Test‘ is created and the value of member ‗j‘ in that object is referenced (the object created is not assigned to any reference) unig the ‗.‘ (dot) operator.
13. What does this code do?
int f = 1+ (int)(Math.random()*6);
This code always assigns an integer to variable f in the range between 1 and 6.
14. All programs can be written in terms of three types of control structures: What are those three?
Sequence,selection and repetition.
15. Is !(x=y) in Java ?
In the case that x and y are of float or double, the value of x or y could be NaN and the results would be different in that case.
16. What are peer "classes"?
Peer classes exist mainly for the convenience of the people who wrote the Java environment. They help in translating between the AWT user interface and the native (Windows, OpenWindows, Mac etc.) interfaces. Unless you're porting Java to a new platform you shouldn't have to use them.
17. What are concrete classes?
The classes from which objects are instantiated are called concrete classes (as opposed to abstract classes from which objects cannot be created).
18. Which methods in Java are implcitly treated as final methods?
Methods that are declared static and that are declared as private.
19. Java's finally block provides a mechanism that allows your method to clean up after itself regardless of what happens within the try block. True or False?
True.
20. Explain why you should place exception handlers furthermost from the root of the exception hierarchy tree first in the list of exception handlers.
An exception hander designed to handle a specialized "leaf" object may be preempted by another handler whose exception object type is closer to the root of the exception hierarchy tree if the second exception handler appears earlier in the list of exception handlers.
21. What method of which class would you use to extract the message from an exception object?
The getMessage() method of the Throwable class.
22. What are"deprecated APIs"?
A deprecated API is a object or method that is not recommended to be used and is an indication that it may be removed from the API list in future. The programs that use such APIs still work fine but not recommended to be used for this reason.


Reblog this post [with Zemanta]

0 comments:

Post a Comment