Core Java Study guide and interview questions - Part 3

This is the 3rd part of the 500+ Core JAVA interview questions and answers. These questions can also be used as a quick study guide for preparing for an interview or for any Core JAVA concepts revision. These questions cover all the topics in core JAVA from basics to advanced concepts. 

1. What is a Java package and how is it used?
Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.

2. What is the difference between the >> and >>> operators?
The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.

3. What restrictions are placed on method overriding?
Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method.

4. To what value is a variable of the boolean type automatically initialized?
The default value of the boolean type is false.

5. Can an abstract class be final?
An abstract class may not be declared as final.

6. What modifiers may be used with a top-level class?
Top-level class may be public, abstract, or final.

7. What is constructor chaining and how is it achieved in Java ?
child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.

8. How are this() and super() used with constructors?
this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.

9. What is the difference between the prefix and postfix forms of the ++ operator?
The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value.

10. What is the difference between a public and a non-public class?
 A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

11. What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. import javnet.* versus import javnet.Socket)?
It makes no difference in the generated class files since only the classes that are actually used are referenced by the generated class file. There is another practical benefit to importing single classes, and this arises when two (or more) packages have classes with the same name. Take javutil.Timer and javax.swing.Timer, for example. If I import javutil.* and javax.swing.* and then try to use "Timer", I get an error while compiling (the class name is ambiguous between both packages). Let's say what you really wanted was thejavax.swing.Timer class, and the only classes you plan on using in javutil are Collection and HashMap. In this case, some people will prefer to import javutil.Collection and import javutil.HashMapinstead of importing javutil.*. This will now allow them to use Timer, Collection, HashMap, and otherjavax.swing classes without using fully qualified class names in.

12. How many static initializers can you have ?
As many as you want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope.

13. Can a double value be cast to a byte?
Yes, a double value can be cast to a byte.

14. To what value is a variable of the String type automatically initialized?
The default value of an String type is null.

15. How does Java handle integer overflows and underflows?
It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

16. What is the difference between method overriding and overloading?
Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments

17. What is the difference between a field variable and a local variable?
A field variable is a variable that is declared as a member of a class. A local variable is a variable that is declared local to a method.

18. What is the difference between the Boolean & operator and the && operator?
If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the & operator is applied to the operand. When an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.

19. Can a method be overloaded based on different return type but same argument type ?
No, because the methods can be called without using their return type in which case there is ambiquity for the compiler.

20. What is the difference between a break statement and a continue statement?
A break statement results in the termination of the statement to which it applies (switch, for, do, or while). Acontinue statement is used to end the current loop iteration and return control to the loop statement.

21. What is the difference between an if statement and a switch statement?
The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.

22. What does it mean that a class or member is final?
 final class cannot be inherited. A final method cannot be overridden in a subclass. A final field cannot be changed after it's initialized, and it must include an initializer statement where it's declared.

23. Can a for statement loop indefinitely?
Yes, a for statement can loop indefinitely. For example, consider the following: for(;;);

24. What happens to a static variable that is defined within a method of a class ?
Can't do it. You'll get a compilation error.

25. What is numeric promotion?
Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.

26. What is a transient variable?
Transient variable is a variable that may not be serialized.

27. Which Java operator is right associative?
The = operator is right associative.

28. Is size of a keyword?
The sizeof operator is not a keyword.

29. What does it mean that a method or class is abstract?
An abstract class cannot be instantiated. Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do. Each subclass of an abstract class must override the abstract methods of its superclasses or it also should be declared abstract.

30.Can we define private and protected modifiers for variables in interfaces?
 No

31.What are some alternatives to inheritance?
 Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).

32.What is synchronization and why is it important?
 With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.

33. What is method overloading?
If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.

34.What does it mean that a method or field is “static”?
 Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That’s how library methods like System.out.println() work. out is a static field in the javlang.System class.

35.Is null a keyword?
 The null value is not a keyword.

36.Why method overloading is not possible by changing the return type in java?
Because of ambiguity....

37.What are wrapped classes?
 Wrapped classes are classes that allow primitive types to be accessed as objects.

38.Why do threads block on I/O?
Threads block on i/o (that is enters the waiting state) so that other threads may execute while the I/O operation is performed.

39.What restrictions are placed on the location of a package statement within a source code file?
 A package statement must appear as the first line in a source code file (excluding blank lines and comments).

40.Why isn’t there operator overloading?
 Because C++ has proven by example that operator overloading makes code almost impossible to maintain. In fact there very nearly wasn’t even method overloading in Java, but it was thought that this was too useful for some very basic methods like print(). Note that some of the classes like DataOutputStream have unoverloaded methods like writeInt() and writeByte().

41.What modifiers are allowed for methods in an Interface?
 Only public and abstract modifiers are allowed for methods in interfaces.

42.What modifiers may be used with an inner class that is a member of an outer class?
 A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.

43.How do I convert a numeric IP address like 192.18.97.39 into a hostname like javsun.com?
String hostname = InetAddress.getByName("192.18.97.39").getHostName();

44.What are order of precedence and associativity, and how are they used?
 Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left

45.Which characters may be used as the second character of an identifier,but not as the first character of an identifier?
 The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.

46.What are the different identifier states of a Thread?
The different identifiers of a Thread are: R - Running or runnable thread, S - Suspended thread, CW - Thread waiting on a condition variable, MW - Thread waiting on a monitor lock, MS - Thread suspended waiting on a monitor lock

47.What is the range of the char type?
 The range of the char type is 0 to 2^16 - 1.

48.What is the difference between preemptive scheduling and time slicing?
 Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

49.How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
 Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.

50.Can an anonymous class be declared as implementing an interface and extending a class?
An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

Next : 1000+ core JAVA quick study material & interview Questions and Answers

0 Comment to "Core Java Study guide and interview questions - Part 3"

Post a Comment