Skip to content

Latest commit

 

History

History
359 lines (251 loc) · 25.4 KB

File metadata and controls

359 lines (251 loc) · 25.4 KB

Java Basics

List questions:

  1. What is the purpose of garbage collection in Java, and when is it used?
  2. What is static in java?
  3. What is final?
  4. What are different type of inner classes?
  5. Can a top level class be private or protected?
  6. What is the different between declaring a variable and defining a variable?
  7. What type of parameter passing does Java support?
  8. Give a simplest way to find out the time a method takes for execution without using any profiling tool?
  9. Is Empty .java file a valid source file?
  10. Can a .java file contain more than one java classes?
  11. Is String a primitive data type in Java?
  12. What happens if you dont initialize an instance variable of any of the primitive types in Java?
  13. What are the different scopes for Java variables?
  14. What is the default value of the local variables?
  15. Does garbage collection guarantee that a program will not run out of memory?
  16. What is the purpose of finalization?
  17. Can a public class MyClass be defined in a source file named YourClass.java?
  18. What will be the output of the following statement? System.out.println ("1" ** + 3);
  19. What will be the default values of all the elements of an array defined as an instance variable?
  20. Length in bytes for primitive types
  21. Contract between equals() and hashCode()
  22. What's the difference between "a == b" and "a.equals(b)"?
  23. What different between StringBuffer and StringBuilder?
  24. What internal methods of String do you know?
  25. Purpose, types, and creation of nested classes?
  26. What does it mean that an object or a class is mutable or immutable?
  27. Is it enough to define this class as final? To make this class immutable?
  28. Besides “String” do you know any other immutable classes?
  29. Increasing/decreasing of methods visibility (inheritance)
  30. You need to create the string, which contains 1,000,000 random numbers, comma separated. How would you do that, considering performance?
  31. Garbage collection principle?
  32. How is the virtual space divided in Java?
  33. What difference between float and BigDecimal. How they store the data?
  34. What is deep copy of Java object?
  35. How and in what cases we need to configure size of memory areas in Java?
  36. What is an Object and how do you allocate memory to it?
  37. What are methods and how are they defined?
  38. What is casting?
  39. What is the difference between an argument and a parameter?
  40. What is final, finalize() and finally?
  41. What is UNICODE?
  42. What are Transient and Volatile Modifiers?
  43. What is a package?
  44. What is the difference between Integer and int?
  45. What is a cloneable interface and how many methods does it contain?
  46. Can you have an inner class inside a method and what variables can you access?
  47. What is the difference between static and non-static variables?
  48. How does Java handle integer overflows and underflows?
  49. What are polymorphism and inheritance?
  50. String literal and String object
  51. What does the keyword transient mean in Java?
  52. What does super keyword do?

Main methods:

  1. Can an application have multi classes having main method?
  2. Can I have multiple main methods in the same class?
  3. What if the main method is declared as private?
  4. What if the static modifier is removed from the signature of the main method?
  5. What if I do not provide the String array as the argument to the method?
  6. What is the first argument of the String array in main method?
  7. If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?
  8. Can main method be declared final?
  9. Can we make main synchronized in Java?
  10. How to call a nonstatic method from main in Java?
  11. Can we overload the main method in Java? Which main method JVM will call?
  12. Can we override the main method in Java?

  1. What is the purpose of garbage collection in Java, and when is it used?

GC in Java is the mechanism that keeps track of the memory and objects residing in the memory. GC collects the object when it is no longer needed (usually when no references to the object are available).

  1. What is static in java?

static is java language keyword.

  1. When used with a method defines a method of a class.

  2. When used with a field defines a class field.

  3. When used on an nested class declaration defines a static nested class.

  4. Can be used as a static initialization block.

  5. What is final?

final is Java Language keyword.

  1. When used with a method protects it from being overridden in subclasses. Done for security and/or performance reasons.

  2. When used with a field means that the value stored in the field cannon be changed after initialization. Not to be confused with immutability of the object.

  3. When used with a class declaration protects it from being subclassed. Done for security and/or performance reasons. Also for immutability. Many of Java core classes are final (e.g. String)

  4. What are different type of inner classes?
  • inner class: a "regular" inner class is declared inside the curly braces of another class, but outside any method or other code block.
  • method-local inner class: is defined within a method of the enclosing class.
  • anonymous inner class: have no name, and their type must be either a subclass of the name type or an implementer of the named interface.
  • static nested class: are inner class marked with the static modifier.
  1. Can a top level class be private or protected?

No, it is only allowed to be public or have to default access modifier.

  1. What is the different between declaring a variable and defining a variable?
  2. What type of parameter passing does Java support?

Java passed all parameters by values. The references to objects are passed by values.

  1. Give a simplest way to find out the time a method takes for execution without using any profiling tool?

System.currentTimeMillis() in the beginning and end of the method

  1. Is Empty .java file a valid source file?

Yes it is.

  1. Can a .java file contain more than one java classes?

Yes it can. It has to contain only one top level public java class but it can contain any number of inner, anonymous and top level classes with default access modifier.

  1. Is String a primitive data type in Java?

No. String is an Object. An immutable one.

  1. What happens if you dont initialize an instance variable of any of the primitive types in Java?

It gets assigned the default value. 0 for int and long, 0.0 for float and double, false for boolean. Though I tried to compile a class where variables were not initialized and it didn't compile.

  1. What are the different scopes for Java variables?

static fields, instance fields, method parameters, local variables

  1. What is the default value of the local variables?

No default value. Default values are assigned to instance fields. Local variables have to be explicitly initialized.

  1. Does garbage collection guarantee that a program will not run out of memory?

No.

  1. What is the purpose of finalization?

Free up the resources. (e.g. close connections and streams, release a lock etc)

  1. Can a public class MyClass be defined in a source file named YourClass.java?

No. Unless it is a nested class public class.

  1. What will be the output of the following statement? System.out.println ("1" + 3);

13

  1. What will be the default values of all the elements of an array defined as an instance variable?

All elements will be initialized to default value of corresponding type.

  1. Length in bytes for primitive types
    maybe virtual machine dependent.
Primitive type length in bytes Comment
boolean 1 bit saved as 4 bytes; 1 byte in an array
char 2 bytes unsigned
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
  1. Contract between equals() and hashCode()

hashCode() method returns an int hash value corresponding to an object. It's used in hash based collection classes e.g Hashtable, HashMap, LinkedHashMap and so on. It's very tightly related to equals() method. According to Java specification, two objects which are equal to each other using equals() method must have same hash code.

So, if a.equals(b) returns true then a.hashCode() == b.hashCode() is also true. Note that equal hashCode doesn't mean anything.

  1. What's the difference between "a == b" and "a.equals(b)"?

The a = b does object reference matching if both a and b are an object and only return true if both are pointing to the same object in the heap space, on the other hand, a.equals(b) is used for logical mapping and its expected from an object to override this method to provide logical equality. For example, String class overrides this equals() method so that you can compare two Strings, which are the different object but contains same letters.

  1. What different between StringBuffer and StringBuilder?

StringBuilder -- new. StringBuffer -- old. StringBuffer -- synchronized. Where possible use StringBuilder. Both represent mutable sequence of characters.

  1. What internal methods of String do you know?

static methods of String class: valueOf, indexOf, lastIndexOf, replace, contains, startsWith, endsWith, substring, matches, split, equals, isEmpty

  1. Purpose, types, and creation of nested classes?
  2. What does it mean that an object or a class is mutable or immutable?

Immutability: the state of the object doesn't change

  1. Is it enough to define this class as final? To make this class immutable?

No. If the class is declared final it only means that it cannot be subclassed.

If the instance of the class is declared to be final it only means that the reference will not change.

The inner state of the object in both cases can change.

  1. Besides “String” do you know any other immutable classes?
  2. Increasing/decreasing of methods visibility (inheritance)

The main rule is that visibility cannot be reduced in the subclass

References: stackoverflow

  1. You need to create the string, which contains 1,000,000 random numbers, comma separated. How would you do that, considering performance?

I would use StringBuilder class

  1. Garbage collection principle?

The garbage collector first performs a task called marking. The garbage collector traverses the application graph, starting with the root objects; those are objects that are represented by all active stack frames and all the static variables loaded into the system.

Each object the garbage collector meets is marked as being used, and will not be deleted in the sweeping stage. The sweeping stage is where the deletion of objects take place.

  1. How is the virtual space divided in Java?

stackoverflow

oracle_java6

  1. What difference between float and BigDecimal. How they store the data?

float is floating point number and can loose precision during the computations.

BigDeciamal is fixed point number. The computations (which type of computations?) are guaranteed to maintain the needed precision.

Internally BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale

If no rounding mode is specified and the exact result cannot be represented, an exception is thrown

  1. What is deep copy of Java object?

Deep copy creates a copy of the object including deep copies of all its fields.

  1. How and in what cases we need to configure size of memory areas in Java?

In case of getting OutOfMemoryError: Java heap space. What other cases?

JVM parameter -Xmx#####m where ##### is number of megabytes you need for the JVM.

-Xms#####m to set the initial heap size

More info on this topic can be found here: http://blog.codecentric.de/en/2011/03/java-memory-configuration-and-monitoring-3rd-act/

  1. What is an Object and how do you allocate memory to it?
  2. What are methods and how are they defined?
  3. What is casting?

changing the type of the object.

  1. What is the difference between an argument and a parameter?

parameter -- abstract. argument -- concrete value of the parameter.

parameters of the function are defined when the function is declared.

arguments of the function are defined when it is called.

  1. What is final, finalize() and finally?

final -- Java keyword, is a modifier which you can apply to variables, methods and classes. If you make a variable final it means its value cannot be changed once initialized.

finalize() -- is a method, gets called before the object is GC-ed, giving it last chance to resurrect itself, but the call to finalize is not guaranteed.

finally -- Java keyword used in exception handling along with try and catch. the finally block is always executed irrespective of whether an exception is thrown from try block or not

detail

  1. What is UNICODE?

See info on Unicode here http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Character.html

  1. What are Transient and Volatile Modifiers?

Transient signifies that the field is not part of the object state (e.g. it's some derieved value or some cache). Transient fields are not present in serialized representation of the object.

If field is declared with volatile keyword then any thread that reads the field will see the most recently written value [Effective Java Item 66]

  1. What is a package?

In Java package is a mechanism to oragnize classes into modules.

  1. What is the difference between Integer and int?

Integer is a wrapper class for int primitive type.

Integer can be used in generic collections whereas int cannot.

Also contains a number of utility methods.

  1. What is a cloneable interface and how many methods does it contain?

Cloneable -- is a marker interface and it doesn't contain any methods.

It determines the behavior of Object’s protected clone implementation: if a class implements Cloneable, Object’s clone method returns a field-by-field copy of the object; otherwise it throws CloneNotSupportedException

  1. Can you have an inner class inside a method and what variables can you access?

You can create a local or anonymous class inside the method. It can access only final variables.

  1. What is the difference between static and non-static variables?
  2. The way they are initialized. Static are initalized when the class is loaded. Non-static -- when it's instantiated.

  3. Non-static belong to the instance of an object while static are class variables.

  4. Static are accessed using ClassName.varName

  5. How does Java handle integer overflows and underflows?
  6. What are polymorphism and inheritance?

    Polymorphism and inheritance are two of the core idioms of object-oriented development.

    Polymorphism allows you to make a definition for a certain type of behavior, and have many differ- ent classes implement that behavior. Inheritance allows you to derive the behavior and de nition of a class from a superclass.

    inheritance; polymorphism

  7. String literal and String object

    Java by default doesn't put all String object into String pool, instead they gives you flexibility to explicitly store any arbitrary object in String pool. You can put any object to String pool by calling intern() method of java.lang.String class. Though, when you create using String literal notation of Java, it automatically call intern() method to put that object into String pool, provided it was not present in the pool already. This is another difference between string literal and new string, because in case of new, interning doesn't happen automatically, until you call intern() method on that object.

    String

    Difference between String literal and New String object in Java

  8. What does the keyword transient mean in Java?

    It marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred, they are lost intentionally.

  9. What does super keyword do?
    • super is used to refer immediate parent class instance variable.
    • super() is used to invoke immediate parent class constructor.
    • super is used to invoke immediate parent class method.

    detail

Main Method

  1. Can an application have multi classes having main method?

Yes, it can. But only one main method can be used as entrance point for the program.

  1. Can I have multiple main methods in the same class?

No, if you mean public static void main(String[] args).

Yes, if you mean a method with a name "main" and any other signatures.

  1. What if the main method is declared as private?

The class will compile but the method cannot be used as an entrance point.

  1. What if the static modifier is removed from the signature of the main method?

It become an instance method. No longer an entrance point but just a valid regular method.

  1. What if I do not provide the String array as the argument to the method?

You just define a static method called "main" with no parameters. It cannot be used as entrance point.

  1. What is the first argument of the String array in main method?

These are the parameters passed to the program from command line.

  1. If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?

Array of size 0

  1. Can main method be declared final?

Yes it can be declared as final

  1. Can we make main synchronized in Java?

    Yes, main can be synchronized in Java, synchronized modifier is allowed in the main signature and you can make your main method synchronized in Java.

  2. How to call a nonstatic method from main in Java?

    This question applies not only to main but all static methods in Java. Since nonstatic methods can not be called from static context directly, you need to first create an Object as local variable and then you can call nonstatic method using that object

  3. Can we overload the main method in Java? Which main method JVM will call?

    Yes you can overload the main method in Java, nothing wrong with this but Java will only call your specific main method, i.e. main method with the following signature: public static void main(String[] args) or public static void main(String args...) which is the main method as variable argument method and only supported post-Java 5 world

  4. Can we override the main method in Java?

    No, you can not override the main method in Java, Why? because main is a static method and in Java static method is bonded during compile time and you can not override static method in Java. If you declare a method with same name and signature its called method hiding.

Reference

svozniuk-repo