7

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?

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • 5
    -1 Man... here I was expecting interesting questions... I don't think this is a good question (e.g. the information is easily researchable and testable and likely incredibly duplicative) -- however, I hope that some good detailed replies come out of it. –  Mar 05 '11 at 17:23
  • 8
    Given that you now have access to a compiler/run-time & Google, what do **you** think the answers are? – Andrew Thompson Mar 05 '11 at 17:24

8 Answers8

17

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?

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?

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • Sometimes the answer size is small,but to get that content take a lot of research and effort.This is the type of answer which takes a lot of time to search and you did it successfully.Thank you Very nice answer @Mark Longair – zeeshan Mar 06 '17 at 12:36
3

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.

Community
  • 1
  • 1
Alex Nikolaenkov
  • 2,505
  • 20
  • 27
3

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.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

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.

Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
0

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

Community
  • 1
  • 1
Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
0

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

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
0

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • `System.exit` is not guaranteed to run. It can be cancelled by the security manager, and more generally, anything that involves a method call can fail if the stack is within one frame of overflowing. – Mike Samuel Mar 05 '11 at 17:32
  • Not the vanilla case, but you're certainly correct. Thanks for the education, Mike. – duffymo Mar 05 '11 at 17:35
0

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)
        ...
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    this is not size of object, but size of object in serialized form. I think these values are more or less valid only for DTO and other transport objects, not for complex ones. It has not much about how much memory object use. – Andrey Mar 05 '11 at 17:57