Core Java Questions
Which non-Unicode letter characters may be used as the first character of an identifier?
$ and _
What value does read() return when it has reached the end of a file?
-1.
What is an Abstract Class and what is it's purpose?
A class that doesn't have complete implementation is abstract. It enforces abstraction.
What is the purpose of declaring a variable as final?
A final variable's value can't be changed. final variables should be initialized before using them.
What is a Marker Interface?
A marker interface has no declaration inside but still enforces a mechanism.
What is the impact of declaring a method as final?
A method declared as final can't be overridden.
What is the difference between a static and a non-static inner class?
A non-static inner class can have object instances that are associated with instances outside of the class's outer class. A static inner class does not have any object instances.
What is a package?
A package has similar classes and interfaces and should be the first thing that is declared in a class.
What is a pointer and does Java support pointers?
A pointer is a reference handle to memory location. If improperly used it can lead to memory leak and reliability issues so Java does not support them.
What is the access scope of a protected method?
A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.
What are the restriction imposed on a static method or a static block of code?
A static method should not refer to instance variables without creating an instance and cannot use "this" operator to refer the instance.
What is the importance of static variable?
A static variable is a class level variable where all objects of the class refers to the same variable. If one object changes the value then all objects will be changed as well.
What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass.
What is the arguement of main() method?
An array of String Object.
Why is it that an interface can extend more than one interface but a class can't extend more than one class?
Because java does not support multiple inheritance so a class is restricted in extending one class. But an interface is a pure abstraction model and doesn't have inheritance hierarchy such as a class. Therefore an interface can extend more than one interface.
Why does Java not support operator overloading?
Because operator overloading makes the code difficult to read and maintain.
What is difference between Path and Classpath?
Both are operating system level environment variables, and Path is used to locate .exe executable files and ClassPath is used to locate .class files.
What is Downcasting ?
Downcasting is the casting from a general type to a specific type.
What is the difference between extends and implements?
Extends is used to extend a class whereas implements is used to implement an interface.
What is Externalizable?
Externalizable is an interface that extends the serializable interface and sends data into streams in a compressed format. There is readExternal and writeExternal.
What are instance variables?
Instance variable are those which are declared on a class level. It does not have to be initialized before being used because it is automatically initialized to its default value.
What is the return type of the main() method?
It has no return type because it is "void".
Why is the main() method declared static?
It is declared static because JVM called the main method before any instantiation of the class.
What is the % operator?
It is referred to as modulo or remainder operator.
What do you mean by platform independence?
It means that you can write and compile Java code in one platform, and execute the class in another supported platform.
What is the difference between a JDK and a JVM?
JDK is Java Development Kit which has a development purpose and a execution environment. JVM is a run time environment hence it does compile source files.
What is a JVM?
JVM is Java Virtual Machine, it is a run time environment for compiled java class files.
What is the most important feature of Java?
Java is a platform independent language.
What is the base class of all classes?
Java.lang.Object
What are local variables?
Local variables are those which are declared in a block of code like a method. It must be initialized before it is used.
Can a Byte object be cast to a double value?
No an object cannot be cast to a primitive value.
Can a abstract class be declared final?
No because an abstract class without being inherited is no use and will result in a compile time error.
Is Java a pure object oriented language?
No because there are primitive data types in Java.
Can a Class extend more than one Class?
No, a class can only extend one class but can have many interfaces.
Does the order of public and static declaration matter in main() method?
No, as long as the void is before the main().
Can an Interface implement another Interface?
No, because an interface does not have an implementation.
Are arrays primitive data types?
No, in Java they are Objects.
Are JVM's platform independent?
No, it is dependent on the run time implementation provided by the vendor.
Should a main() method be compulsorily declared in all java classes?
No, it should only be declared if the source class is a java application.
Can an Interface be final?
No, it will cause a compilation error.
Can a class be declared as protected?
No, not for classes nor interfaces. But it can be used for methods and fields as long as they are not in an interface.
Can a method inside a Interface be declared as final?
No, only the modifiers public and abstract can be used to declare a method inside a Interface.
Can we define private and protected modifiers for variables in interfaces?
No.
Does Java support multiple inheritance?
No.
Can you create an object of an abstract class?
No. Abstract classes can't be instantiated.
Can a class declared as private be accessed outside it's package?
Not possible.
If a variable is declared as private, where may the variable be accessed?
Only within the class it is declared.
Which object oriented Concept is achieved by using overloading and overriding?
Polymorphism.
I want to print "Hello" even before main() is executed. How will you acheive that?
Print the statement inside a static block of code.
What modifiers may be used with an inner class that is a member of an outer class?
Public, private, protected, static, final, or abstract.
What does it mean that a method or field is "static"?
Static methods and fields are instantiated once per class. In other words, they are class variables, not instance variables. If you change the value of a static variable of an object, then it changes for all instances of the class. Static methods can be referenced by the name of the class instead of the name of the object.
Can we declare a static variable inside a method?
Static variables are class level variables so they can't be declared inside a method. If they are the class will not compile.
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.
Which class is extended by all other classes?
The object class.
What is casting?
There are two types of casting, first one is casting between primitive numeric types and second one is casting between object references. Casting between numeric types is used to convert larger values to smaller values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.
What do you understand by private, protected and public?
They are accessibility modifiers. Private is the most restricted whereas public is the least restricted. Protected allows visibility to a derived class in a different package.
What restrictions are placed on method overloading?
Two method cannot have the same name or arguments, but different return types.
How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
Unicode requires 16 ASCII requires 7 but uses 8 UTF-8 uses 8, 16, 18 bit patterns UTF-16 uses 16 and larger bit patterns
What is use of an abstract variable?
Variables can't be declared abstract, only classes and methods can be declared abstract.
What is a local, member and a class variable?
Variables that are declared within a method are "local" variables. Variable declared within the same class, but not in any method is called a "member" or "global" variable. Variable declared within the class, but not in any method and are defined as static are called "class" variables.
Can a class be declared as static?
We can not declare top level class as static, but only inner class can be declared static.
How to define a constant variable in Java?
We use the keywords "final" and "static" so that it is the only copy that exists in the instances of the class. And so that the value does not change.
When will you define a method as static?
When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static.
Can a class be defined inside an Interface?
Yes it is possible.
Can an Interface be defined inside a class?
Yes it is possible.
Can an Interface extend another Interface?
Yes, an Interface can inherit another interface.
Can a main() method be declared final?
Yes, but inheriting class won't be able to have their own default main() method.
Can a source file contain more than one class declaration?
Yes, it can have any number of class declarations but only one can be public.
Can a main() method be overloaded?
Yes, we can have many number of main() methods with different method signature and implementation of the class.
Can a abstract class be defined without any abstract methods?
Yes.
I don't want my class to be inherited by any other class. What should i do?
You should declared your class as final. But you can't define your class as final, if it is an abstract class. A class declared as final can't be extended by any other class.
What is a native method?
a method that is implemented in another language in java.
what is inheritance and abstraction and encapsulation in java?
inheritance lets you create classes that are derived from other classes. You do abstraction when deciding what to implement. You do encapsulation when hiding something that you have implemented.
Which package is imported by default?
java.lang package
Can you give few examples of final classes defined in Java API?
java.lang.String, java.lang.Math are final classes.
What modifiers are allowed for methods in an Interface?
public and abstract.