Understanding basic Java-specific interverview questions
Last day I faced an interview and they asked me java questions among which for some questions I didn't know the answer. I am curious to know the answer for that question. Interviewer did not tell me the answer. I am asking that questions here:
- Does Java provide any construct to find out the size of an object?
- Give a开发者_高级运维 simplest way to find out the time a method takes for execution without using any profiling tool?
- What are checked exceptions and runtime exceptions?
- If I write return at the end of the try block, will the finally block still execute?
- If I write System.exit (0); at the end of the try block, will the finally block still execute?
Want to know answers of above question so that it can help me next time.
Explanations, notes, and/or relevant links to the specification would be greatly appreciated over just the simple answers -- and what is a good way to learn this stuff?
I think that all of these can be answered by searching for existing Stack Overflow questions. The reason I think it's worth answering this question with links to previous answers is that you've asked about a lot of different issues, each one of which is interesting in its own right. It's not so likely you'll get an in-depth discussion of these when asking about them all together, but in the answers and comments on previous questions here you'll find a lot of detail and discussion which (a) may be interesting and (b) may help in further interviews. (There may well be better choices - these are just the first reasonable SO answers I found.)
Does Java provide any construct to find out the size of an object?
- In Java, what is the best way to determine the size of an object?
- sizeof java object
Give a simplest way to find out the time a method takes for execution without using any profiling tool?
- How do I time a method's execution in Java?
What are checked exceptions and runtime exceptions?
- Differences between Runtime/Checked/Unchecked/Error/Exception
If I write return at the end of the try block, will the finally block still execute?
- Does finally always execute in Java?
If I write System.exit (0); at the end of the try block, will the finally block still execute?
- How does Java's System.exit() work with try/catch/finally blocks?
Does Java provide any construct to find out the size of an object?
AFAIK no.
Give a simplest way to find out the time a method takes for execution without using any profiling tool?
Measure time with System.currentTimeMillis()
, System.nanoTime()
.
What are checked exceptions and runtime exceptions?
For detailed explanation check this thread.
If I write return at the end of the try block, will the finally block still execute?
yes.
If I write System.exit (0); at the end of the try block, will the finally block still execute?
no.
Does Java provide any construct to find out the size of an object?
No.
Give a simple way to find out the time a method takes for execution without using any profiling tool?
long t0 = System.nanoTime();
// code to profile
long timeTaken = System.nanoTime() - t0;
What are checked exceptions and runtime exceptions?
A checked exception is any Throwable
that does not extend from java.lang.Error
or java.lang.RuntimeException
. The java compiler will give you an error if you do not handle or explicitly propagate checked exceptions.
If I write return at the end of the try block, will the finally block still execute?
Yes
If I write System.exit (0); at the end of the try block, will the finally block still execute?
Maybe. If there is a SecurityManager
then it can throw a SecurityException
instead of exiting which will trigger the finally
block.
If your stack is too large, calling System.exit
might result in a stack overflow exception which would trigger the finally
block.
Does Java provide any construct to find out the size of an object?
There is no cross-platform/cross-VM way, as I know.
Give a simplest way to find out the time a method takes for execution without using any profiling tool?
by create new Date() before and after execution.
What are checked exceptions and runtime exceptions?
You should wrap checked exceptions into try/catch block
If I write return at the end of the try block, will the finally block still execute?
yes
If I write System.exit (0); at the end of the try block, will the finally block still execute?
No.
Does Java provide any construct to find out the size of an object?
Not really. You could create an object and note down the memory differences, or perhaps read the spec to see what an object is made up of.
Give a simplest way to find out the time a method takes for execution without using any profiling tool?
Like this:
long start = System.currentTimeMillis();
// do something
System.out.println("Time: " + (System.currentTimeMillis() - start));
What are checked exceptions and runtime exceptions?
Checked exception are exception that are declared in the throws
section of a routine. Similarly unchecked exctions are those which are not declared, e.g., subclasses of RuntimeException
.
If I write return at the end of the try block, will the finally block still execute?
Yup, at least on the sun/oracle jvm. Not pretty though ..
If I write System.exit (0); at the end of the try block, will the finally block still execute?
Nope
Does Java provide any construct to find out the size of an object?
No, it does not.
Give a simplest way to find out the time a method takes for execution without using any profiling tool?
long begTime = System.currentTimeInMillis();
x.method();
long endTime = System.currentTimeInMillis();
System.out.println("method running time: " + (endTime-begTime));
What are checked exceptions and runtime exceptions?
Checked exceptions must be caught, or the compiler will flag; runtime exceptions do not require catch blocks.
If I write return at the end of the try block, will the finally block still execute?
Yes.
If I write System.exit (0); at the end of the try block, will the finally block still execute?
No. http://download.oracle.com/javase/tutorial/essential/exceptions/finally.html
1.
no, not for objects generally. .length for arrays and strings...
2.
final long startTime = System.nanoTime();
final long endTime;
try {
methodToTime();
} finally {
endTime = System.nanoTime();
}
final long duration = endTime - startTime;
from How do I time a method's execution in Java?
3.
unchecked exceptions are bugs, see http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html
checked exceptions are for example, invalid user inputs, exceptions that can be handled.
4.
the finally block will still execute
5.
the finally block will not execute
The size of (serializable) objects can be found relatively easily.
import javax.swing.*;
import java.io.*;
class HowLongIsAPieceOfString {
public static int getObjectSize(Object object) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
return baos.toByteArray().length;
}
public static void main(String[] args) throws IOException {
Object object = "string";
System.out.println( "String size: \t" + getObjectSize(object) );
object = new JButton("Click Me!");
System.out.println( "Button size: \t" + getObjectSize(object) );
object = new JTree();
System.out.println( "Tree size: \t" + getObjectSize(object) );
object = new Object();
System.out.println( "Object size: \t" + getObjectSize(object) );
}
}
Output:
String size: 13
Button size: 4347
Tree size: 5628
Exception in thread "main" java.io.NotSerializableException: java.lang.Object
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
...
精彩评论