- You need a boolean in the if condition.
- An overriding method only needs to declare that it can throw a subset of the exceptions the overriden method can throw. Having no throws clause in the overriding method is OK.
- Normal execution will not resume if an exception is uncaught by a method. The exception will propagate up the invocation stack until some method handles it. If no one handles it then the that thread will terminate.
- The expression (a = b) does not compare the variables a and b, but rather assigns the value of b to the variable a. The result of the expression is the value being assigned. Since a and b are boolean variables, the value returned by the expression is also boolean. This allows the expressions to be used as the condition for an if-statement.
- “if-clause and the else-clause can have empty statements. Empty statement ( ie. just ; ) is a valid statement.
But this is illegal :
if (true) else;
because the if part doesn’t contain any valid statement. ( A statement cannot start with an else!)
So, the following is valid.
if(true) if(false);
because if(false); is a valid statement.
“
|
Java™ Application Development on Linux® – Free 599 Page eBook |
Enabling Rapid ROI: With Java™ – Based Business Intelligence Applications: |
||||
- Read the questions carefully. This is very important. Questions are easy but you need to read them carefully.
- The type of the switch expression (ie. in switch( i ), the type of i )must be char, byte, short, or int, or a compile-time error occurs.
- The body of a switch statement must be a block. Any statement immediately contained by the block may be labeled with one or more case or default labels. These labels are said to be associated with the switch statement, as are the values of the constant expressions in the case labels.
- “Every case constant expression associated with a switch statement must be assignable to the type of the switch
Expression. ie. if the switch expression is of type byte then all the case constants must fit in a byte.(eg. you can’t you 200 as a case value)” - No two of the case constant expressions associated with a switch statement may have the same value.
- At most, one default label may be associated with the same switch statement.
- “public class TestClass
{
public static void main(String args[])
{
Exception e = null;
throw e;
}
}”
You are throwing an exception and there is no try catch block or a throws clause. So it will not compile. If you do that (ie. either put a try catch block or declare a throws clause) then at run time it will throw a null pointer exception as e is null.
A method that throws an exception either must declare it in throws clause or put the code that throws the exception in try/catch block. - Every case constant expression in a switch block must be assignable to the type of switch expression. Meaning :
“byte by = 10;
switch(by)
{
200 : //some code;
300 : //some code;
}
This will not compile as 300 is not assignable to ‘by ‘ which can only hold values from -128 to 127.
“
The use of break keyword is not mandatory, and without it the control will simply fall through the labels of the switch statement. - “A break without a label breaks the current loop (ie. no iterations any more) and a break with a label tries to pass the control to the given label.
‘Tries to’ means that if the break is in a try block and the try block has a finally clause associated with it then it will be executed.” - U can apply a label to any block but not like loop: int x=0;
- “This is the hierarchy:
java.lang.Object
|
+–java.lang.Throwable
|
+–java.lang.Exception, java.lang.Error - So the method can even give Throwable ( but not Object !) in throws statement”
- With a try, either of (multiple) catch and (only one) finally or both can occur. finally must occur only in the end. A try MUST be followed by atleast a catch or finally.
- The break statement is to break out of any loop completely. So the current iteration and any other remaining iterations of the loop will not happen. Control is transferred to the first statement below the loop.
- Name of the file is not passes in the args
- if finally is throwing an Null pointer exception which is RuntimeException so there is no need to handle it or declare it in the throws clause.
- It is not possible to break out of an if statement. But if the if statement is placed within a labeled block or a switch statement or a loop construct, the usage of break in option 3 would be valid.
- for (int i=5; i=0; i–) { }
uses ‘=’ instead of ‘==’ for condition which is invalid. The loop condition must be of type boolean - int j=5;
for(int i=0, j+=5; i uses ‘j +=5′. Now, this statement is preceeded by ‘int i=0,’ and that means we are trying to declare variable j . But it is already declared before the for loop. If we remove the int in the initialization part and declare i before the loop then it will work.But if we remove the statement int j = 5; it will not work because compiler will try to do j = j+5 and you can’t use the variable before it is initialized. Although the compiler gives a message ‘Invalid declaration’ for j += 5 but it really means the above mentioned thing. - “int i, j;
for (j=10; iThrowable – > Exception -> MyException.” - You can declare anything that is a Throwable or a subclass of Throwable, in the throws clause.
- “while (false) { x=3; } in a compile-time error because the statement x=3; is not reachable;
but the superficially similar case:
if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as “”unreachable”" in the technical sense .
The rationale for this differing treatment is to allow programmers to define “”flag variables”" such as: - static final boolean DEBUG = false;
- and then write code such as:
if (DEBUG) { x=3; }
The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text. This ability to “”conditionally compile”" has a significant impact on, and relationship to, binary compatibility . If a set of classes that use such a “”flag”" variable are compiled and conditional code is omitted, it does not suffice later to distribute just a new version of the class or interface that contains the definition of the flag. A change to the value of a flag is, therefore, not binary compatible with preexisting binaries. - This is valid : for( int i = 0; i< 0; i++) x = 3; but this is not : for( int i = 0; false; i++) x = 3;
- “
- “The Exception that is thrown in the last, gets thrown by the method.
So, When no exception or any exception is thrown at line 1, the control goes to finally or some catch block. Now, even if the catch blocks throw some exception, the control goes to finally. The finally block throws CloneNotSupportedException, so the whole method ends up throwing CloneNotSupportedException.” - “All the arrays are initialized to contain the default values of their type. This means,
int[] iA = new int[10]; will contain 10 ints having 0.
Object[] oA = new Object[10]; will contain 10 object references pointing to null.
boolean[] bA = new boolean[10] will contain 10 booleans of value ‘false’.
So, as bA[0] is false, the if condition fails and str remains 111.” - “If no args are given a Zero length string array is received in the main method. So, there is no NullPointerException on accessing args even if no args are given.
Indexing in java starts from zero. so the last element will be at args.length -1.” - Runnable r=new Thread(); is allowed since Thread implements runnable interface
- To make a class abstract, you only need to mark it as abstract as in option4. You don’t neccessorily need to put an abstract method.
- java and classname are not part of the args array.
- String is not a keyword , it’s a normal Class
- “You cannot have two methods with the same name in one class.
Also, even if you put one sayHello() method in other class which is a subclass of this class, it won’t compile because you cannot override/hide a static method with a non static method and vice versa.” - Objects are created from class definitions that implement abstractions. The objects that are created are concrete realizations of those abstractions.
- “Note that Arrays are Objects (ie. cA instanceof Object is true) so are passed by reference. So in m1() the change in cA[1] done by m2() is reflected everywhere the array is used.
c is a primitive type and is passed by value. In method m2() the passed parameter c is different than instance variable ‘c’ as local variable hides the instance variable. So instance member ‘c’ keeps it’s default (i.e 0) value.
“ - Note that both equals() and hashcode() methods can be overridden by the programmer so you can’t say how they work.
- “public class TestClass
{
public static void main(String args[ ] )
{
Object a, b, c ;
a = new String(“”A”");
b = new String(“”B”");
c = a;
a = b;
System.out.println(“”"”+c);
}
}”
“The variables a, b and c contain references to actual objects. Assigning to a refrence only changes the refrence value, and not the object pointed to by it. So, when c = a is executed c starts pointing to the string object containing A. and when a = b is executed, a starts pointing to the string object containing B but the object containing A still remains same and c still points to it. So the program prints A and not B. - For the class that does not define toString() method, the inherited toString() method ( from java.lang.Object) is called. This method returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of :
getClass().getName() + ‘@’ + Integer.toHexString(hashCode()) - But in this case, String class overrides the toString() method that returns the actual string.
“ - “Theoratically, java supports Pass by Value for everything ( ie. primitives as well as Objects).
. Primitives are always passed by value.
. Object “”references”" are passed by value. So it looks like the object is passed by reference but actually it’s the value of the reference that is passed. - A crude example:
Object o1 = new Object(); //say, the object is stored at memory location 15000. So o1 actually contains 15000. - now, when you call someMethod(o1); the value 15000 is passed.
- Inside the method someMethod
someMethod( Object localVar)
{
……localVar contains 15000 so, whatever method you call or modication you do to this varibale, it is done on the original object. But when you try to change it’s value, for eg. make it null, it then contains 000000 (say). But the original variable o1 still contains 15000 so it still points to the same object.
} - In the above question, the local variable is what is changed the original variable ie.someInt remains unchanged.
“ - “The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
- getClass().getName() + ‘@’ + Integer.toHexString(hashCode())”
- There no unsigned keyword in java!
- “Java implements abstractions using classes that have properties and behavior. Behavior is dictated by the operations of the abstraction. In Java the operations are defined in classes using methods.
“ - “All member fields (static and non-static) are initialized to their default values.
Default values are: objects are initilized to null (String is also an object), numeric types to 0 (or 0.0 ) and boolean to false.
“ - Note that if no argument is passed the args parameter is NOT null but a valid non-null string array of length zero.
- “As the question says, “”…an instance of the class is not needed…”", the method has to be static.
Also, as the question does not say that other packages should not have access to the method so public or protected is also correct. But it is better to choose option 2 as it is most appropriate.
Also as you are asked to select only 1 option,it is better to select option 2 other wise you can select other options also.” - “Non-static inner classes can contain final static fields (but not methods).
Anonymous classes cannot have explict constructors, since they have no names. - A static inner class is also known as A Top Level Nested class. So,there are two types of Top level classes. One, that is a standard class and other an inner class which is static. Eg.
public class A //This is a standard Top Level class.
{ - class X
{
static final int j = 10; //compiles fine!
}
public static class B //This is also a Top Level class (but nested!)
{
}
}
You can create objects of B with having objects of A. Eg. A.B b = new A.B(); - Members in outer instances are directly accessible using simple names. There is no restriction that member variables in inner classes must be final.
Nested classes define distinct types from the enclosing class, and the instanceof operator does not take of the outer instance into consideration.” - Altough nonsensical, an empty file is a valid source file. A source file can contain optional package declaration, any number of import statments and any number of class and interface definitions.(only one class/interface in a file can be public though.)
- An instance member belongs to a single instance, not the class as a whole. An instance member is a member variable or a member method that belongs to a specific object instance. All non-static members are instance members.
- “The following is a valid code…
- abstract class SomeClass { public abstract void m1(); }
public class TestClass
{
public static SomeClass getSomeClass() //note static
{
return new SomeClass()
{
public void m1() { }
};
}
} - Another important point to note here is : Contrary to common belief, Anonymous class declared/defined/instantiated in a static method is NOT static. In fact, you can verify it by doing: javap TestClass$1
Other interesting FAQs related Posts on Java, you may like:
Java Notes – 1 (clean-clouds.com)
Java Notes – 2 (clean-clouds.com)
Java Notes – 3 (clean-clouds.com)
Java Notes -4 (clean-clouds.com)
Java Notes -5 (clean-clouds.com)
Java Notes -6 (clean-clouds.com)
Frequently Asked Questions in Java Part- 1 (clean-clouds.com)
Frequently Asked Questions in Java Part- 2 (clean-clouds.com)
Frequently Asked Questions in Java Part-3 (clean-clouds.com)
Frequently Asked Questions in Java Part-4 (clean-clouds.com)
Frequently Asked Questions in Java Part-5 (clean-clouds.com)
Frequently Asked Questions in Java Part-6 (clean-clouds.com)
Frequently Asked Questions in Java Part-7 (clean-clouds.com)
Frequently Asked Questions in Java Part-8 (clean-clouds.com)
Frequently Asked Questions in Java Part-9 (clean-clouds.com)
Frequently Asked Questions in Java Part-10 (clean-clouds.com)
Frequently Asked Questions in Java Part-11 (clean-clouds.com)
Frequently Asked Questions in Java Part-12 (clean-clouds.com)
Frequently Asked Questions in Java Part-13 (clean-clouds.com)
Frequently Asked Questions in Java Part-14 (clean-clouds.com)
Related articles
- Java Notes – 1 (clean-clouds.com)
- Frequently Asked Questions in Java Part-2 (clean-clouds.com)
- Frequently Asked Questions in Java Part-1 (clean-clouds.com)
- Exceptions in Java (clean-clouds.com)
- Operators, Expressions and Control Structures in Java (clean-clouds.com)
- Creating, Compiling and Running a Simple program in Java (clean-clouds.com)












