Language Fundamentals
How many number of non-public class definitions can a source file have
A source file can contain unlimited number of non-public class definitions
List primitive data types, there size and there range (min, max)
| Data Type | Bytes | bits | min | max |
| boolean | - | 1 | - | - |
| char | 2 | 16 | 0 | 2^16-1 |
| byte | 1 | 8 | -2^7 | 2^7-1 |
| short | 2 | 16 | -2^15 | 2^15-1 |
| int | 4 | 32 | -2^31 | 2^31-1 |
| long | 8 | 64 | -2^63 | 2^63-1 |
| float | 4 | 32 | - | - |
| double | 8 | 64 | - | - |
What types of values does boolean variables take
It only takes values true and false
Which primitive datatypes are signed
All except char and Boolean
Is char type signed or unsigned
char type is integral but unsigned. It range is 0 to 2^7-1
What forms an integral literal can be
decimal, octal and hexadecimal, hence example it can be 28, 034 and 0x1c respectively
What is the default value of Boolean
False
Why is the main method static
So that it can be invoked without creating an instance of that class
What is the difference between class variable, member variable and automatic(local) variable
class variable is a static variable and does not belong to instance of class but rather shared across all the instances member variable belongs to a particular instance of class and can be called from any method of the class automatic or local variable is created on entry to a method and has only method scope
When are static and non static variables of the class initialized
The static variables are initialized when the class is loadedNon static variables are initialized just before the constructor is called
When are automatic variable initialized
Automatic variable have to be initialized explicitly
How is an argument passed in java, by copy or by reference
If the variable is primitive datatype then it is passed by copy. If the variable is an object then it is passed by reference
What is garbage collection
The runtime system keeps track of the memory that is allocated and is able to determine whether that memory is still useable. This work is usually done in background by a low-priority thread that is referred to as garbage collector. When the gc finds memory that is no longer accessible from any live thread it takes steps to release it back to the heap for reuse
Does System.gc and Runtime.gc() guarantee garbage collection
No
Operators and assignment
What are different types of operators in java
Uniary ++, –, +, -, |, ~, () Arithmetic *, /, %,+, – Shift <<, >>, >>> Comparison =, instanceof, = =,!=Bitwise &, ^, |Short Circuit &&, ||Ternary ?:Assignment =
How does bitwise (~) operator work
It converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s, e.g 11110000 coverts to 00001111
What is a modulo operator %
This operator gives the value which is related to the remainder of a divisione.g x=7%4 gives remainder 3 as an answer
Can shift operators be applied to float types.
No, shift operators can be applied only to integer or long types
What happens to the bits that fall off after shifting
They are discarded
What values of the bits are shifted in after the shift
In case of signed left shift >> the new bits are set to zero But in case of signed right shift it takes the value of most significant bit before the shift, that is if the most significant bit before shift is 0 it will introduce 0, else if it is 1, it will introduce 1
Modifiers
What are access modifiers
These public, protected and private, these can be applied to class, variables, constructors and methods. But if you don’t specify an access modifier then it is considered as Friendly
Can protected or friendly features be accessed from different packages
No when features are friendly or protected they can be accessed from all the classes in that package but not from classes in another package
How can you access protected features from another package
You can access protected features from other classes by subclassing the that class in another package, but this cannot be done for friendly features
What are the rules for overriding
Private method can be overridden by private, friendly, protected or public methods Friendly method can be overridden by friendly, protected or public methods Protected method can be overridden by protected or public methods Public method can be overridden by public method
Explain modifier final
Final can be applied to classes, methods and variables and the features cannot be changed. Final class cannot be subclassed, methods cannot be overridden
Can you change the reference of the final object
No the reference cannot be change, but the data in that object can be changed
Can abstract modifier be applied to a variable
No it is applied only to class and methods
Can abstract class be instantiated
No abstract class cannot be instantiated i.e you cannot create a new object of this class
When does the compiler insist that the class must be abstract
If one or more methods of the class are abstract. If class inherits one or more abstract methods from the parent abstract class and no implementation is provided for that method If class implements an interface and provides no implementation for those methods
How is abstract class different from final class
Abstract class must be subclassed and final class cannot be subclassed
Where can static modifiers be used
They can be applied to variables, methods and even a block of code, static methods and variables are not associated with any instance of class
When are the static variables loaded into the memory
During the class load time When are the non static variables loaded into the memory They are loaded just before the constructor is called
How can you reference static variables
Via reference to any instance of the class
Computer comp = new Computer ();
comp.harddisk where hardisk is a static variable
comp.compute() where compute is a method
Via the class name
Computer.harddisk
Computer.compute()
Can static method use non static features of there class No they are not allowed to use non static features of the class, they can only call static methods and can use static data
What is static initializer code
A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g.
public class Demo{
static int =10;
static{
System.out.println(“Hello world’);
}
}
And this code is executed exactly once at the time of class load
Where is native modifier used
It can refer only to methods and it indicates that the body of the method is to be found else where and it is usually written in non java language
What are transient variables
A transient variable is not stored as part of objects persistent state and they cannot be final or static
What is synchronized modifier used for
It is used to control access of critical code in multithreaded programs
What are volatile variables
It indicates that these variables can be modified asynchronously
Incoming search terms:
- How many number of non-public class definitions can a source file have (3)
- ajax interview questions (1)
- non-stop marquee no gap (1)
- pageflip4 encryption (1)
- plugin marquee without gap (1)
- spring mvc drop down onchange= (1)
- symfony2 getuniqid (1)
- system gc() vs runtime gx() (1)
- vtiger cobro/pago (1)
- non stop without gap marquee (1)
- non stop marquee html no gap (1)
- jquerymobile pass parameters to popup (1)
- jquery mobile flip toggle switch programming example (1)
- java interview tutorials (1)
- java interview questions 0 (1)
- java interview questions (1)
- flip toggle switches in mvc example (1)
- examples for copy constructor in c# (1)
- why char takes 2 bytes in java (1)

