Athipathy Language Fundamentals 1
Athipathy Language Fundamentals 1
Language Fundamentals
1. 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
2. List primitive data types, there size and there range (min, max)
2
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
3
10.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
memory that is no longer accessible from any live thread it takes steps to release it back
to the heap for reuse
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
5
Modifiers
21. 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
23. 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
29.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
6
32. When are the static variables loaded into the memory
During the class load time
33. When are the non static variables loaded into the memory
They are loaded just before the constructor is called
Computer.harddisk
Computer.compute()
And this code is executed exactly once at the time of class load
42. What are the four general cases for Conversion and Casting
Conversion of primitives
Casting of primitives
Conversion of object references
Casting of object references
44.What are the rules for primitive assignment and method call conversion
boolean
47.What are the rules for object reference assignment and method call
conversion
An interface type can only be converted to an interface type or to object. If the new type
is an interface, it must be a superinterface of the old type
A class type can be converted to a class type or to an interface type. If converting to a
class type the new type should be superclass of the old type. If converting to an
interface type new type the old class must implement the interface
An array maybe converted to class object, to the interface cloneable, or to an array.
Only an array of object references types may be converted to an array, and the old
element type must be convertible to the new element
When both Oldtypes and Newtypes are classes, one should be subclass of the other
When both Oldtype ad Newtype are arrays, both arrays must contain reference types
(not primitive), and it must be legal to cast an element of Oldtype to an element of
Newtype
Runtime rules
If Newtype is a class. The class of the expression being converted must be Newtype or
must inherit from Newtype
If NewType is an interface, the class of the expression being converted must implement
Newtype
Do while loop walways executes the body of the loop at least once, since the test is
performed at the end of the body
50.When do you use continue and when do you use break statements
When continue statement is applied it prematurely completes the iteration of a loop.
When break statement is applied it causes the entire loop to be abandoned.
51.What is the base class from which all exceptions are subclasses
All exceptions are subclasses of a class called java.lang.Throwable
Use of system.exit()
11
63. Is it legal for the extending class which overrides a method which throws an
exception, not o throw in the overridden class
Yes it is perfectly legal
Exception class. This allows custom exceptions to be generated (using throw) and
caught in the same way as normal exceptions.
Example:
class myCustomException extends Exception {
// The class simply has to exist to be an exception
}
69.If the method to be overridden has access type protected, can subclass have
the access type as private
No, it must have access type as protected or public, since an overriding method must
not be less accessible than the method it overrides
72.If you use super() or this() in a constructor where should it appear in the
constructor
It should always be the first statement in the constructor
75.Can objects that are instances of inner class access the members of the outer
class
Yes they can access the members of the outer class
76.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
Threads
Where does java thread support reside
It resides in three places
The java.lang.Thread class (Most of the support resides here)
The java.lang.Object class
The java language and virtual machine
What happens when you call the start() method of the thread
This registers the thread with a piece of system code called thread scheduler
The schedulers determines which thread is actually running
Define your run method. Pass this object as an argument to the Thread constructor
Which way would you prefer to implement threading , by extending Thread class
or implementing Runnable interface
The preferred way will be to use Interface Runnable, because by subclassing the
Thread class you have single inheritance i.e you wont be able to extend any other class
Does the thread method start executing as soon as the sleep time is over
No, after the specified time is over the thread enters into ready state and will only
execute when the scheduler allows it to do so.
Synchronize a subset of a method by surrounding the desired lines of code with curly
brackets and inserting the synchronized expression before the opening curly. This
17
allows you to synchronize the block on the lock of any object at all, not necessarily the
object that owns the code
Using notify () method how you can specify which thread should be notified
You cannot specify which thread is to be notified, hence it is always better to call
notifyAll() method
What would you use to compare two String variables - the operator == or the
method equals()?
I'd use the method equals() to compare the values of the Strings and the == to check if
two variables point at the same instance of a String object
What will be the result if you compare StringBuffer with String if both have same
values
It will return false as you cannot compare String with StringBuffer
Arrays are fast to access, but are inefficient if the number of elements grow and if you
have to insert or delete an element
In the above command line Hello is the class, Tom is the first argument and Jerry is the
second argument.
Does the order of public and static declaration matter in main method?
No it doesn't matter but void should always come before main().
If a source file has 2 class declaration in it, on compilation how many class files
will be created?
2 class files will be generated by the compiler.
Can the source file name and the class name in the file be different?
If the class in the source is not of public access, then the name can be anything but
should confirm to identifier rules.
21
System.out.println("Hi");;;
The statement will compile and execute without any problem. Semicolon is just a
statement terminator.