1) Will this work?
public static void main(String args[])
{
RuntimeException re;
throw re;
}
Ans -> No , compile time exception :: variable re may not have been initialized
-------------------------------------------------------------------------------------------------------------
2) NumberFormatException is a subclass of IOException
-------------------------------------------------------------------------------------------------------------
3) if ("string".toUpperCase() =="STRING")
{
System.out.println("Yes");
}
else System.out.println("No");
Ans -> No // if we use .equals() here , then we get “yes” . This is because , “string” and “STRING” are two separate object references pointing to two separate objects. == is used to compare whether two object references point to same object or not. .equals() compares the objects for their contents.
-------------------------------------------------------------------------------------------------------------------
3) when we extend a class , the static class variables are also passed on to the child class. Final class variables are not passed. Transient class variables are passes( they are implicitly initialized ).
--------------------------------------------------------------------------------------------------------------------
4) We cannot extend a math class ( its final) . all its constants and methods are static.
---------------------------------------------------------------------------------------------------------------------
5) We cannot instantiate a math class .. its constructors are private.
---------------------------------------------------------------------------------------------------------------------
5) class XTC {
public static void main ( String [ ] ka ) {
int s = 64 / 9 ;
float f = 64 / 9 ;
double d = 64 / 9 ;
System . out . println ( s + " & " + f + " & " + d ) ;
}
};
Ans -> 7 , 7.0 , 7.0
---------------------------------------------------------------------------------------------------------------------
6) What is the output of the following ? Will it compile at all ?
class XTC {
public static void main ( String [ ] ka ) {
Integer b = new Integer(3) ;
System . out . println ( b instanceof Integer ) ;
}
};
Ans -> true
-----------------------------------------------------------------------------------------------------------------------
7) What is the output of the following ? Will it compile at all ?
class XTC {
public static void main ( String [ ] ka ) {
Integer b = null;
System . out . println ( b instanceof Integer ) ;
}
};
Ans -> false
--------------------------------------------------------------------------------------------------------------------
8) What is the output of the following ? Will it compile at all ?
class XTC {
public static void main ( String [ ] ka ) {
Integer b = null;
System . out . println ( b instanceof Object ) ;
}
};
Ans -> false
------------------------------------------------------------------------------------------------------------
9) What is the output of the following ? Will it compile at all ?
class XTC {
public static void main ( String [ ] ka ) {
Integer b = new Integer(3);
System . out . println ( b instanceof Object ) ;
}
};
Ans -> true
------------------------------------------------------------------------------------------------------------------------
10) if (Double . POSITIVE_INFINITY == Double . POSITIVE_INFINITY )
{
System . out . println ( “Yes”) ;
}
Ans -> Yes
--------------------------------------------**********************---------------------------------------------
Saturday, December 22, 2007
Mock Questions for Test Preparation
Posted by
javaexpert
at
5:30 AM
0
comments
Labels: Mock Question and Answers
Monday, December 17, 2007
SCJP Tutorial -- Exception Handling
Exception Handling 
Defination :- An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
There are 3 main advantages for exceptions:
1. Separates error handling code from "regular" code
2. Propagating errors up the call stack (without tedious programming)
3. Grouping error types and error differentiation
Syntax :-
try {
int y =5;
for(int i =5; i<=0; i++)
{ int z = x/i; }
}catch(Exception e1)
{ system.out.println( "Exception Occured" +e.printstacktrace());
}
finally {
System.out.println("Fional code is always executed");
}
Important tricks to be Noted :-
" An exception causes a jump to
the end of try block. If the exception occurred in a method called from a try
block, the called method is abandoned.
******************************************************************************************************
" If there's a catch block for
the occurred exception or a parent class of the exception, the exception is now
considered handled.*******************************************************************************************************
" At least one 'catch' block or one 'finally' block
must accompany a 'try' statement. If all 3 blocks are present, the order is
important. (try/catch/finally)*******************************************************************************************************
" finally and catch can come only with
try, they cannot appear on their own.****************************************************************************************************
" Regardless of whether or not an
exception occurred or whether or not it was handled, if there is a finally
block, it'll be executed always. (Even if there is a return statement in try
block).**************************************************************************************************
" System.exit() and error conditions are the only exceptions
where finally block is not executed.****************************************************************************************************
" If there was no exception or the
exception was handled, execution continues at the statement after the
try/catch/finally blocks.****************************************************************************************************
" If the exception is not handled, the process
repeats looking for next enclosing try block up the call hierarchy. If this
search reaches the top level of the hierarchy (the point at which the thread was
created), then the thread is killed and message stack trace is dumped to
System.err.****************************************************************************************************
" Use throw new xxxException() to throw an exception. If the
thrown object is null, a NullPointerException will be thrown at the handler.****************************************************************************************************
" If an exception handler re-throws an exception (throw in a catch
block), same rules apply. Either you need to have a try/catch within the catch
or specify the entire method as throwing the exception that's being re-thrown in
the catch block. Catch blocks at the same level will not handle the exceptions
thrown in a catch block - it needs its own handlers.****************************************************************************************************
" The method
fillInStackTrace() in Throwable class throws a Throwable object. It will be
useful when re-throwing an exception or error.****************************************************************************************************
" The Java language
requires that methods either catch or specify all checked exceptions that can be
thrown within the scope of that method.****************************************************************************************************
" All objects of type
java.lang.Exception are checked exceptions. (Except the classes under
java.lang.RuntimeException) If any method that contains lines of code that might
throw checked exceptions, compiler checks whether you've handled the exceptions
or you've declared the methods as throwing the exceptions. Hence the name checked exceptions.****************************************************************************************************
" If there's no
code in try block that may throw exceptions specified in the catch blocks,
compiler will produce an error. (This is not the case for super-class Exception)****************************************************************************************************
" Java.lang.RuntimeException and java.lang.Error need not be handled or
declared.****************************************************************************************************
Exception-->ClassNotFoundException, ClassNotSupportedException, IllegalAccessException, InstantiationException, IterruptedException, NoSuchMethodException, RuntimeException, AWTException, IOException
RuntimeException-->EmptyStackException, NoSuchElementException, ArithmeticException, ArrayStoreException, ClassCastException, IllegalArgumentException, IllegalMonitorStateException,
IndexOutOfBoundsException, NegativeArraySizeException, NullPointerException, SecurityException.
IllegalArgumentException-->IllegalThreadStateException, NumberFormatException
IndexOutOfBoundsException-->ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException
IOException-->EOFException, FileNotFoundException, InterruptedIOException, UTFDataFormatException, MalformedURLException, ProtocolException, SockException, UnknownHostException, UnknownServiceException.
Posted by
javaexpert
at
9:48 PM
1 comments
Thursday, December 13, 2007
Notes on Java Fundamentals
Basic Lexical elements----
--->keywords & other reserved words
*******************************************************************
1. Access modifiers - private,public,protected
2.other modifiers---abstract,final,static,synchronized,transient, volatile
3 Name space keywords -- import,package
*******************************************************************
byte,short,int,long-- signed two's complement integers
boolean -- 1 bit
char-- 16 bit unicode characters rather than ASCII set used in C
********************************************************************
Formula ------2^(n-1) to 2^(n-1)-1`
********************************************************************
Exceptional Floating point constants---Float.NAN (NAN Stands for Not A Number)
Float.NEGATIVE_INFINITY
Float.POSITIVE_INFINITY
Double.NAN
Double.NEGATIVE_INFINITY
Double.POSITIVE_INFINITY
************************************************************************
char represented in single quotes -
'\u0000'--- followed by four hexadecimal
************************************************************************
Integral can be decimal, hexadecimal or octal
Hexadecimal starts with 0x
Octals start with 0
************************************************************************
Floating point e or E stands for exponential value
eg 7e3 =7000
*************************************************************************
note--> double x=77; is valid declaration as doublex =77D;
*************************************************************************
To convert boolean, characters,integers,floating point numbers to String use valueOf() method
String is not null terminated.
**************************************************************************
Once initialized string objects are inmutable i.e cannot change their contents. so use stringBuffer if the contents have to be changed.
****************************************************************************
String bounds checked at run time rather than compile time.
string abc ="sdefg";
is not same as
string abc =new("string");
if we use first one compiler scans current cache to check for another string object with same literal value.
If it is there it reuses same object.
********************************************************************************
Posted by
javaexpert
at
11:18 PM
1 comments
Labels: Java Fundamentals
Friday, December 7, 2007
Books for SCJP preparation
The following are good books that can be refered for the SCJP 1.5 Exam ;-
1. A Programmer's guide to Java Certification by Khalid A Mughal
2. SCJP Exam for J2SE 5 - A Concise and Comprehensive Study Guide for the Sun Certified Java Programmer Exam by Paul Sanghera
3. Complete Java 2 certification Study Guide by Philip Heller
4. SCJP 5 book by Katherine sierra and Berth Bates
Other Resources Over Internet :-
Posted by
javaexpert
at
12:47 AM
2
comments
Tuesday, November 27, 2007
Important link for notes SCJP 1.5(310-055)
- The following are some links that can be used for your preparation SCJP 5.0
This link is really useful http://java.boot.by/scjp-tiger/
It covers in depth all the exam objectives and notes for CX-310-055 (Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 by sun).
1. Declarations, Initialization and Scoping
2. Flow Control
3. API Contents
4. Concurrency
5. OO Concepts
6. Collections / Generics
7. Fundamentals
Another useful link is
http://www.javabeat.net/javabeat/scjp5/index.php
Here you can find lot of practice questions for preparation.
http://www.javabeat.net/javabeat/scjp5/index.php : it includes articles about many topics that are in SCJP 1.5
http://www.examulator.com/home/tigertamer.htm :TigerTamerDB Mock Exam Questions for the JDK1.5
(Java 5) Programmer Exam
http://www.examulator.com.com/phezam/question.php : A fresh SCJP 1.5 Question every day
http://www.javabeat.net/javabeat/scjp5/mocks/index.php : avaBeat: 2 exams with 25 questions in total
http://www.whizlabs.com/scjp/scjp-5.0-details.html : WhizLabs Free Demo (3.94MB Download)
http://www.eJavaGuru.com/scjp5freemockexam.php
http://www.javablackbelt.com/
Posted by
javaexpert
at
1:01 AM
0
comments
Labels: SCJP 1.5 Syllabus
Thursday, November 8, 2007
Topic --Inner Classes
Following are important points to be noted about Inner Classes :-
1. An Inner class defined local to a block should not be static
2. An inner class can extend any class.
3. An inner class cannot declare static variables unless the inner class is itself static.
4. An inner class can be a sub class of the outer class.
5. An inner class cannot have the same name as its outer class.
6. An anonymous inner class is always assumed to directly extend Object. // false
7. An anonymous inner class can implement more than one interface using a list separated by commas //false
8. Methods of non-static inner classes can directly access any member of any enclosing class. Static and non-static inner classes cab have any accessibility
9. Non-static inner classes cannot define any static variables.
10. Multiple objects of inner classes can be associated with an object of an enclosing class.
11. If a class is derived from a base class , and the derived class is also an inner class in another class. If the derived class and the container class contain the same variable, the conflict occurs. In that the inherited member shadows the other.
12. Local class can access members defined within the class.
Please post all your queries about inner clases here.
Posted by
javaexpert
at
10:30 PM
0
comments
Labels: Inner Classes
Wednesday, November 7, 2007
Get Certified by Sun and Prove Your Knowledge
Hi Friends,
This is the first time I am writing for any blog, Please bear with me.
The purpose of this blog is to help my fellow professionals, who wish to take SCJP 1.5 (310-055) Exam by Sun.
I wish to help you all, by giving you all the Tips and Tricks that might help you when you take this exam
I will be posting tips and updating this blog on daily basis.
Please feel free to post your queries and comments. I will try my best to explain you.
Best of Luck !!!
Posted by
javaexpert
at
11:18 PM
3
comments