Java Class Loader
Java Class Loader
1
Prepared by: Imdad Areeph
11. Can we declare a local variable with in static block, like below?
public class TestVariable {
static{
int i=9;
System.out.println("print "+i);
}
yes
2
Prepared by: Imdad Areeph
15. If I am writing static and non static block in my program like below, what is output?
public class TestBlock {
static{
{
System.out.println("I am in non static block");
}
public static void main(String a[]){
TestBlock block=new TestBlock();
}
}
Ans: I am in static block
I am in non static block
static{
{
System.out.println("I am in non static block");
}
static{
3
Prepared by: Imdad Areeph
static{
{
System.out.println("I am in non static block");
}
public TestBlock() {
System.out.println("I am in non argumented constructor");
}
public TestBlock(String s) {
System.out.println("I am in argumented constructor "+s);
}
}
}
I am in static1 block
I am in non static block
I am in argumented constructor hello
18. Can I use instance variable in static variable like below?
public class TestBlock {
int z=98;
static{
System.out.println("I am in static block"+new TestBlock().z);
}
public static void main(String a[]){
TestBlock block=new TestBlock();
}
}
yes, you can execute this program, out put will
I am in static block98
4
Prepared by: Imdad Areeph
}
public class B extends A{
public void testB(){
System.out.println("Class B");
}
}
What will be output for following questions
5
Prepared by: Imdad Areeph
}
Ans run time error. A can not cast to B. Super class object can not cast to sub class.
5. In the above example can we write like this
public class Test {
public static void main(String args[]){
B b= new B();
A a =(A)b;
a.testA();
}
}
Ans yes out put will "Class A"
6. In the above example can I write like this
public class Test {
public static void main(String args[]){
A a =new B();
a.testA();
}}
yes, Super class reference can point to subclass Object.
7. In the above example can I write like this
public class Test {
public static void main(String args[]){
B b = new A();
b.testB();
}
}
no
8. In the above example can I write like this?
6
Prepared by: Imdad Areeph
}
no
7
Prepared by: Imdad Areeph
Ans
public class Test {
public static void main(String args[]){
}
}
11. There are four classes A,B,C,D. B extends A and D extends C. Like this
public class A {
public void testA(){
System.out.println("class A");
}
}
public class B extends A{
public void testB(){
System.out.println("Class B");
}
}
public class C {
public void tectC(){
System.out.println("class c");
}
}
8
Prepared by: Imdad Areeph
Method name same and argument different Method name and arguments same
It is known as compile time and static binding Its known as runtime or dynamic binding
It can have any access level But here same access level or widder. It can't be narrow scope
2.
Bellow we have given some examples for better understanding of overloading and overridding
3. What is out put of this program?
public class OverLoading {
public int methodA(){
return 5;
}
public void methodA(){
System.out.println("I am in void return tpye method");
}
public static void main(String a[])
{
OverLoading loading= new OverLoading();
int x=loading.methodA();
System.out.println(x);
loading.methodA();
}
}
Ans compile time error, because both methodA(), consider as one method. Overloading doesn't consider return type
9
Prepared by: Imdad Areeph
}
Ans
5
hello
5. What is out put of this program?
Ans
compile time error, argument mismatch
6. What is out put of this program?
public class OverLoading {
public void methodA(float x){
System.out.println(x);
}
10
Prepared by: Imdad Areeph
public static void main(String a[])
{
OverLoading loading= new OverLoading();
loading.methodA(4);
loading.methodA(2.0f);
}
}
Ans
4.0
2.0
7. What is out put of this program?
public class OverLoading {
public void methodA(float x){
System.out.println(x);
}
public static void main(String a[])
{
OverLoading loading= new OverLoading();
loading.methodA(4);
loading.methodA(2.0f);
loading.methodA(4.8d);
}
}
Ans
compile time error. Because 4.8d can't implecitily type cast to float
8. What is out put of this program?
public class OverLoading {
public void methodA(float x){
System.out.println(x);
}
public void methodA(Object x){
System.out.println("object"+x);
}
public static void main(String a[])
{
OverLoading loading= new OverLoading();
loading.methodA(4);
loading.methodA(2.0f);
loading.methodA(4.8d);
}
}
Ans
11
Prepared by: Imdad Areeph
4.0
2.0
object4.8(because 4.8d can type cast to object)
9. What is out put of this program?
public class OverLoading {
public void methodA(Object x){
System.out.println("object "+x);
}
public void methodA(String x){
System.out.println("string "+x);
}
public static void main(String a[])
{
OverLoading loading= new OverLoading();
loading.methodA("hello");
loading.methodA(3);
}
}
Ans
string hello
object 3
10. What is out put of this program?
public class OverLoading {
public void methodA(Object x){
System.out.println("object "+x);
}
public void methodA(int x){
System.out.println("string "+x);
}
public static void main(String a[])
{
OverLoading loading= new OverLoading();
loading.methodA("hello");
loading.methodA(loading);
loading.methodA(6.0f);
}
}
Ans
object hello
object OverLoading@1bc4459
object 6.0
12
Prepared by: Imdad Areeph
11. What is out put of this program?
public class A {
public void testA(){
System.out.println("class A");
}
}
13
Prepared by: Imdad Areeph
Ans
Class B
Class B
13. What is out put of this program?
public class A {
public void testA(){
System.out.println("class A");
}
}
Ans
class A
class A
14. What is out put of this program?
public class A {
public void testA(){
System.out.println("class A");
}
}
14
Prepared by: Imdad Areeph
}
}
Ans
Compilation error in class B,becaue over ridding method can't be narrow scope that described in super class
15. What is out put of this program?
public class A {
private void testA(){
System.out.println("class A");
}
}
Ans
Class B
Because private method can't override. The method testA(), we are declaring in B class, its itself one new method
16. What is out put of this program?
public class A {
final void testA(){
System.out.println("class A");
}
}
15
Prepared by: Imdad Areeph
b.testA();
}
}
Ans
Final method can't override, so it'll give compilation error in class B
17. What is out put of this program?
public class A {
public void testA(){
System.out.println("class A");
}
}
Ans
Static method can't hide, again it'll give compilation error in class B
18. What is out put of this program?
public class A {
public void testA() throws Exception{
System.out.println("class A");
}
}
public class B extends A{
public void testA(){
System.out.println("Class B");
}
}
public class OverRiddingInJava {
public static void main(String ar[]){
B b= new B();
b.testA();
A a=new B(); a.testA();
16
Prepared by: Imdad Areeph
}
}
Ans
Compilation error : Unhandled exception type Exception , in a.testA()
19. What is out put of this program?
public class A {
public void testA()throws Exception{
System.out.println("class A");
}
}
Ans:
Class B
Class B
20. What is out put of this program?
public class A {
public void testA()throws IOException{
System.out.println("class A");
}
}
17
Prepared by: Imdad Areeph
b.testA();
}
}
Ans: Compilation erro in class B. Because overridding method should throw, same exception or subclass excetion or no exception
21. What is dynamic method ditchpatch?
Ans:This is occours in case of over-ridding and super class and subclass having same method. Super class reference refers to sub class object
and calls the subclass method.
Its runtime binding or late binding.
Example
package com;
public class A {
public void test(){
System.out.println(" I am in Class A");
}
}
package com;
public class B extends A{
public void test()
{
System.out.println("I am in class B");
}
}
package com;
public class TestDynamicMethodDispatch {
public static void main(String ar[])
{
A a= new B();
a.test();
}
}
O/P
I am in class B
18
Prepared by: Imdad Areeph
b volatile int j;
c private int x;
d public int y;
e final int z;
f abstact int a;
Ans: a,b,c,d correct, e and f wrong because final variable always initalised so we have to write final int z=7;
abstact modifier can not asign to variable.
3. What are the wrong statement about method declaration?
public void methodA()
private void methodA()
final void methodA()
static void methodA()
volatile void methodA()
transient void methodA()
abstact void methodA();
Ans: Except volatile and transient other are correct, but if we are declaring method as abstact that class must be abstact
4. Can I write main method as private static void main(String a[])?
Ans: You won't get any compilation error, but JVM can not consider as main method. It will be a new method.
5. Can we write main method like this?
public static final void main(String ar[])
Ans: yes we can
6. Can we wtire a calss like?
public final abstract class A
Ans :no
Because, class is final means we can't extend that calss else where, but abstract means, it needs to be subclassed
7. Can I write one method like this
final static void methodA()
Ans: we can
8. What is difference between interface and abstact class?
19
Prepared by: Imdad Areeph
final int z=8;//5
}
Ans: only 3 and 5 are true and rest of lines is now allowed inside method.
12. What is the use of volatile modifier?
Ans:The volatile modifier applies only to instance variables .
13. What is transient modifiier in java?
Ans:Transient variable can not be serialized.
14. What is Java Serialization?
Ans:Serialization is a process to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again
For that we have to implements Serializable interface.
15. What is marker interface?
Ans:Interface which doesn't contain any method or variable called as marker interface. Exemple Serializable
21
Prepared by: Imdad Areeph
8. What is output of following program?
package com;
public class TestString {
public static void main(String a[]){
String str1="abc";
String str2="abc";
System.out.println(str1==str2);
}
}
Ans: true
9. What is difference between "==" and "equals()" methods?
Ans: equal() method checks weather two String contains same content or not.
Like String s1=new Sting("xyz")
String s2 ="xyz"
When I'll write s1.equals(s2), answer is true.
The operator "==", compares the references, whether two reference pointing to same object or not.
In above example If I'll write
s1==s2
Answer is false;
If I'll write like
String s="asd";
String s1="asd";
In this case ,s==s1 is true.
10. What is difference between String Buffer , String Builder and String Buffer?
Ans: String: String is immutable in nature. We can't modify String object after creating.
String Builder: Its mutable and synchronized (which means it is thread safe and it can be used in multi threaded envirnoment). So its
perpermance is slower than String Buffer.
String Buffer: Its also mutable and not synchronized. So perfermance wise its better that String Bulider.
11. When We should use String and when StringBuffer?
When the requirement like what ever is there in object should not cheange that case we should go for String.
But when we have to change frequently in object, in that case , we should go for StringBuffer.
For example String s="some";
s= s.concat("com");
In this case two object will unnecessarily create, and it will take more memory space as well.
Bu in case of StingButter, when we'll write
StringBuffer sb= new StringBuffer("some");
sb=sb.append("come");
It's all together one object.
22