Você está na página 1de 410

Lara Technologies

Lara Technologies

SCJP practice
Questions
Volume-1

Become a Laraite
to become a
SCJPite

Document Version : 2.0


st
Released on : 1 Jan 2009

080-41310124 http://www.javaeasytoall.com
Lara Technologies

“Many of the great achievements of the world


were accomplished by tired and hardworking men
who kept on working”.

Here is the man who kept on working for


us for 5 days round the clock. I’m very proud to
introduce Mr. Shiva Prakash (13th Batch) to our
Laraoites. He has put his complete effort to
develop this material. The best way to
appreciate his job is to imagine him without
one. Whole heartedly I appreciate all of his
hard work and time and also appreciate the
sacrifices he has had to make while he may
have been working so hard.

I honestly wish I could say or do more to


show my appreciation but I figured this was the
least I could do. I wish him a very bright
future. Thanks for your excellent and tireless
work
ALL THE BEST SHIVA

By
B.Ramesh

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Core Java
Day 1

 Jdk is java related software to be installed for developing Core


Java Applications
 What is JDK?
o J2SE Development kit
o Common location of jdk installation:- C:\Program Files
 Packages or software bundled with JDK are:-
o Development Tools (Mandatory with JDK)
o Demos (optional)
o Source Code (optional)
o Public JRE (optional)
 Public JRE is not for developer
 Public JRE is for customer purpose(purely for Production
purpose)
 Don’t install Development softwares inside Program Files
 Don’t put any spaces in between words while creating a folder
 Don’t install public JRE for Development
 JDK Contents
o Bin:- stands for Binary
 All binary files are kept in bin folder
 Binary files are .exe files or .cmd files or .bat files
 Binary files are unreadable by human
o Lib:- stands for Library
 All the library files form sun will be there in lib
folder
 Lib folder contains library i.e jar files
o Jre:- stands for J2SE runtime environment
 It is a part of JDK installation which is called
private JRE
 Jre is a separate software coming along with JDK which also
contains bin and lib folder in it
 In lib folder of JRE there is one jar file available i.e rt.jar
which contains all .class files developed by Sun Microsystems
development team
 For developing a core java application or any java project we
have to develop “.java” files
 We have to follow Sun’s Standard conventions while
developing java programs
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A simple HelloWorld application

Steps in developing a simple java program


 composing and saving with [class name].java
 compiling
 Running

Opening an editor for developing program


 Press Start button + r and type notepad and press enter key
 Press Window button + r and type notepad and press enter key
 Selecting notepad option from accessories of start button

Rule 1 Always place the public class or interface in a file as the


first class.

Type the program


class Hello
{
public static void main(String args[])
{
System.out.println(“Hello”);
}//end of main method
}//end of class Hello
save it in the bin folder of your jdk root folder with file name
Hello.java

compiling the java program


javac Hello.java

Running the java program


Java Hello
Output:-
Hello

Compiler’s responsibility
 First: Checking for syntactical errors
 Second: conversion of java code into bytecode
 Third: generating one .class file and coping the bytecode into
that file

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Some important commands of command window to be known are:


 Changing one drive to another drive i.e D (drivename): enter
key
Command prompt defaults directory
C:\users\documents and settings\ramesh>D:

 Clearing the screen i.e cls


 Changing directory i.e cd
 Move to parent directory i.e cd..
 Move to sub directory i.e cd [directory name]
 Pasting any text in command window i.e press alt +enter at
once and type e and type p
 To open windows explorer i.e window(key left to alt in the
keyboard) + e
 For coping i.e ctrl + c
 For pasting i.e ctrl + v

Rule 1 Always place the public class or interface in a file as the


first class.

Day 1 Review Questions:-


1. What is the latest Version of JDK?
2. JDK stands for?
3. J2SE stands for?.
4. Why exe files are used?
5. What is WIZARD?
6. While installing JDK5.0 will get four items, name them.
7. Which item is mandatory to install?
8. Which item is not mandatory to install?
9. Which item is used for development purpose?
10. Which item is used for customer purpose?
11. Which item is used for production purpose?
12. Which item has to be filtered while installing JDK5.0?
13. Why we will go for EXE files?
14. In which drive we have to keep the development software?
15. Why development languages are not kept inside the C drive?
16. What happens if we install the development language inside the C
drive?
17. Currently companies are using which version of JDK?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

18. While installing whether we have to give space between JDK and
5.0(Y/N)
19. If we give space between JDK and 5.0 what happens?
20. If we don’t give space between JDK and 5.0 what happens?
21. bin stands for what?
22. What is the use of bin?
23. What kinds of files are kept inside the bin folder?
24. Which are the different types of binary files?
25. Whether binary files are readable by humans?
26. Which type of language is understandable by the computers?
27. Which type of language is understandable by the humans?
28. Name two types of file in bin folder.
29. Lib stands for what?
30. What does lib folder contain?
31.In case of java, what we call for lib folder?
32. What does lib folder contain?
33. What does jar stands for?
34. Mention the type of jre.
35. Why private jre is used?
36. Why public jre is used?
37. Why we are filtering the public jre while installing?
38. What stands for jre?
39. jre is a software (Y/N)
40. Whether jre contains bin and lib files?
41. bin and lib files of jre are used for what?
42. For running the java programs which files are used?
43. Where .dll files are located?
44. What is rt.jar?
45. For what rt.jar files are used?
46. What does rt.jar file contain?
47. What is src?
48. What is wrar?

Rule 1 Always place the public class or interface in a file as the


first class.
49. For what purpose wrar files are used?
50. In which condition we cannot open the zip file?
51. What we have to do if src is not in a proper format?
52. Whether wrar is used for developing software?
53. How we can unzip the src file?
54. Where .java files are located?
.java files are character based files(Y/N)
56. What do you mean by open source?
57. Why java is better than other languages?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

58. What do you mean by composing?


59. How to open an editor?
60. How to open a command window?
61. Which command is used for copying?
62. Which command is used for saving the file?
63. In which location we have to save the java file?
64. What is the file name has to be given while saving the java
program?
64. By giving which extension we can save the java program?
65. First letter of the class name should be in upper case(Y/N)
66. What happens if we are not saving the file name of java program
inside the
double quote?
67. Which command is used for compiling the java program?
68. Which command is used to run the java program?
69. What is compilation?
70. After compiling what type of file we will get?
71. What do you mean by ‘cd’?
72. Which are the two conditions occurred while compiling the java
program?
73. For what .java files are used?
74. For what .class are used?
75. What is the input for javac command?
76. What is the input for java command?
77. For javac command input is .class file(Y/N)
78. For java command input is .java file(Y/N)
79. Why we are not giving any extension to the filename while
running the java
program?
80. Name common three types of binary files
81. Abbreviate J2ME.
82. Abbreviate J2EE.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 1 Always place the public class or interface in a file as the


first class.
Day 2

Developing a java program outside the bin directory of jdk

Updating path environment variable

Supposing we are developing our java program outside the bin


directory like
F:\lab\app1
We will not be able to compile the java program and command
prompt will say
‘Javac’ is not recognized as an internal or external command,
operable program or batch file.

To avoid the above problem we have to set path to javac command’s


location(for this purpose we use path environment variable of
operating system and if a command is known to operating system
then the command is know to command window also).

If we have to update the path we have 2 ways for doing it.

 Command window wise


 Operating system wise

Command window wise:


By using set command
i.e f:\lab\app1>set path = d:\JDK5.0\bin;

before compilation use the set command


set <environment-variable>=<value>;
‘ ; ’ is delimiter for more than one value

after compilation the .class file will be generated and kept in the same
folder from where we compile the java file

The above command will override the default value of path variable
of os
If we have to put our jdk into path and default value also then we
have to use the following code

080-41310124 http://www.javaeasytoall.com
Lara Technologies

f:\lab\app1>set path = %path%;d:\JDK5.0\bin;

try the following command to read path environment value


echo %path% will display the value of path variable
If we want to read any environment variable’s value use
%environment-variable%
Operating wise

1. Right click on the mycomputer icon in the desktop and select


advanced tab
2. Click on the Environment variables,
3. Click on the new button of user level
4. Type the variable name like path in Variable name text field.
5. Type the value like d:\jdk5.0\bin; in Variable value text field.

Rule 2 Always use explicit import statements

Close the current command window and open a new command


window for applying the changes

Illustration
Create a folder in f: drive with name as lab
Create a folder in lab folder with name app1
Open notepad editor and type the program in the app1 folder
Save the file with [classname].java
Set the path again if you are setting path in command window
wise( skip step if 2nd way), Compile the file like javac [filename].java
classname.class file is generated in the same folder where java file is
located

We shouldn’t keep both the java file and class file in the same folder
If we have to achieve this use –d option of the javac command
Eg:- javac –d f:\lab [filename].java

With the above compilation the .class file will be generated in f:\lab
directory not in the f:\lab\app1 directory.

Day 2 Review Questions:-

1. What is meant by environment variable?


2. Why set command is used?
3. Why set echo is used?
4. Whether the environment variable value is lost while closing the
command window?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

5. How to set the environment-value in a command window


permanently?
6. Where we have to go for setting environment variable?
7. What is path?
8. How to set path environment variable?
9. If the java code is syntactically correct, then its binary code is
stored in which type of file?
10.What is the usage of set command?
11.What is the usage of more than one semi colon in the path
environment variable?
12.Updating of path in operating system itself is globally available to
all the command window(y/n)?
13.How to synchronize two different directory or drive location?
14.What is classpath?
15.What does command window do?
16.What is need to set the environment variable?
17.Which are the two ways of setting the path?
18.Name the two types of environment variable?
19.What do you mean by system variable?
20.What do you mean by user variable?
21.What are the disadvantages of system variable?
22.What are the advantages of user variable?
23.How we can set new user variable?
24.How to update the path environment variable in operating
stystem?
25.By using which command we can display the new and existing
env- var?
26.Before setting the path in operating system, is it possible to run
the command in the command window?
Rule 2 Always use explicit import statements
Day 3

About Editplus
Development of various applications
Following sun standards
To separate the java files and class files and develops a core java
application
1. create 8th-batch folder in E: drive
2. create a folder with app1 as name in 8th-batch directory
3. create a folder with name src in app1 directory
4. create a folder with name classes parallel to src in app1
directory
5. develop all our java files and save them in src folder
6. compile our java files from the src directory with –d option
080-41310124 http://www.javaeasytoall.com
Lara Technologies

i.e javac –d ../classes *.java ( to compile all files at once)


javac –d ../classes [filename].java (to compile one file)
7. run .class file

Not Following sun standards


Names of directories like src and classes can also be changed develop
a core java application
1. create 8th-batch folder in E: drive
2. create a folder with app1 as name in 8th-batch directory
3. create a folder with name source-files in app1 directory
4. create a folder with name class-files parallel to src in app1
directory
5. develop all our java files and save them in src folder
6. compile our java files from the src directory with –d option
i.e javac –d ../class-files *.java ( to compile all files at once)
javac –d ../class-files [filename].java (to compile one file)
7. run .class file
Compilation if classes folder is kept in src folder itself and develop a
core java application
1. create 8th-batch folder in E: drive
2. create a folder with app1 as name in 8th-batch directory
3. create a folder with name src in app1 directory
4. create a folder with name classes parallel to java files in src
directory
5. develop all our java files and save them in src folder
6. compile our java files from the src directory with –d option
i.e javac –d classes *.java ( to compile all files at once)
javac –d classes [filename].java (to compile one file)
7. run .class file
Compilation without classes folder and keep .class files in parent of
src folder itself and develop a core java application
1. create 8th-batch folder in E: drive
2. create a folder with app1 as name in 8th-batch directory
3. create a folder with name src in app1 directory
4. develop all our java files and save them in src folder
5. compile our java files from the src directory with –d option
i.e javac –d .. *.java ( to compile all files at once)
javac –d .. [filename].java (to compile one file)
6. run .class file

Rule 3 Do not use static imports


Compilation without classes folder and keep .class files in current
folder using –d option and develop a core java application

080-41310124 http://www.javaeasytoall.com
Lara Technologies

1. create 8th-batch folder in E: drive


2. create a folder with app1 as name in 8th-batch directory
3. create a folder with name src in app1 directory
4. develop all our java files and save them in src folder
5. compile our java files from the src directory with –d option
i.e javac –d . *.java ( to compile all files at once)
javac –d . [filename].java (to compile one file)
6. run .class file
Compilation by keeping classes folder in src directory
1. create 8th-batch folder in E: drive
2. create a folder with app1 as name in 8th-batch directory
3. create a folder with name src in app1 directory
4. create a folder with name business-type inside src folder
5. create a folder with name ui-type inside src folder
6. create a folder with name classes parallel to java files in src
directory
7. create a folder with name business-type inside classes folder
8. create a folder with name ui-type inside classes folder
9. develop all some java files and save them in business-type and
ui-type folder of src folder
10.compile our java files from the src directory with –d option
i.e javac –d classes/business-type *.java ( to compile all files
into business-type)
javac –d classes/business-type [filename].java (to compile
one file)
javac –d classes/ui-type *.java (to compile all files into ui-
type)
javac –d classes/ui-type [filename].java (to compile one
file)
11.run .class file
Compilation by keeping classes folder in src directory
1. create 8th-batch folder in E: drive
2. create a folder with app1 as name in 8th-batch directory
3. create a folder with name src in app1 directory
4. create a folder with name classes parallel to app1 folder in 8th-
batch directory
5. develop all our java files and save them in src folder
6. compile our java files from the src directory with –d option
i.e javac –d ../../classes *.java ( to compile all files at once)
javac –d ../../classes [filename].java (to compile one file)
7. run .class file
Application with absolute path
Java files and class files are kept in different drives
1. create 8th-batch folder in E: drive
080-41310124 http://www.javaeasytoall.com
Lara Technologies

2. create a folder with app1 as name in 8th-batch directory in E:


drive
3. create 8th-batch folder in F: drive
4. create a folder with app1 as name in 8th-batch directory in F:
drive
5. develop all our java files and save them in src folder
6. compile our java files from the src directory with –d option
i.e javac –d F:\8th-batch\app1 *.java ( to compile all files at
once)
javac –d F:\8th-batch\app1 [filename].java (to compile
one file)
7. run .class file

Rule 3 Do not use static imports

Day 1 Review Questions:-


1. What is the necessity of creating .java and .class file in separate
folders?
2. How to install edit plus?
3. What is the command to compile .java programs?
4. What is the command to run the .java program?
5. What is the command to see all the available option of javac
command?
6. What does –help do?
7. for what –d is used?
8. Why deliminator is used?
9. What command is given if we want to go for other folder from
the current folder?
10.How to change the directory from parent directory to child
directory?
11.What happens if –d is not used in javac command?

SCJP Oriented

Question 1
Given the following Directory structure
bigProject
|- source
| |-Utils.java
|
|- classes
|-

080-41310124 http://www.javaeasytoall.com
Lara Technologies

And the following command line invocation


javac –d classes source/Utils.java.
Assume that current directory is bigProject,what is the result?
A. If the compile is successfully ,Utils.class is added to the
source directory.
B. The compiler returns an invalid flag error.
C. If the compile is successful ,Utils.class is added to the classes
directory.
D. If the compile is successful ,Utils.class is added to the
bigProject directory
Answer: C

Rule 3 Do not use static imports

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Day4

Object Oriented Programming


An object oriented program can be characterized as data
controlling access to code. The fundamental component of the
structured model is its function. Data is generated outside the system
and flows through a set of functions before being stored or processed.
In an object-oriented model, data becomes the fundamental
component in the form of objects.

Principles of Object Orientation:

The main principles of object oriented programming are

1) Abstraction
2) Encapsulation
3) Inheritance
4) Polymorphism

Abstraction:
The concept, in which multiple independent elements work
together to perform a single task. For example a car is having many
individual parts such as wheels, steering etc which work together to
form a single object car. We can manage complexity through this.

Encapsulation:
It is a mechanism which binds together code and data that the
code manipulates. With this we can achieve data hiding.(Mechanism
of grouping related data).

Inheritance:
It is a process in which one object inherits properties of other
object. It helps in code reuse.

Polymorphism:
Polymorphism means many forms. In case of polymorphism
code takes multiple forms.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Identify the entities and categories or types


Identify the properties and behavior of an each and every entity

Encapsulation with Java:-

Design a class with all properties and behavior of an entity is known


as Encapsulation

Illustration of encapsulation:-
Program 1:-
class Person
{
String name;
int age;
Rule 4 Do not use numbers as words in names.
double weight;
double height;
String qualification;
public double runRate()
{
double rate=0.0;
rate =(age /weight)*height;
return rate;
}
}

Methods will be working on attributes


Entities behavior will be depending on properties of the class
Methods are used to find out the behavior
Class is a blueprint which can be used for similar type of different
objects

Person ramesh = new Person();


Person vijay = new Person();
Person kiran = new Person();

‘new’ is a keyword used for creating an object. Right hand side of ‘=’
operator is executed first and secondly left hand side will be executed
and assignment operation is done. ‘ramesh’ is a derived variable
which can refer an Person class object. Person is called as derived
data type even though it is a class.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

2 types of variables, one is primitive variable and another is derived


variable. If a variables data type is among 8 primitives like byte,
short, int, long, float, double, character and Boolean; therefore that
variable is called primitive variable.
A variable which can hold object is called derived variable.

‘main’ method is an entry point and exit point of the java execution

Program 2:-
class Person
{
int age;
double weight;
double height;
double findRunRate()
{
double rate=0.0;
rate = (age/weight)*height;
return rate;
}
}
class PersonManager
{
public static void main(String args[])
{
Person p1 = new Person(); //step1
Person p2 = new Person(); //step2
Person p3 = new Person(); //step3
P1.age = 29; //step4
P1.weight = 59.08; //step5
P1.height = 6.0; //step6
P2.age = 18; //step7
P2.weight = 48.05; //step8
P2.height = 5.0; //step9
P3.age = 29; //step10
P3.weight = 59.08; //step11
P3.height = 6.0; //step12
Double rate = p1.findRunRate(); //step13
System.out.println(“P1 runrate: ”+rate); //step14
rate = p2.findRunRate(); //step15
System.out.println(“P2 runrate: ”+rate); //step16
rate = p3.findRunRate(); //step17
System.out.println(“P3 runrate: ”+rate); //step18
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 4 Do not use numbers as words in names.

Whenever we run program using java [classname] , then it will load


java related files into memory and look for [classname].class file and
load it into memory. And it searches for a main method in the class
declaration and executes main method.
Number of ‘.class’ files will be generated depending on number of
class declarations.

In the above program, when we trigger java PersonManager,


command window identifies where javacommand is located and
command window requires memory for loading javacommand
related files. Command window requests memory and gets from
operating system. Memory is divided into 2 parts, one is stack and
another is heap. Stack memory is used for loading statements and
execution also. Heap memory is used for storing objects.

P1 is local variable, which is defined inside a method. Local variable


is nothing out of main method scope means p1 doesn’t exist outside
main method.

There are 2 variables, one is local variable and another is global


variable
Age is global variable, which is defined in class and is also called
attribute.

Consider an entity class as Person and attributes as age, weight &


height and behavior named as findRunRate(). And one manager class
for control execution

Execution Steps:

1. An object of Person type is created in step1 (at this step


memory is allocated in heap memory for object and its
properties and a reference of Person class called p1 to refer any
object of Person type)
2. Another object of Person type is created in step2 (at this step
memory is allocated in heap memory for object and its

080-41310124 http://www.javaeasytoall.com
Lara Technologies

properties and a reference of Person class called p2 to refer any


object of Person type)

Rule 4 Do not use numbers as words in names.


3. Another object of Person type is created in step3 (at this step
memory is allocated in heap memory for object and its
properties and a reference of Person class called p3 to refer any
object of Person type)
4. A property age of the object referred by p1 is assigned with 29
in step 4
5. A property weight of the object referred by p1 is assigned with
59.08 in step 5
6. A property height of the object referred by p1 is assigned with
6.0 in step 6
7. A property age of the object referred by p2 is assigned with 29
in step 7
8. A property weight of the object referred by p2 is assigned with
59.08 in step 8
9. A property height of the object referred by p2 is assigned with
6.0 in step 9
10.A property age of the object referred by p3 is assigned with 29
in step 10
11.A property weight of the object referred by p3 is assigned with
59.08 in step11
12.A property height of the object referred by p3 is assigned with
6.0 in step 12
13.Behavior findRunRate() is called on object referred by p1
reference variable (

Class:
A class serves as a blueprint or a plan or a template. It specifies
what data (properties) and what functions (Behavior) are included in
object of that class. Class does not have existence (Memory will not
be allocated).

Attribute:
Specifies the properties of a Class.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Ex: Class A //Class name


{
int i; // i is an attribute behaving as a property of a class
A.
}

Method:
Defines a particular functionality(behavior) using the
properties of a class.

Ex: Class A
{
int a,b;
void AnMethod()
{
int c=a+b;
}// “AnMethod “ method defining an addition
behaviour(Functionality)
//using the property a & b of class A.
}

Object:
Every object is a real-world “instance” of a class, which is a
type of template used to define the characteristics
of an object. Each object has a name, attributes,
and operations.

A sample Hello World Program to explain instance(object) creation


of a class:

Program 3:-
class A
{
int i;
void show()
{
System.out.println(“Hello World”);
}//End of show()

} //End of class A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class B
{
public static void main(String args[])
{
A obj = new A();

Rule 4 Do not use numbers as words in names.

/* ”obj” is an object reference of type class A, is referring to an object


of type class A. The new operator dynamically allocates memory (at
run time) for an object and returns the base address of object.*/

obj.show(); //Calling statement to show method of class


A.
}//End of main()
}

Output: Hello World

Memory Management for Program 3:-

Stack Heap
(3)
i=0
(4)show() Object
(2)main()
obj

(1)JRE

Note: (1),(2),(3),(4) are the way memory has been utilized in program
3.

During execution of class B,. at first JRE (Java Run time


Environment) will be called to stack, JRE in turn calls main() method
to stack, then an object of class A will be created in Heap segment of
a memory depending on attributes size. main method in turn calls
show() method to stack for execution using object reference. show()

080-41310124 http://www.javaeasytoall.com
Lara Technologies

method uses attribute “i” for its functionality. After the execution of
show() method it
will be removed from the stack. As a step towards the completion of
the execution of program all methods will be removed from the stack
in the LIFO (Last In First Out) order as soon there execution
completes.

Rule 4 Do not use numbers as words in names.

Creating and handling multiple objects of a class:


If we create multiple objects of a class, then each object will
have its own state and behavior as defined by class.

Progrm 4:-
class Person
{
int age;
double weight;
double height;
void showDays()
{
System.out.println(“You are “ +age*365+ “days old”);
}
}
class PersonManager
{
public static void main(String args[])
{
1 Person per1 = new Person();
2 Person per2 = new Person();
3 per1.age=24;
4 per2.age=23;
5 per1.weight=60;
6 per2.weight=65;
7 per1.height=5.9;
8 per2.height=5.8;
9 per1.showDays();
10 per2.showDays();
}
}

In this program we are creating two objects of class Person


inside PersonManager class at lines 1&2. While creating object new
080-41310124 http://www.javaeasytoall.com
Lara Technologies

keyword will assign memory for the object inside Heap. Attribute’s
value of each object will be assigned to certain default values.
Default values are:
For int, char 0.
For double, float 0.0
For String & object reference variable value is Null.

Rule 4 Do not use numbers as words in names.

Stack Heap
Object
age=24
weight=60
showDays() height=5.9

main()
age=23
per1 weight=65
per2 height=5.8

JRE

As we can see in the above diagram each object is having its


own values of attributes. And also we can see that per1 and per2 are
two object reference variables of same class type referring to
different objects of class Person. In lines 9 & 10 we are calling
showDays() method by using each object reference variable. The
behavior of showDays() method is also changed depending on
properties of objects.

Program 5:-
class Person
{
int age;
double weight;
double height;
void showDays()
{
System.out.println(“You are “ +age*365+ “days old”);
}
}
class PersonManager
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String args[])


{
1 Person per1 = new Person();
2 Person per2;
3 per1.age=24;
4 per1.weight=60;
5 per1.height=5.9;
6 per2=per1;
7 per1.showDays();
8 per2.showDays();
9 per1=null;
10 per2.showDays();
}
}
In this program at statement 2, we have just declared an object
reference of type class Person. At statement 3,4,5 we are assigning
values to the attributes of class Person object. At statement 6 per2
object reference is referring to the object of per1
object reference. It means both object references are referring to the
same object. As a result we are getting same o/p for statement 7 & 8.

Output:
you are 8760 days old.//at statement 7
you are 8760 days old. //at statement 8
At statement 9 per1 is referring null, ie it is not referring any
address. At statement 10 we got the o/p as : you are 8760 days old.

An object can have many references to it. The object will be in


reachable state till at least one object reference variable referring to
it. If there is no single object reference referring to it then such a
object will become Not reachable or Abandoned. Now such an object
will not be referred in future, now it is eligible for garbage collection.

Program 6:-
class Address
{
int streetNum; //attribute of Address class
}
class Person
{
int age;
Address ad1;
void showDays()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(“You are “ +age*365+ “days old”);


}
}
class PersonManager
{
public static void main(String args[])
{
1 Person per1 = new Person();
2 System.out.println(per1.ad1);
// System.out.println(per1.ad1.streetNum);
3
4 per1.ad1=new Address();
5 System.out.println(per1.ad1.streetNum);

}
}

The above program will compile successfully. If we uncomment


rd
3 line then NullPointerException will occur at run time since the
value of ad1 is null by default and it is not pointing to any Address
object. At 4th line a new Address object is assigned to ad1. Now the 5 th
statement will be executed without any error and default value of
streetNum will be printed.

Day 4 Reivew Questions:-

1. What is encapsulation?
2. How to achieve encapsulation in java?
3. What is entity?
4. What is class?
5. What does class contain?
6. Fields or attributes of class called as properties(y/n)
7. Why stack memory is used?
8. Where the execution process occurs?
9. Why heap memory is used?
10.Where the objects are kept?
11.Which are the two types of memory?
12.Where the internals files of java command are kept?
13.What is the use of new keyword?
14.What is primitive data type?
15.What is derived data type?
16.What is reference variable?
17.What is live object?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

18.What is abandoned object?

SCJP Oriented

Question 1
Given:
20. public class CreditCard {
21.
22. private String cardlD;
23. private Integer limit;
24. public String ownerName;
25.
26. public void setCardlnformation(String cardlD,
27. String ownerName,
28. Integer limit) {
29. this.cardlD = cardlD;
30. this.ownerName = ownerName;
31. this.limit = limit;
32. }
33. }
Which is true?
A. The class is fully encapsulated.
B. The code demonstrates polymorphism.
C. The ownerName variable breaks encapsulation.
D. The cardlD and limit variables break polymorphism.
E. The setCardlnformation method breaks encapsulation.
Answer: C

Day 5
Data types

 What is a token?
The basic element recognized by the compiler is the "token." A
token is source-program text that the compiler does not break down
into component elements.
 Types:
• Constants
• Variables
• Datatypes
• Operators
• Keywords
• Identifiers
• Statements
080-41310124 http://www.javaeasytoall.com
Lara Technologies

• Comments

Constants(literals):
Literal means any number, text, or other information that
represents a value.
Types of constants:
Integral data types:
It is represented in Hexadecimal (16 bit), Octal (8 bit), Decimal
(10 bit), and Binary (2 bit)
Floating point data types: A floating-point literal can be denoted as a
decimal point, a fraction part, an exponent (represented by E or e)
and as an integer.
Character: We can specify a character literal as a single printable
character in a pair of single quote characters such as 'a', '#', and '3'.
Boolean : Boolean literal consists of TRUE or FALSE.

Variables:
Variables are locations in the memory that can hold values. Before
assigning any value to variable it must be declared. Every variable
has a datatype.
Java has 3 type of variables:
• Global variables:
A global variable is a variable that is accessible in every scope
in a class. Global variables are assigned with default literals.
• Local variable:
Local variables are used in blocks or methods.
Initial value: No value and must be assigned a value before the
first use.
Access from outside the scope is Impossible. Local variable
names are known only within the method.

Example 1: Example for global variable


class test
{
int i; //is a global variable
public static void main (String s[])
{
System.out.println(“Hello”);
}
}
Example 2: Example for local variable
class test1
{
public static void main (String s[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(“Hello”);
int i; //local variable
}
}

Rule 5 Do not capitalize any letter other than the first for all words
in a name.

Data Types
Java is a strongly typed language. This means that every
variable must have a declared type.

Java has eight primitive data types: four integer types for
whole-valued signed numbers (short, byte, int, long), two floating
point types for representing numbers with fractional precision (float,
double), one character type for Unicode encoding (char), and one
boolean type for representing logical values (boolean).

char
Java uses Unicode to represent characters. The char type is an
unsigned 16-bit (2 bytes) value ranging from 0 to 65,535 (\u0000 to
\uFFFF Unicode). The standard set of characters known as ASCII
still range from 32 to 127 (\u0020 to \u007F Unicode).
 a literal character is represented inside a pair of single quotes.
 the escape sequence \udddd, where d is a hexadecimal digit, is
used to directly denote a Unicode character. Hexadecimal
characters must always have four digits.
 the escape sequence \ddd, where d is a octal digit, can express a
character literal. Octal character constants have three or fewer
digits and cannot exceed \377.

Type Default Size Range

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 5 Do not capitalize any letter other than the first for all
words in a name.
Java allows the following escape sequences for special characters:

Derived data types are objects derived from classes, interfaces,


enums
Standard Arithmetic Operators and Arithmetic Operator
Assignments
The common arithmetic operators + - * / % are supported in Java.
 The / operator denotes integer division if both arguments are
integer and floating point division if a least one operand is
floating point.
 The modulus operator % returns the remainder of an integer
division.
 Each arithmetic operator has a shortcut assignment operator.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Increment and Decrement Operators


The increment and decrement operators (++ and --) are short hand
notation for adding or subtracting 1 from an operand. These
operators can appear in prefix form where they precede the operand
or postfix form where they follow the operand.
 In prefix form (++operand, --operand), first
increment/decrement the operand by 1, then use the new value
in the expression.
 In postfix form (operand++, operand--), first use the current
value of the operand in the expression, then
increment/decrement by 1.

Relational and Boolean Operators

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Conditional (short-circuit) Logical Operators

The conditional operators && (AND) and || (inclusive OR) avoid


evaluating their second operand if possible. Thus, for an &&
operator, if the first operand is false, then the second operand is not
evaluated. For the || operator, if the first operand is true, then the
second operand is not evaluated.

The Conditional Operator ? :


The conditional operator ? : yields one of two values based on a
boolean expression. The general form is

Keywords:
Java reserves some words. They cannot be used as variable,
method or class names.
With no keyword prior to data type will give compile time
error

Example 3:
class test1
{
public static void main (String s[])
{
System.out.println(“Hello”);
}
}
In above example class, public, static, void are keywords.

Identifiers:
Identifiers are the names of variables, methods, classes,
packages and interfaces.
Identifiers must be composed of letters, numbers, the
underscore _ and the dollar sign $. Identifiers may only begin with a
letter, the underscore or a dollar sign.

Example 4:
class test1
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main (String s[])


{
System.out.println(“Hello”);
}
}
In above example “s”(variable identifier), Hello, main.

Comments:
Comments in Java, like comments in most programming
languages, do not show up in the executable program.
Java has three ways of marking comments.
• Single line comment: The most common method is a //. You use
this for a comment that will run from the // to the end of the
line
• Multiline comment: Commenting multiple lines.
/*-----------------*/
• Documentation Comments: The JDK contains a very useful
tool, called javadoc, that generates HTML documentation from
your source files.
The javadoc utility extracts information for the following
items:
Packages
Public classes and interfaces
Public and protected methods
Public and protected fields
• You can (and should) supply a comment for each of these
features. Each comment is placed immediately above the
feature it describes. A comment starts with a /** and ends with
a */.
• Each /** . . . */ documentation comment contains free-form text
followed by tags. A tag starts with an @, such as @author.
• A javadoc comment consisting of characters between the /**
that begins the comment and the */ that ends the javadoc
comment. The utility javadoc can generate HTML
documentation based on this comment structure.

 Main method:
Program execution starts with main method and ends with
main method

Example 5:
class Helloworld
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String s[])


{
System.out.println(“main method”);
}
public static void test()
{
System.out.println(“test method”);
}
}
In above example o/p is “main method” since then there is no calling
statement for test method (called method).

Example 6:
Depending on number of calling statement the method is executed.
class Helloworld
{
public static void main(String s[])//callback method
{
test();//calling statement
System.out.println(“main method”);
test();
}
public static void test() //called method
{
System.out.println(“test method”)
}
}
 Output of Example 6:
C:\Java\jdk1.5.0_05\bin>javac Test.java
C:\Java\jdk1.5.0_05\bin>java Test
test method
main method
test method

Control Flow

if-else

The if-else construct, given below, is the most basic form of


conditional control flow. First, the boolean-expression is evaluated. If
the resulting boolean value is true, then statement1 is executed. If the
resulting boolean value is false, then statement2 is executed.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

for
The for loop is used to loop over a range of values. The basic form is

while and do-while


The format for the while loop is

If the body loop should execute at least once, then the do-while
construct should be used
where a boolean-expression is placed at the end of the statement
body.

switch

The switch statement evaluates an integer expression (byte, short, int)


to change flow control to a corresponding labeled case statement or
block. If a matching case label is found, then control is transferred to
the first statement following the case label. If a corresponding case
label is not found, then control flow is transferred to the first
statement following the default label.
The general form of the switch control is

080-41310124 http://www.javaeasytoall.com
Lara Technologies

A break statement is optional for each case label. If no break is


stated, then the control flow of the program will continue to the next
case label (fall through to the next case). If a break (without a label)
is stated, then the break statement will cause execution to branch to
the first statement past the switch.
continue and break
A continue statement skips to the end of a loop’s body and evaluates
the boolean-expression that controls the loop. In particular, in a for
loop, the increment expression is executed before examining the
boolean-expression.
 A ‘continue’ only has meaning inside: for, while, do-while.
 If the ‘continue’ keyword is followed by a label of an enclosing
loop, execution skips to the end of the labeled loop.
 If no label is specified, then execution skips to the end of the
loop in which the continue is located within.

A labeled break statement can be used to exit from any block,


whereas an unlabeled break can only be used inside a loop control
structure or switch structure.
 An unlabeled break terminates the innermost for, while, do or
switch.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

 To terminate an outer loop, use a labeled break, where the


outer loop statement is proceeded by the label name.

Day 5 Reivew Questions:-

1. What are data types?


2. What are primitive data types?
3. What are operators?
4. Which are the different types of operators?
5. List the arithmetic operators?
6. In java which is the only operator that can be overloaded?
7. What are operands?
8. Wheather addition takes place if any of the operand is string?
9. While doing addition addition, what happens if any of the
operand is string?
10. Let, string a =”java”;
Int b=3;
Int c=7;
System.out.println(“a+b+c=” a+b+c); Write the output?
11. Let, string a =”hello”;
Int b=3;
Int c=7;
System.out.println(“a+(b+c)=” a+(b+c)); Write the
output?
12. What are the different types of unary operator?
13. If i=8, What is the value of
a. i++
b. ++i
c. i--
d. --i
14. List out the Assignment operators?
15. List out the relational operators?
16. List out the logical operators?
17. List out bitwise operator?
18. List out short circuit operator?
19. Consider int a=5; int b=7;
a. a&b=?
b. a!b=?
c. a^b=?
d. a|b=?
20. Write the syntax for ternary operator?
21. List the two different types of flow control?
22. What is looping?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

23. Write the syntax for


a. if-else
b. nested if-else
c. switch statement
d. while loop
e do-while loop
f. for loop
24. Write the output,
for(int i=0;i<5;i++)
{
System.out.println(“i”);
If(i==2)
{
break;
}
System.out.println(“Hello”);
}

25. Write the output,


for(int i=0;i<5;i++)
{
System.out.println(“i”);
If(i==2)
{
continue;
}
System.out.println(“Hello”);
}

26. Write the output,


for(int i=0;i<5;i++)
{
while(true)
{
System.out.println(“i”);
Break outer;
}
System.out.println(“hi”);
}
System.out.println(“abc”);

SCJP Oriented
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Question 2
Given:
10. public class Bar {
11.static void foo(int...x) {
12. // insert code here
13. }
14. }
Which two code fragments, inserted independently at line 12, will
allow
the class to compile? (Choose two.)
A. foreach(x) System.out.println(z);
B. for(int z : x) System.out.println(z);
C. while( x.hasNext()) System.out.println( x.next());
D. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);
Answer: BD

Question 3
Given:
11. public class Test {
12. public static void main(String [] args) {
13. int x =5;
14. boolean b1 = true;
15. boolean b2 = false;
16.
17.if((x==4) && !b2)
18. System.out.print(”l “);
19. System.out.print(”2 “);
20. if ((b2 = true) && b1)
21. System.out.print(”3 “);
22. }
23. }
What is the result?
A. 2
B. 3
C. 1 2
D. 2 3
E. 1 2 3
F. Compilation fails.
G. Au exceptional is thrown at runtime.
Answer: D

Question 4
Given:
080-41310124 http://www.javaeasytoall.com
Lara Technologies

35. String #name = “Jane Doe”;


36.int $age=24;
37. Double _height = 123.5;
38. double ~temp = 37.5;
Which two are true? (Choose two.)
A. Line 35 will not compile.
B. Line 36 will not compile.
C. Line 37 will not compile.
D. Line 38 will not compile.
Answer: AD

Question 5
Given:
10.int x=0;
11.int y 10;
12. do {
l3. y--;
14. ++x;
15. } while (x < 5);
16. System.out.print(x + “,“ + y);
What is the result?
A. 5,6
B. 5,5
C. 6,5
D. 6,6
Answer: B

Question 6
Given:
25.intx=12;
26. while (x < 10) {
27. x--;
28. }
29. System.out.print(x);
What is the result?
A. 0
B. 10
C. 12
D. Line 29 will never be reached.
Answer: C

Question 7
Given:
35. int x= 10;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

36. do {
37. x--;
38. } while(x< 10);
How many times will line 37 be executed?
A. ten times
B. zero times
C. one to me times
D. more than ten times
Answer: D

Question 8
Given:
11. public static void main(String[] args) {
12. for (int i=0;i<= 10;i++){
13. if( i>6) break;
14. }
15. System.out.println(i);
16. }
What is the result?
A. 6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: E

Question 9
Given:
11. class Cup { }
12. class PoisonCup extends Cup { }
21. public void takeCup(Cup c) {
22. if(c instanceof PoisonCup) {
23. System.out.println(”Inconceivable!”);
24. } else if(c instanceof Cup) {
25. System.out.println(”Dizzying intellect!”);
26. } else {
27. System.exit(0);
28. }
29. }
And the execution of the statements:
Cup cup = new PoisonCup();
takeCup(cup);
What is the output?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A. Inconceivable!
B. Dizzying intellect!
C. The code runs with no output.
D. An exception is thrown at runtime.
E. Compilation fails because of an error in line 22.
Answer: A

Question 10

Given:
3. public class Batman{
4. int suares = 81.
5. public static void main(String args[]){
6. new Batman().go();
7. }
8. void go(){
9. incr(++squares);
10. System.out.println(squares);
11. }
12. void incr(int squares){squares + =10;}
13. }
What is the result?
A. 81
B. 82
C. 91
D. 92
E. compilation fails.
F. An Exception is thrown at runtime.
Answer: B

Question 11
Given:
1. public class Breaker2{
2. static String o = “”;
3. public static void main(String args[]){
4. z:
5. for(int x=2;x<7;x++){
6. if(x == 3)continue;
7. if(x == 5)break z;
8. o = o+x;
9. }
10.System.out.println(o);
11.}
12.}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

What is the result?


A. 2
B. 24
C. 234
D. 246
E. 2346
F. compilation Fails
Answer: B

Question 12
2. Given:
1. public class Breaker{
2. static String o = “”;
3. public static void main(String args[]){
4. z:
5. o = o + 2;
6. for(int x=3;x<8;x++){
7. if(x == 4)break;
8. if(x == 6)break z;
9. o = o+x;
10.}
11.System.out.println(o);
12.}
13.}
What is the result?
A. 23
B. 234
C. 235
D. 2345
E. 2357
F. 23457
G. compilation Fails
Answer: G

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Day 6

Constructor
Class can have four members in it.

They are
1] Attributes
2] Methods
3] Constructors
4] Initialization Blocks.

Attributes and methods are already discussed. Third member


is Constructor.

Constructors:
Constructors constructs desired state of an object at the time of
creation of object. A constructor is having identifier same as class
identifier to which it belongs. Constructors don’t have any return
type.

Example 1:-
class Person
{

080-41310124 http://www.javaeasytoall.com
Lara Technologies

int age;
double weight;
Person()
{
age=30;
}
Person(int a)
{
age=a;
}
Person(int a, double b)
{
age=a;
weight=b;
}
}
class PersonManager
{
public static void main(String args[])
{
1 Person per1 = new Person(); //object creation using
no-arg constructor
2 Person per2 = new Person(20); //object creation
using single
// parameterized
constructor
3 System.out.println(per1.age); // will print 30
4 System.out.println(per2.age); // will print 20
}
}

In the above example we achieved desired state of objects while


creating them. By output of 3 and 4 it is confirmed.
We can write as many constructors as we want till constructor
signature differs. For creating an object we should have at least one
constructor. If we don’t define any constructor explicitly, in our class,
then compiler will keep one default (no-arg) constructor byte code
during generation of .class file, as we saw in our previous programs.

Constructor signature consists of


1] Constructor Name
2] List of arguments
3] Order of arguments

080-41310124 http://www.javaeasytoall.com
Lara Technologies

In 5th program we have defined 3 constructors which are


differing in signature.

Example 2:-
class Person
{
int age;
//Person() if u comment this no org constructor then u will get
compile time error
//{
//}
Person(int a)
{
age=a;
}
}

Rule 6 Do not remove letters from words, except to create


standard short forms.
class PersonManager
{
public static void main(String args[])
{
Person per2 = new Person(); //object creation using
default constructor.
}//End of main()
}
Output: Compiler time Error.

In this program we have defined a single parameterized


constructor, but we are trying to create an object using default
constructor. If we don’t define any constructor then only compiler
will keep a byte code of default constructor, otherwise it will not, as a
result we got compile time error. To avoid this error, we can create
object using single parameterized constructor or have to define no-
arg constructor explicitly in class Person.

Example 3:-
class Person
{
Person()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Person per = new Person();


}
}
class PersonManager
{
public static void main(String args[])
{
Person per2 = new Person(); //object creation using
default constructor.
}//End of main()
}
Output: StackOverFlow, Run time Error.

We are trying to create object of a class Person inside


constructor, during run time our stack will over flew because we are
trying to create object in recursion order.

Example 4:-
class Person
{
Person per = new Person();
}
Output: StackOverFlow, Run time Error.

We are trying to create object of a class Person outside a


method, here object is getting created in recursive order.

Example 5:-
class Person
{
int age;
double weight;
double height;
Person ()
{
//constructor
}//end of no-arg constructor
Person (int a, double w, double h)
{
//Parameterized or 3-arg constructor
080-41310124 http://www.javaeasytoall.com
Lara Technologies

age = a;
weight = w;
height = h;
}//end of parameterized constructor

double findRunRate()
{
return (height/weight) * age;
}//end of method
}//end of class

class PersonManager
{
Public static void main(String args[])
{
Person p1 = new Person();
p1.age = 29;
p1.weight = 59.08;
p1.height = 5.8
double rate = p1.findRunRate();
System.out.println(rate);
Person p2 = new Person(25,60.0,5.8);
rate = p2.findRunRate();
System.out.println(rate);
}//end of main method
}//end of class

 Constructor has same name as of the class but doesn’t have


return type.
 Method can have any name but it should have return type.
 Constructors are stored in stack memory while execution.
 Constructors will be called automatically when objects are
created.
 Without a constructor objects of that class cannot be created.
 When compiler finds no constructor in class it will develop the
byte code equivalent to no-arg constructor in ‘.class’ file. There
must be at least one constructor in any ‘.class’ file.

Constructor is a definition block to construct initial state of an object


with the desired values.
Definition:-
Constructor is defined as an executable block defined for
constructing desired state at the time of creating an object.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Example 6:-
Class A
{
int i;
A(int a)
{
i = a;
}//end of 1-arg constructor
int getI()
{
return i;
}//end of getI method
void setI(int a)
{
i = a;
}//end of setI method
}//end of class A

class Manager
{
public static void main(string args[])
{
A a1 = new A (10);
int i = a1.getI();
System.out.println(i);
a1.setI(20);
i = a1.getI();
System.out.println(i);
a1.setI(30);
i = a1.getI();
System.out.println(i);
}// end of main method
}//end of Manager class

Compiler will add new parameter ‘this’ to every definition block for
example
A (this, int a). Compiler will be writing this code only in the class file,
but not in java file. When two variables are having of same name it
can be differentiated by this.

Usage and need of ‘this’ keyword (field hiding)


080-41310124 http://www.javaeasytoall.com
Lara Technologies

class B
{
int i, j;
B()
{
this.i = 10;
this.j = 20;
}
B(int i, int j)
{
this.i = i;
this.j = j;
}
int getSum()
{
return this.i + this.j;
}
}//end of the class B

class Manager
{
public static void main(String args[])
{
B b1 =new B();
B b2 = b1;
System.out.println(b1.getSum());
System.out.println(b2.getSum());
B b3 = new B(20, 30);
B b4 = b3;
System.out.println(b1.getSum());
System.out.println(b2.getSum());
System.out.println(b3.getSum());
System.out.println(b4.getSum());
}
}

Local and global variables are differentiated by ‘this’ operator.


this.i --> global i declared in class
this.j --> global j declared in class
Constructor signature
1) Constructor name
2) List of arguments(i.e parameters)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

3) Order of arguments
As long as constructor signature changing, we can
have as many constructors as possible. Keeping more than one
constructor in same class with different signatures is called
“Constructor Overloading”. We can overload constructor as long as
we wish with change in constructor signature. Constructor
overloading is required to define different ways of creating object
with respect to constructor.

Day 6 Review Questions:-

1. What do you mean by global parameter?


2. What do you mean by local parameter?
3. What is constructor?
4. Methods have return type(Y/N).
5. Constructors have return type(Y/N).
6. Constructor are those which have same name as class
name(Y/N).
7. Methods can take any name(Y/N).
8. Constructor and Methods are executed only from stack(Y/N).
9. Whether we can create object without constructor?
10.Define constructor.
11.What do you mean by No-Argument-Constructor?
12.What is desired value?
13.What is desired constructor?
14.What is “this” inside the .class file?
15.What is the use of “this”?
16.What is calling statement?
17.What is abandant object?
18.Is it possible to keep only constructor inside the class?
19.If class is their without a constructor then, that class is of
waste(Y/N)
20.Is it necessary to have attributes or methods inside the class
compulsorily?
21.What happens if their is no constructor inside the class while
execution?
22.What do you mean by constructor signature?
23.Name three types of constructor signature?
24.What is constructor overloading?
25.Is it possible to overload constructor as many times as
possible?
26.Why do you require constructor overloading?
27.On what basis the compilers will come to know about the
constructor duplication.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

SCJP Oriented

Question 1
Click the Exhibit button.
11. class Person {
12. String name = “No name’;
13. public Person(String nm) { name = nm; }
14. }
15.
16. class Employee extends Person {
17. String emplD = “0000”;
18. public Employee(String id) { empID = id; }
19. }
20.
21. public class EmployeeTest {
22. public static void main(String[] args) {
23. Employee e = new Employee(”4321”);
24. System.out.println(e.empID);
25. }
26. }
What is the result?
A. 4321
B. 0000
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 18.
Answer: D

Question 2
Click the Exhibit button.
1. public class A {
2.
3. private int counter = 0;
4.
5. public static int getInstanceCount() {
6. return counter;
7. }
8.
9. public A() {
10. counter++;
11. }
12.
13. }
Given this code from Class B:
080-41310124 http://www.javaeasytoall.com
Lara Technologies

25.A a1 =new A();


26. A a2 =new A();
27. A a3 =new A();
28. System.out.printIn(A.getInstanceCount() );
What is the result?
A. Compilation of class A fails.
B. Line 28 prints the value 3 to System.out.
C. Line 28 prints the value 1 to System.out.
D. A runtime error occurs when line 25 executes.
E. Compilation fails because of an error on line 28.
Answer: A

Question 3
Given:
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
.....
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a= 5; }
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
A. Change line 2 to:
public int a;
B. Change line 2 to:
protected int a;
C. Change line 13 to:
public Sub() { this(5); }
D. Change line 13 to:
public Sub() { super(5); }
E. Change line 13 to:
public Sub() { super(a); }
Answer: CD

Question 4
Given:
10. public class Hello {
11. String title;
12. int value;
13. public Hello() {
14. title += “ World”;
15. }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

16. public Hello(int value) {


17. this.value = value;
18. title = “Hello”;
19. Hello();
20. }
21. }
and:
30. Hello c = new Hello(5);
31. System.out.println(c.title);
What is the result?
A. Hello
B. Hello World
C. Compilation fails.
D. Hello World 5
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: C

Question 5
Click the Exhibit button.
1. public class Employee {
2. String name;
3. double baseSalary;
4. Employee(String name, double baseSalary) {
5. this.name = name;
6. this.baseSalary = baseSalary;
7. }
8. }
And:
1. public class Salesperson extends Employee {
2. double commission;
3. public Salesperson(String name, double baseSalary,
4. double commission) {
5. // insert code here
6. }
7. }
Which code, inserted at line 7, completes the Salesperson
constructor?
A. this.commission = commission;
B. superb();
commission = commission;
C. this.commission = commission;
superb();
D. super(name, baseSalary);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

this.commission = commission;
E. super();
this.commission = commission;
F. this.commission = commission;
super(name, baseSalary);
Answer: D

Question 6
Click the Exhibit button.
11. class Payload {
12. private int weight;
13. public Payload(int wt) { weight = wt; }
13. public void setWeight(mt w) { weight = w; }
15. public String toString { return Integer.toString(weight); }
16. }
17.
18. public class TestPayload {
19. static void changePayload(Payload p) {
20. /* insert code here */
21. }
22.
23. public static void main(String[] args) {
24. Payload p = new Payload();
25. p.setWeight(1024);
26. changePayload(p);
27. System.out.println(”The value of p is “+ p);
28. }
29. }
Which statement, placed at line 20, causes the code to print “The
value of p is 420.”?
A. p.setWeight(420);
B. p.changePayload(420);
C. p = new Payload(420);
D. Payload.setWeight(420);
E. p = Payload.setWeight(420);
F. p = new Payload();
p.setWeight(420);
Answer: A

Question 7
Click the Exhibit button.
10. class Foo {
11. private int x;
12.publicFoo(intx) {this.x=x; }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

13. public void setX( int x) { this.x = x; }


14. public int getX() { return x; }
15. }
16.
17. public class Gamma {
18.
19. static Foo fooBar( Foo foo) {
20. foo = new Foo( 100);
21. return foo;
22. }
23.
24. public static void main( String[] args) {
25. Foo foo = new Foo( 300);
26. System.out.print( foo.getX() + “-“);
27.
28. Foo fooFoo = fooBar( foo);
29. System.out.print( foo.getX() + “-“);
30. System.out.print( fooFoo.getX() + “-“);
31.
32. foo = fooBar( fooFoo);
33. System.out.print( foo.getX() + “-“);
34. System.out.prmt( fooFoo.getX());
35. }
36. }
What is the output of this program?
A. 300-100-100-100-100
B. 300-300-100-100-100
C. 300-300-300-100-100
D. 300-300-300-300-100
Answer: B

Day 7
Inheritance:
Inheritance is a process of Properties from generalized entity to
specialized entity. Example Dog is a kind on animal. Inheritance
means new class extending a generalized class with. A specialized
class is inheriting the properties from a generalized class.
Generalized class is called super class or parent class. Specialized
class is called as sub class or derived class.
We can develop sub class by using maximum of one class but not
more one class.
<<extends>> is a relation between super class and sub class.

class Address
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
}

class LatestAddress extends Address


{
}

A sub class should have only one super class i.e. in java we can’t
make more than one class as super class. Multiple inheritance is not
possible. Constructors will not be inherited to sub class. Only
methods and variables will be inherited.

class B
{
int i;// i here is a variable declared in class B (incorporated member)
}
class C extends B
{
}

B class contains one attribute and no-arg constructor. C class


contains one attribute and one sub class constructor, but not the
super class one.

Every class should become sub class to object class, so object class is
super class to every class. Super-most parent class in java is an
Object class. Achieving specialized-class from a generalized-super
class is called inheritance.

Example 1:-
Class A
{
int i;
}
Class B extends A
{
int j;// i will be inherited and ‘j’ is incorporated
}
Class C extends B
{
int k;// i and j will be inherited ‘k’ is incorporated
}
In the above example, there are 3 classes named A, B & C. class B is
declared and a variable named ‘i’ of data type is also declared in it,
080-41310124 http://www.javaeasytoall.com
Lara Technologies

meaning ‘i’ is incorporated in class A. Another class B is declared as a


sub class to class A, in which there is a variable j declared whose data
type is int and got one inherited variable named ‘i’. So, there are 2
members other than B class Constructor. Another class C is declared
with a variable named k whose data type is int which called
incorporated member. Finally, there are 3 members apart from
constructor 2 are inherited and one is declared. Here constructors of
super classes will not be inherited to sub classes.
Example 2:-
Class A
{
A()
{
System.out.println(“A”);
}
}

Rule 7 Do not use the long form of a word when a standard


short form exists.
Class B extends A
{
B()
{
System.out.println(“B”);
}
}
class C extends B
{
C()
{
System.out.println(“C”);
}

class Manager
{
public static void main(String args[])
{
C c = new C ();
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Output:-
A
B
C
The above output is obtained because of a concept called
“Constructor chain”.

Inheriting the features of one class into other class is known as


Inheritance. A class which is being inherited is a generalized class or
super class, and a class which is inheriting is a specialized class or sub
class.

Here latestAddress class is super class and Address class is sub class

Address
Generalization Specialization

LatestAddress

Example 3:-
class Address
{
int houseNum;
int streetNum;
int pinCode;
}
class LatestAddress extends Address//Through extends keyword we
are inheriting //class Address features into
class LatestAdderss.
{
int mobileNum;
void showLatestAdd() {
System.out.println(“House Num:” +houseNum);
System.out.println(“Street Num:” + streetNum);
System.out.println(“Pin Code:” + pinCode);
System.out.println(“Mobile Num:” + mobileNum);
}
}
class AddressManager
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String args[])


{
LatestAddress la = new LatestAddress();
la.showLatestAdd();
}//End of main()
}
In this program the LatestAddress class extends Address class.
Hence the object of type LatestAddress will have proprties of Address
class and its own properties too.
Class LatestAddress is having totally 6 members. They are ,
houseNum, streetNum, pinCode of Address class and mobileNum,
showLatestAdd() and LatestAddress() (constructor) of LatestAddress
class. Constructors of super class will not be inherited to sub classes.
In java every class is a sub class of java.lang.Object class by
default.

HAS-A Relation :
In case of HAS-A relation we create object of one class inside
other class or same class. Except that there is no other relation
present between those two classes.
Example 4:-
class Person
{
int i;
}
class PersonManager
{
public static void main(String args[])
{
Person per2 = new Person();
System.out.println(per2.i);
}
}
In above example PersonManager class HAS-A Person object
in it.

IS-A Relation :
In case of IS-A relation one class is a sub class of other class.
Thus one class inherits properties of other class.

Example 5:-
class Person
{
int i;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class PersonManager extends Person
{
public static void main(String args[])
{
PersonManager p1 = new PersonManager();
System.out.println(p1.i);
}
}
In above example PersonManager class IS-A type of Person
class.

Constructor Chain
Even though constructor is not inherited, super class constructor is
called before executing the child’s class constructor body. Every class
constructor’s first statement should be either ‘super’ or ‘this’
statement
For example,
super (); or super (- , - ,…..);
this (); or this (-, -,…..);
When developer fails to keep one among the above four statements,
compiler by default it will put super (); and it is a calling statement to
super class constructor. Though constructor is not inherited to sub
class, due to super (); the super class constructor will be
automatically called.
Compiler will automatically put the calling statement in any
constructor if it doesn’t finds super or this statement as the first
statement.

Program 6:-
Class A extends Object
{
A(int i)
{
 Super();
System.out.println(“A”);
}
}
Class B extends A
{
B()
{
 Super();
System.out.println(“B”);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}

B class will not be compiled because there is no-arg constructor in


class A as there is calling statement to super class constructor kept by
compiler.

Program 7:-
Class A extends Object
{
A()
{
 Super();
System.out.println(“A”);
}
}
Class B extends A
{
B()
{
super(10);
System.out.println(“B”);
}
}

B class will not be compiled because there is a calling statement to A


class 1-arg constructor and which is not written by developer or
compiler in class A.

Modifications to make both program compile success

Program6 has 2 options


Option - 1:-
Class A extends Object
{
A(int i)
{
 Super();
System.out.println(“A”);
}
}
Class B extends A
{
B()
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
super(10);// to put this code
System.out.println(“B”);
}
}

Option – 2:-
Class A extends Object
{
A()// remove the parameter
{
 Super();
System.out.println(“A”);
}
}
class B extends A
{
B()
{
 Super();
System.out.println(“B”);
}
}

Program 8:-
class A extends Object
{
A(int i, int j)
{
 Super();
System.out.println(“A”);
}
}
class B extends A
{
 B()
{
Super();
}
}

The above program is also not compiled because a no-arg constructor


is kept by compiler as there is no constructor kept in class B, and the

080-41310124 http://www.javaeasytoall.com
Lara Technologies

first statement in default constructor will be super (); and there is no


no-arg constructor in class A.

Program 9:-
class A
{
A()
{
this(10);
}

A(int i)
{
this();
}
}

The above program will never be compiled successfully because of


cyclic constructor calling and which is not allowed by compiler.

For any object creation the super-most parent class called Object
class’s constructor will be called because of calling statement to super
class in sub class constructor.

The above process is known as ‘Constructor Chaining’.

Program 10:-
class A
{
A()
{
super(10);
}
}

The above program will not compile because parent class is Object
class and it doesn’t have any constructor which is taking arguments
and we are trying to call 1-arg constructor.

Program 11:-
class A
{
A()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(“A”);
super();
}
}

Program 12:-
class A
{
A()
{
System.out.println(“A”);
this(10);
}
A(int i)
{
System.out.println(“A (int)”);
}
}

The above 2 programs will not compile because in same constructor,


we cannot call any other constructors more than once.

We can call the constructor without creating an object also but we


can’t use constructor name and we can only use super or this for
calling the constructors.

Constructor Chain:

Program 13:-
class A
{
A()
{
//super();
System.out.println(“A()”);
}
}
class B extends A
{
int i;
B()
{
//super();
System.out.println(“B()”);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
class Manager
{
public static void main(String args[])
{
B b1 = new B();
System.out.println(“End of object creation”);
}
}

Output:
A()
B()
End of object creation

Here to create object of class B we are calling constructor of


class B. During compilation compiler will search for super() or this()
statement as a first statement inside called constructor , if it is not
finding then compiler by itself will keep byte code of super() as first
statement. Here super() means no arg constructor of super class. This
applies to all constructors. In this program the commented
statements are kept by compiler during compilation. The super()
statement of constructor A() is calling no arg constructor of
Object.class.

Program 14:-
class A
{
A(int a)
{
System.out.println(“A(int)”);
}

}
class B extends A
{
int i;
B()
{
super(10);
System.out.println(“B()”);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

B(int a)
{
this();
System.out.println(“B(int)”);
}
}
class Manager
{
public static void main(String args[])
{
B b1 = new B(10);
System.out.println(“End of object creation”);
}
}
Output:
A(int)
B()
B(int)

Here in class Manager we are creating object with single


parameterized constructor ie B(int). Within it we have statement
this() as its first statement, so this() will call no arg constructor of
present class. In no arg constructor of present class we have a
statement super(10) as first statement, which will call single
parameterized constructor of super class A.
In A(int a) we have not kept super() or this() statement, so compiler
will by default keep super() byte code in .class file as its first
statement.

Program 15:-
class A
{
A()
{
this();
}
}

Program 16:-
class A
{
A()
{
this(10);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
A(int a)
{
this();
}
}

If we create object of class A using no arg constructor, in above


both cases, during compilation compiler will produce error as cyclic
calling. In case of constructors compiler is very cautious and will not
allow recursive calling of constructors where as in methods recursion
is allowed.

Program 17:-
class A extends A
{
}
The above program will generate compiler error, because of
duplication of same properties of class A.

Program 18:-
class A
{
}
The above program will compile successfully. But will produce
run time error because there is no main method inside it.

Definition
Superclass and subclass illustration
Inheritance rules
Constructor chain
Usage of ‘super’ and ‘this’ with constructors

Day 7 Review questions:-

1. What is Inheritance?
2. Give example for generalized and specialized form.
3. What is super class?
4. What is sub class?
5. Give example for super class and sub class.
6. How will you represent super class and sub class?
7. For what extend is used?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

8. What is stereotype?
9. Super class are called specialized class(T/F).
10.Sub class are called generalized class(T/F).
11.Super class are called generalized class(T/F).
12.Sub class are called specialized class(T/F).
13.In java whether it is possible develop one sub class from many
super class?
14.Constructor will be inherited to sub class.(T/F)
15.Whether multiple inheritance is supported by java or not?
16.Attributes inherited to the sub class(T/F).
17.What do you mean by object class?
18.Whether object class is super class or it’s a sub class?

19. Say whether compilation is success or not.


class B
{
int i;
}
class C extends B
{
}

20. Say whether compilation is success or not.


class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}

21. Say whether compilation is success or not


class A
{
A()
{
System.out.println(B);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class B extends A
{
B()
{
System.out.println(A);
}
}
classs C extends B
{
C()
{
System.out.println(C);
}
}

22. Say whether compilation is success or not


class A
{
A(int i)
{
System.out.println(A);
}
}
class B extends A
{
B()
{
System.out.println(B);
}
}

23.Say whether compilation is success or not


class A
{
A()
{
System.out.println(B);
}
}
class B extends A
{
B()
{
super(10);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(A);
}
}

24. Say whether compilation is success or not


class A
{
A(int i)
{
System.out.println(A);
}
}
class B extends A
{
B()
{
super(10) ;
System.out.println(B);
}
}

25. Say whether compilation is success or not.


class A
{
A(int i, int j)
{
System.out.println(A);
}
}

class B extends A
{
}

26.Say whether compilation is success or not.


class A
{
A(int i)
{
}
}
class B extends A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

B()
{
System.out.println(B);
}
}

27. Say whether compilation is success or not.


class A
{
A()
{
System.out.println(B);
}
A(int i)
{
this();
System.out.println(“A(int)”);
}
}

Rule 7 Do not use the long form of a word when a standard


short form exists.
28. Say whether compilation is success or not
class A
{
A()
{
this(10);
}
A(int i)
{
this();

}
}

29. Say whether compilation is success or not.


class A
{
A()
{
super(10);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

30. Say whether compilation is success or not.


class A
{
A()
{
this();
}
}

31. Say whether compilation is success or not


class A extends A
{

32. Say whether compilation is success or not.


class A
{
A()
{
System.out.println(“A”);
Super();
}
}

33.Say whether compilation is success or not


class A
{
A()
{
System.out.println(“A”);
}
A(int i)
{
System.out.println(“B”):
}
}

SCJP Oriented

Question 1
Given:
1. public class Plant {
080-41310124 http://www.javaeasytoall.com
Lara Technologies

2. private String name;


3. public Plant(String name) { this.name = name; }
4. public String getName() { return name; }
5. }
1. public class Tree extends Plant {
2. public void growFruit() { }
3. public void dropLeaves() { }
4. }
Which is true?
A. The code will compile without changes.
B. The code will compile if public Tree() { Plant(); } is added to the
Tree class.
C. The code will compile if public Plant() { Tree(); } is added to the
Plant class.
D. The code will compile if public Plant() { this(”fern”); } is added to
the Plant class.
E. The code will compile if public Plant() { Plant(”fern”); } is added
to
the Plant class.
Answer: D

Question 2
Assume that country is set for each class.
Given:
10. public class Money {
11. private String country, name;
12. public getCountry() { return country; }
13.}
and:
24. class Yen extends Money {
25. public String getCountry() { return super.country; }
26. }
27.
28. class Euro extends Money {
29. public String getCountry(String timeZone) {
30. return super.getCountry();
31. }
32. }
Which two are correct? (Choose two.)
A. Yen returns correct values.
B. Euro returns correct values.
C. An exception is thrown at runtime.
D. Yen and Euro both return correct values.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

E. Compilation fails because of an error at line 25.


F. Compilation fails because of an error at line 30.
Answer: BE

Question 3
Which Man class properly represents the relationship “Man has a
best
friend who is a Dog”?
A. class Man extends Dog { }
B. class Man implements Dog { }
C. class Man { private BestFriend dog; }
D. class Man { private Dog bestFriend; }
E. class Man { private Dog<bestFriend> }
F. class Man { private BestFriend<dog> }
Answer: D

Question 4
Given:
1. public interface A {
2. String DEFAULT_GREETING = “Hello World”;
3. public void method1();
4. }
A programmer wants to create an interface called B that has A as its
parent. Which interface declaration is correct?
A. public interface B extends A { }
B. public interface B implements A {}
C. public interface B instanceOf A {}
D. public interface B inheritsFrom A { }
Answer: A

Question 5
Given:
10. interface Data { public void load(); }
11. abstract class Info { public abstract void load(); }
Which class correctly uses the Data interface and Info class?
A. public class Employee extends Info implements Data {
public void load() { /*do something*/ }
}
B. public class Employee implements Info extends Data {
public void load() { /*do something*/ }
}
C. public class Employee extends Info implements Data {
public void load() { /*do something */ }
public void Info.load() { /*do something*/ }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
D. public class Employee implements Info extends Data {
public void Data.load() { /*d something */ }
public void load() { /*do something */ }
}
E. public class Employee implements Info extends Data {
public void load() { /*do something */ }
public void Info.load(){ /*do something*/ }
}
F. public class Employee extends Info implements Data{
public void Data.load() { /*do something*/ }
public void Info.load() { /*do something*/ }
}
Answer: A

Question 6
Given:
11. public abstract class Shape {
12. private int x;
13. private int y;
14. public abstract void draw();
15. public void setAnchor(int x, int y) {
16. this.x = x;
17. this.y = y;
18. }
19. }
Which two classes use the Shape class correctly? (Choose two.)
A. public class Circle implements Shape {
private int radius;
}
B. public abstract class Circle extends Shape {
private int radius;
}
C. public class Circle extends Shape {
private int radius;
public void draw();
}
D. public abstract class Circle implements Shape {
private int radius;
public void draw();
}
E. public class Circle extends Shape {
private int radius;
public void draw() {/* code here */}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
F. public abstract class Circle implements Shape {
private int radius;
public void draw() { / code here */ }
}
Answer: BE

Question 7
Click the Exhibit button.
1. public class GoTest {
2. public static void main(String[] args) {
3. Sente a = new Sente(); a.go();
4. Goban b = new Goban(); b.go();
5. Stone c = new Stone(); c.go();
6. }
7. }
8.
9. class Sente implements Go {
10. public void go() { System.out.println(”go in Sente.”); }
11. }
12.
13. class Goban extends Sente {
14. public void go() { System.out.println(”go in Goban”); }
15. }
16.
17. class Stone extends Goban implements Go { }
18.
19. interface Go { public void go(); }
What is the result?
A. go in Goban
go in Sente
go in Sente
B. go in Sente
go in Sente
go in Goban
C. go in Sente
go in Goban
go in Goban
D. go in Goban
go in Goban
go in Sente
E. Compilation fails because of an error in line 17.
Answer: C

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Question 8
Given:
10. class One {
11. public One() { System.out.print(1); }
12. }
13. class Two extends One {
14. public Two() { System.out.print(2); }
15. }
16. class Three extends Two {
17. public Three() { System.out.print(3); }
18. }
19. public class Numbers{
20. public static void main( String[] argv) { new Three(); }
21. }
What is the result when this code is executed?
A. 1
B. 3
C. 123
D. 321
E. The code rims with no output.
Answer: C

Question 9
Click the Exhibit button.
11. public class Bootchy {
12. int bootch;
13. String snootch;
14.
15. public Bootchy() {
16. this(”snootchy”);
17. System.out.print(”first “);
18. }
19.
20. public Bootchy(String snootch) {
21. this(420, “snootchy”);
22. System.out.print(”second “);
23. }
24.
25. public Bootchy(int bootch, String snootch) {
26. this.bootch = bootch;
27. this.snootch = snootch;
28. System.out.print(”third “);
29. }
30.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

31. public static void main(String[] args) {


32. Bootchy b = new Bootchy();
33. System.out.print(b.snootch +“ “ + b.bootch);
34. }
35. }
What is the result?
A. snootchy 420 third second first
B. snootchy 420 first second third
C. first second third snootchy 420
D. third second first siiootchy 420
E. third first second snootchy 420
F. first second first third snootchy 420
Answer: D

Question 10
Given:
1. class SuperClass {
2. public A getA() {
3. return new A();
4. }
5. }
6. class SubClass extends SuperClass {
7. public B getA() {
8. return new B();
9. }
10. }
Which is true?
A. Compilation will succeed if A extends B.
B. Compilation will succeed if B extends A.
C. Compilation will always fail because of an error in line 7.
D. Compilation will always fail because of an error in line 8.
Answer: B

Question 11
Given:
1. class ClassA {
2. public int numberOfinstances;
3. protected ClassA(int numberOfinstances) {
4. this.numberOflnstances = numberOfinstances;
5. }
6. }
7. public class ExtendedA extends ClassA {
8. private ExtendedA(int numberOfinstances) {
9. super(numberOflnstances);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

10. }
11. public static void main(String[] args) {
12. ExtendedA ext = new ExtendedA(420);
13. System.out.print(ext.numberOflnstances);
14. }
15. }
Which is true?
A. 420 is the output.
B. An exception is thrown at runtime.
C. All constructors must be declared public.
D. Constructors CANNOT use the private modifier.
E. Constructors CANNOT use the protected modifier.
Answer: A

Question 12
Click the Exhibit button.
1. public class Car {
2. private int wheelCount;
3. private String vin;
4. public Car(String vin) {
5. this.vin = vin;
6. this.wheelCount = 4;
7. }
8. public String drive() {
9. return “zoom-zoom”;
10. }
11. public String getInfo() {
12. return “VIN: “+ vin + “wheels: “+ wheelCount;
13. }
14. }
And:
1. public class MeGo extends Car {
2. public MeGo(String vin) {
3. this.wheelCount = 3;
4. }
5. }
What two must the programmer do to correct the compilation
errors?
(Choose two.)
A. insert a call to this() in the Car constructor
B. insert a call to this() in the MeGo constructor
C. insert a call to super() in the MeGo constructor
D. insert a call to super(vin) in the MeGo constructor
E. change the wheelCount variable in Car to protected
080-41310124 http://www.javaeasytoall.com
Lara Technologies

F. change line 3 in the MeGo class to super.wheelCount = 3;


Answer: DE

Question 13
Which four are true? (Choose four.)
A. Has-a relationships should never be encapsulated.
B. Has-a relationships should be implemented using inheritance.
C. Has-a relationships can be implemented using instance variables.
D. Is-a relationships can be implemented using the extends keyword.
E. Is-a relationships can be implemented using the implements
keyword.
F. The relationship between Movie and Actress is an example of an is-
a
relationship.
G. An array or a collection can be used to implement a one-to-many
has-a relationship.
Answer: CDEG

Question 14
Which two are true about has-a and is-a relationships? (Choose two.)
A. Inheritance represents an is-a relationship.
B. Inheritance represents a has-a relationship.
C. Interfaces must be used when creating a has-a relationship.
D. Instance variables can be used when creating a has-a relationship.
Answer: AD

Question 15
Given:
10. interface Jumper { public void jump(); }
......
20. class Animal {}
......
30. class Dog extends Animal {
31. Tail tail;
32. }
......
40. class Beagle extends Dog implements Jumper {
41. public void jump() { }
42. }
.......
50. class Cat implements Jumper {
51. public void jump() { }
52. }
Which three are true? (Choose three.)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A. Cat is-a Animal


B. Cat is-a Jumper
C. Dog is-a Animal
D. Dog is-a Jumper
E. Cat has-a Animal
F. Beagle has-a Tail
G. Beagle has-a Jumper
Answer: BCF

Question 16
3. Given:
5. class Atom{
6. Atom(){System.out.println(”atom”);}
7. }
8. class Rock extends Atom {
9. Rock(String type){System.out.println(type);}
10. }
11. public class Mountain extends Rock{
12. Mountain(){
13. super(“granite”);
14. new Rock(“granite”);
15. }
16. public static void main(String args[]){new Mountain();}
17.}
What is the result?
A. Compilation fails.
B. atom granite
C. granite granite
D. atom granite granite
E. An Exception is thrown at runtime
F. atom granite atom granite
Answer: F

Question 17
Given:
11. class Mammal{}
12.
13. class Raccoon extends Mammal{
14. Mammal m = new Mammal();
15. }
16.
17. class BabyRaccoon extends Mammal{}
Which four statements are true?(Choose four)
A. Raccoon is-a Mammal.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

B. Raccoon has-a Mammal.


C. BabyRaccoon is-a Mammal.
D. BabyRaccoon is-a Raccoon.
E. BabyRaccoon has-a Mammal
F. BabyRaccon is-a BabyRaccoon.
Answer: A, B, C, F

Question 17
Given
11. public interface A{public void m1();}
12.
13. class B implements A{}
14. class C implements A{public void m1(){}}
15. class D implements A{public void m1(int x){}}
16. abstract class E implements A{}
17. abstract class F implements A{public void m1(){}}
18. abstract class G implements A{public void m1(int x){}}
What is the result?
A. compilation succeeds.
B. Exactly one class does not compile.
C. Exactly two classes do not compile.
D. Exactly four classes do not compile.
E. Exactly three classes do not compile.
Answer: C

Question 18
Given:
5. class Thingy {Meter m = new Meter();}
6. class Component { void go(){System.out.println(“c”);}}
7. class Meter extends Component {void go()
{System.out.println(“m”);}}
8.
9. class DeluxeThingy extends Thingy{
10. public static void main(String args[]){
11. DeluxeThingy dt = new DeluxeThingy();
12. dt.m.go();
13. Thingy t = new DeluxeThingy();
14. t.m.go();
15. }
16. }
Which two are true?(choose two)
B. The output is mm.
C. The output is mc
D. Component is-a meter.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

E. Component has-a meter.


F. DeluxeThingy is-a Component.
G. DeluxeThingy has-a Component.
Answer: A,F

Rule 7 Do not use the long form of a word when a standard


short form exists.

Day 8
Static attributes and methods:
‘static’ is a keyword used to make attributes or methods,
common to every object. Non-static attributes and methods are
specific to objects where as static are common to all objects because
they are class members.

Program 1:-
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a =new A();
A a1 =new A();
A a2 =new A();
A a3 =new A();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
If I want to know how many objects are created for a class
programmatically the above program is not enough because the ‘i’
variable is specific to object. If we maintain any variable that is
global to a class, then only it is possible to find out the number of
objects created.

Static members are global and are common to all objects of the same
class.

Program 2:-
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
{
B b1 = new B();
B b2 = new B();
B b3 = new B();
B b4 = new B();
b1.i = 19;
System.out.println(b2.i);
}
}
Static members will not be allocated in object. Only non-static
members will be present in object.

Rule 8 Always include units in a name when the name refers to an


entity with units.
Program 3:-
class C
{
int i;
C()//no-arg constructor
{
i++;
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

C(int k) //1-arg constructor


{
i++;
}
}

class Manager
{
public static void main(String args[])
{
C c1 = new C();
C c2 = new C();
C c3 = new C();
C c4 = new C();
System.out.println(c4.i);
}
}

Program 4:-
class D
{
static int i;
D()//no-arg constructor
{
i++;
}
D(int k) //1-arg constructor
{
i++;
}
}

class Manager
{
public static void main(String args[])
{
D d1 = new D();
D d2 = new D();
D d3 = new D();
D d4 = new D();
D d5 = new D();
System.out.println(d5.i);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Program 5:-
class A
{
static int i;
}
class Manager
{
public static void main(String args[])
{
A.i = 19;
A a2 = new A();
System.out.println(a2.i);
}
}

Since ‘i’ is static we don’t need to create an object or reference


variable that hold an object. If we use reference variable, compiler
automatically modifies variable to class name and gives a warning
message that static members needs to be accessed by the class name
but not the reference variable.

Program 6:-
class A
{
static void test()
{
System.out.println(“I am in test method”);
}
}
class Manager
{
public static void main(String s[])
{
A a1 = new A();
A a2 = new A();
A a3 = new A();
A1.test();
A2.test();
A3.test();
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Program 7:-
class B
{
int i;
static int j;
B(int i)
{
this.i = i;
}
void test()
{
System.out.println(i);
System.out.println(j);
}
void test1()
{
System.out.println(j);
}

public static void main(String arg[])


{
B b1 = new B(15);
B b2 = new B(20);
b1.test();
b1.test1();
b2.test();
b2.test1();
}
}

For accessing non-static members we need an object as it depends on


instance variables. In static methods we cannot access non static
members. Inside static methods we cannot use ‘this’ or ‘super’
keywords.

Program 8:-
class A
{
static int counter;
A()
{
counter++;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A(int i)
{
counter++;
}
A(int i, int j)
{
counter++;
}
}
class Manager
{
public static void main(String args[])
{
A a1 =new A();
A a2 =new A(10);
A a3 =new A(10,15);
A a4 =new A();
System.out.println(A.counter);
}
}

Initialization Block:-
Initialization blocks are the executables codes Initialization
blocks will not be inherited. There are 2 types of initialization blocks
available.
1. Static initialization blocks.
2. Instance initialization blocks.

Instance initialization blocks (IIB’s).


IIB’s will be executed during object creation in the same order
they have been written in a class.

Program 9:-
class A
{
static int counter;
A()
{
}
A(int i)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
A(int i, int j)
{
}
{
counter++;
}
}

To avoid the counter incrementing statement again and again in all


constructors we will use instance initialization block.

Instance initialization block is a block of code which will be executed,


whenever we create an object of that class irrespective of whatever
constructor we might use for object creation.

Instance Initialization Block gets executed for every object creation,


immaterial of constructor used for creation.

class A
{
static int count; //default value of count will be 0.

A()
{
count++;
}
A(int i)
{
count++;
}
A(int i, int j)
{
count++;
}
static void show()
{
System.out.println(“Inside static method of A”);
}
}
class Manager
{
public static void main(String[] args)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
A o1 = new A();
A o2 = new A(10);
A o3 = new A(10,20);
System.out.println(“No of objects created:”+A.count);
A.show();
}
}
O/P: No of objects created:3
Inside static method of A

As static members are common to all objects, whenever we are


creating new object, value of count will be incremented. Thus the
value of static attributes is same to all objects.We can access static
members using class name as well as object reference. But accessing
using class name is preferable, because if we access it using object
reference then compiler will change that object reference to its class
name during compilation.
‘static’ members are called as class members, non-static
members are called as instance members.
We can declare a method as static if its behavior is not
changing object wise. Constructors cannot be static.

class A
{
int i;
static int j;
public static void main(String[] args)
{
1------- System.out.println(i);
2------- System.out.println(A.j);
}
}
Non static members cannot be accessed from static context.
Compiler will generate error during compilation if we do so.In the
above program, 1st SOP statement will produce compiler error, where
as 2nd will not. Non static members can be accessed inside static
context through object reference.
We can access both static and non static members inside a non
static method. static members will not be on heap because they are
class members.

Static definition
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Static Attributes
Initialization block

Day 8 Review Questions:-

1.Class using non-static attribute- write the output


class A
{
int i;
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=19;
System.out.println (a2.i);
}
}

2.Class using static attribute – write the output


class B
{
static int i;
}
class Manager
{
public static void main (String args[])
{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=19;
System.out.println (b2.i);
}
}

3. write the output


class C
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
int i;
C()
{
i++;
}
C(int k)
{
i++;
}
}
class Manager
{
public static void main(String args[])
{
C c1=new C();
C c2=new C();
C c3=new C();
C c4=new C();
c1.i=19;
System.out.println (c4.i);
}
}

4. write the output


class D
{
static int i;
D()
{
i++;
}
D(int k)
{
i++;
}
}
class Manager
{
public static void main(String args[])
{
D d1=new D();
D d2=new D();
D d3=new D();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

D d4=new D();
D d4=new D();
System.out.println (d5.i);
}
}

5.write the output


class D
{
static int i;

D()
{
i++;
}
D(int k)
{
}
}
class Manager
{
public static void main (String args[])
{
D d1=new D();
D d2=new D();
D d3=new D();
D d4=new D();
D d4=new D();
System.out.println (d5.i);
}
}

6.write the output


class A
{
static int i;
}
class Manager
{
public static void main (String args[])
{
A.i=19;
System.out.println (A.i);
A a1=new A();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println (a1.i);
A a2= new A();
System.out.println (a2.i);
}
}

7.what are static members?


8.what are non static members?
9.what are instance members?
10.what are class members?
11.write the output
class A
{
static void test()
{
System.out.println (“I am from test”);
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.test();
a2.test ();
a3.test();
}
}

12.write the output


class B
{
int i;
static int j;
B(int i)
{
this.i=I;
}
void test()
{
System.out.println(i);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(j);
}
void test1()
{
System.out.println (j);
}
public static void main (String args[])
{
B a1=new B(15);
B a2=new B(20);
b1.test();
b1.test1();
b2.test ();
b2.test1();
}
}
13. why this is called as an object member?
14. what is this?
15. wheather we can access non static member inside a static
members?
16. what happens if we access access non static member inside a static
members?
17 wheather we can use this() or super() statis member?

18. write the output


class A
{
static int counter++;
A()
{
counter++;
}
(int i)
{
counter++;
}
A(int I, int j)
{
counter++;
}
}
class Manager
{
public static void main (String args[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
A a1=new A();
A a2=new A(10);
A a3=new A(10,15);
A a4=new A();
System.out.println (A.counter);
}
}

19.write the output


class A
{
static int counter++;
A()
{
call counter++;
}
A(int i)
{
call counter++;
}
A(int i, int j)
{
call counter++;
}

void call counter()


{
counter++;
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A a2=new A(10);
A a3=new A(10,15);
A a4=new A();
System.out.println (A.counter);
}
}

20. What is instance initialization block?


080-41310124 http://www.javaeasytoall.com
Lara Technologies

21. Whether instance initialization is executed for every creation of


object from any of the constructor?
22. Whenever object is creating by default, only one constructor is
called.(T/F)
23. How many instance initialization block can be kept inside a class?
24. Compiler will keep the calling statement for instance initialization
block(T/F)
25. Whether instance initialization block is a definition block?
26. Inside non static member we can declare class members well as
object members (T/F)

27Write output for the following programs:


1. class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=20;
System.out.println("a1.i="+a1.i);
}
}

2.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
a2.i=34;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println("a1.i="+a1.i);
System.out.println("a2.i="+a2.i);
}
}

3.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=10;
a2.i=24;
System.out.println("a1.i="+a1.i);
System.out.println("a2.i="+a2.i);
System.out.println("a3.i="+a3.i);
System.out.println("a2.i="+a2.i);
}
}

4.
class B
{
int i;
}
class Manager
{
public static void main(String args[])
{
B a1=new B();
B b2=new B();
B a3=new B();
B a4=new B();
a1.i=15;
System.out.println("b2.i="+b2.i);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

5.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a2.i="+a2.i);
}
}

6.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a2.i="+a2.i);
System.out.println("a3.i="+a3.i);
}
}

7.
class A
{
int i;
}
class Manager
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a1.i="+a1.i);
System.out.println("a2.i="+a2.i);
System.out.println("a3.i="+a3.i);
System.out.println("a4.i="+a4.i);
}
}

8.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a1.i="+a1.i);
System.out.println("a2.i="+a2.i);
System.out.println("a3.i="+a3.i);
System.out.println("a4.i="+a4.i);
}
}

9.
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=23;
System.out.println("b1.i="+b1.i);
}
}

10.
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=23;
System.out.println("b1.i="+b1.i);
System.out.println("b2.i="+b2.i);
}
}

11.
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=23;
b2.i=45;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println("b1.i="+b1.i);
System.out.println("b2.i="+b2.i);
System.out.println("b3.i="+b3.i);
System.out.println("b2.i="+b2.i);
}
}

14.
class A
{
static int i=10;
static
{
A a1=new A();
a1.i=10;
System.out.println(a1.i);
}

public static void main(String args[])


{
A a1=new A();
System.out.println("a1.i="+a1.i);
}
}

15.
class A
{
static int i=20;
static
{
A a1=new A();
a1.i=30;
System.out.println(a1.i);
}

public static void main(String args[])


{
A a1=new A();
System.out.println("a1.i="+a1.i);
}
}

16.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class A
{
int i=20;
static
{
A a1=new A();
a1.i=30;
System.out.println(a1.i);
}

public static void main(String args[])


{
A a1=new A();
System.out.println("a1.i="+a1.i);
}
}

17.
class A
{
int i=20;
static

{
A a1=new A();
a1.i=0;
System.out.println(a1.i);
}
public static void main(String args[])
{
A a1=new A();
System.out.println("a1.i="+a1.i);
}
}

28.Write output for following programs:


1.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a1.i="+a1.i);
}
}

2.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
a2.i=34;
System.out.println("a1.i="+a1.i);
System.out.println("a2.i="+a2.i);
}
}

3.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

a2.i=34;
System.out.println("a1.i="+a1.i);
System.out.println("a2.i="+a2.i);
System.out.println("a3.i="+a3.i);
System.out.println("a2.i="+a2.i);
}
}

4.
class B
{
int i;
}
class Manager
{
public static void main(String args[])
{
B a1=new B();
B b2=new B();
B a3=new B();
B a4=new B();
a1.i=23;
System.out.println("b2.i="+b2.i);
}
}

5.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a2.i="+a2.i);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

6.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a2.i="+a2.i);
System.out.println("a3.i="+a3.i);
}
}

7.
class A
{
int i;
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a1.i="+a1.i);
System.out.println("a2.i="+a2.i);
System.out.println("a3.i="+a3.i);
System.out.println("a4.i="+a4.i);
}
}

8.
class A
{
int i;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
System.out.println("a1.i="+a1.i);
System.out.println("a2.i="+a2.i);
System.out.println("a3.i="+a3.i);
System.out.println("a4.i="+a4.i);
}
}

9.
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=23;
System.out.println("b1.i="+b1.i);

}
}

10.
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=23;
System.out.println("b1.i="+b1.i);
System.out.println("b2.i="+b2.i);
}
}

11.
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=23;
b2.i=45;
System.out.println("b1.i="+b1.i);
System.out.println("b2.i="+b2.i);
System.out.println("b3.i="+b3.i);
System.out.println("b2.i="+b2.i);
}
}

12.
class A
{
static int i=10;
static
{
A a1=new A();
a1.i=10;
System.out.println(a1.i);
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String args[])


{
A a1=new A();
System.out.println("a1.i="+a1.i);
}
}

13.
class A
{
static int i=20;
static
{
A a1=new A();
a1.i=30;
System.out.println(a1.i);
}
public static void main(String args[])
{
A a1=new A();
System.out.println("a1.i="+a1.i);
}
}

14.
class A
{
int i=20;
static
{
A a1=new A();
a1.i=30;
System.out.println(a1.i);
}
public static void main(String args[])
{
A a1=new A();
System.out.println("a1.i="+a1.i);
}
}

15.
class A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

int i=20;
static
{
A a1=new A();
a1.i=0;
System.out.println(a1.i);
}
public static void main(String args[])
{
A a1=new A();
System.out.println("a1.i="+a1.i);
}
}

SCJP Oriented
Question 1
Which two code fragments correctly create and initialize a static
array
of int elements? (Choose two.)
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2] { 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }
Answer: AB

Question 2
Given:
10. class Foo {
11. static void alpha() { /* more code here */ }
12. void beta() { /* more code here */ }
13. }
Which two are true? (Choose two.)
A. Foo.beta() is a valid invocation of beta().
B. Foo.alpha() is a valid invocation of alpha().
C. Method beta() can directly call method alpha().
D. Method alpha() can directly call method beta().
Answer: BC

Question 3
147. Given:
1. public class Base {
2. public static final String FOO = “foo”;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

3. public static void main(String[] args) {


4. Base b = new Base();
5. Sub s = new Sub();
6. System.out.print(Base.FOO);
7. System.out.print(Sub.FOO);
8. System.out.print(b.FOO);
9. System.out.print(s.FOO);
10. System.out.print(((Base)s).FOO);
11. } }
12. class Sub extends Base {public static final String FOO=bar;}
What is the result?
A. foofoofoofoofoo
B. foobarfoobarbar
C. foobarfoofoofoo
D. foobarfoobarfoo
E. barbarbarbarbar
F. foofoofoobarbar
G. foofoofoobarfoo
Answer: D

Question 4
Which three statements are true? (Choose three.)
A. A final method in class X can be abstract if and only if X is
abstract.
B. A protected method in class X can be overridden by any subclass
of X.
C. A private static method can be called only within other static
methods in class X.
D. A non-static public final method in class X can be overridden in
any subclass of X.
E. A public static method in class X can be called by a subclass of X
without explicitly referencing the class X.
F. A method with the same signature as a private final method in
class X can be implemented in a subclass of X.
G. A protected method in class X can be overridden by a subclass of
A only if the subclass is in the same package as X.
Answer: BEF

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Day 9
1.class A
{
int i;
{
System.out.println(1);
}
{
System.out.println(2);
}
void test()
{
System.out.println(3);
}
A()
{
System.out.println(4);
}
{
System.out.println(5);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
class Manager
{
public static void main(String args[])
{
A a1 =new A();
A a2 =new A();
}
}

2.class A
{
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
}

A(int i)
{
System.out.println(4);
}
{
System.out.println(5);
A(int i, int j)
{
this(1);
System.out.println(6);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
System.out.println(“A”);
A a1 = new A(10, 15);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(“B”);
A a1 = new A(10);
System.out.println(“C”);
}
}

3.class A
{
{
System.out.println(1);
}
A()
{
super();
System.out.println(2);
}
{
System.out.println(3);
}
A(int i, int j)
{
this();
System.out.println(4);
}
A(int i)
{
super();
System.out.println(5);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
A a2 = new A(10, 15);
A a3 = new A(15);
A a4 = new A(11,12);
}
}

4.class A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
A(int i)
{
System.out.println(4);
}
A(int i, int j)
{
this(1);
System.out.println(5);
}
{
System.out.println(6);
}
Rule 9 Do not use acronyms in names unless the acronym is
industry-standard or is defined in user documentation.
}
class B extends A
{
{
System.out.println(7);
}
B()
{
System.out.println(8);
}
B(int i)
{
System.out.println(9);
}
{
System.out.println(10);
}
B(int i, int j)
{
super();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(5);
}
B(double d)
{
super(10, 15);
System.out.println(12);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
System.out.println(“A1”);
B b1 = new B(10);
System.out.println(“B1”);
B b2 = new B();
System.out.println(“B2”);
A a2 = new A(10);
System.out.println(“A2”);
A a3 = new A(10, 15);
System.out.println(“A3”);
B b3 = new B(1, 2);
System.out.println(“B1”);
B b4 = new B(10.0);
}
}

Constructor body is divided into 3 parts


1) Super class constructor calling for constructor chaining
2) Callings to instance initialization blocks
3) Then the constructors body
Instance initialization will not be inherited to sub classes

class Manager
{
static
{
System.out.println(1);
}
public static void main(String args[])
{
System.out.println(2);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class Manager
{
static
{
System.out.println(1);
}
public static void main(String args[])
{
System.out.println(2);
Manager.test();
System.out.println(3);
}
static
{
System.out.println(4);
}
public void test()
{
System.out.println(5);
}
}

class A
{
static
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
{
System.out.println(4);
}
void test()
{
System.out.println(5);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

static void check()


{
System.out.println(6);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
A.check();
a1.test();
A a2 = new A();
a2.test();
A.check();
A a3 = new A();
A.check();
a3.test();
}
static
{
System.out.println(“begin”);
}
}

Initialization block
Static Initialization block

Day 9 Review Questions:-

1.write the output for this program

class A
{
int i;
{
System.out.println(“1”);
}
int j;
{
System.out.println(“2”);
}
void test()
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(“3”);
}
A()
{
System.out.println(“4”);
}
{
System.out.println(“5”);
}

class Manager
{
public static void main (String args[])
{
A a1=new A();
A a2=new A();
}
}

2.write the output for this program

class A
{
int i;
{
System.out.println(“1”);
}
int j;
{
System.out.println(“2”);
}
void test()
{
System.out.println(“3”);
}
A()
{
System.out.println(“4”);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(“5”);
}

class Manager
{
public static void main (String args[])
{
A a1=new A();

}
}

3.write the output for this program


class A
{
int i;
{
System.out.println(“1”);
}
int j;
{
System.out.println(“2”);
}
void test()
{
System.out.println(“3”);
}
A()
{
System.out.println(“4”);
}
{
System.out.println(“5”);
}
A(int i)
{
System.out.println(“6”);
}
}
class Manager
{
public static void main (String args[])
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A a1=new A();
A a2=new A(10);
}
}

4.write the output for this program


class A
{
int i;
{
System.out.println(“1”);
}
int j;
{
System.out.println(“2”);
}
void test()
{
System.out.println(“3”);
}
A()
{
System.out.println(“4”);
}
{
System.out.println(“5”);
}
A(int i)
{
System.out.println(“6”);
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
}
}

5.write the output for this program


class A
{
int i;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(“1”);
}
int j;
{
System.out.println(“2”);
}
void test()
{
System.out.println(“3”);
}
A()
{
System.out.println(“4”);
}
{
System.out.println(“5”);
}
A(int i)
{
System.out.println(“6”);
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A a2=new A(12,15);
}

6. write the output for this program


class A
{
System.out.println(“1”);
}
A()
{
System.out.println(“2”);
}
{
System.out.println(“3”);
}
A(int i)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(“4”);
}
{
System.out.println(“5”);
}
A(int i ,int j)
{
this(i);
System.out.println(“6”);
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
System.out.println(“__________”);
A a2=new A(10,15);
System.out.println(“__________”);
A a2=new A(10);
}
}

7 write the output for this program


class A
{
{
System.out.println(“1”);
}
A()
{
System.out.println(“2”);
}
{
System.out.println(“3”);
}
A(int i, int j)
{
System.out.println(“4”);
}
A(int i )
{
super();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(“5”);
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A a2=new A(10,15);
A a3=new A(10);
A a4=new A(10,20);
}
}

8 write the output for this program


class A
{
{
System.out.println(“1”);
}
A()
{
System.out.println(“2”);
}
{
System.out.println(“3”);
}
A(int i)
{
System.out.println(“4”);
}

A(int i , int j)
{
this(i);
System.out.println(“5”);
}
{
System.out.println(“6”);
}
}

class B extends A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(“7”);
}
B()
{
System.out.println(“8”);
}
B(int i)
{
System.out.println(“9”);
}
{
System.out.println(“10”);
}
B(int i, int j)
{
System.out.println(“11”);
}
B(double d)
{
super();
System.out.println(“12”);
}
}

class Manager
{
public static void main (String args[])
{
A a1=new A();
System.out.println(“________”);
B b1=new B();
System.out.println(“________”);
B b2=new B(10);
System.out.println(“________”);
A a2=new A(10);
System.out.println(“________”);
A a3=new A(10,14);
System.out.println(“________”);
B b3=new B(11,20);
B b4=new B(10.5);
}
}

9 write the output for this program


080-41310124 http://www.javaeasytoall.com
Lara Technologies

class Manager
{
static
{
System.out.println(“1”);
}
public static void main (String args[])
{
System.out.println(“2”);
}
}

10. write the output for this program


class Manager
{
static
{
System.out.println(“1”);
}
public static void main (String args[])
{
System.out.println(“2”)
manager test2();
System.out.println(“3”);
}
static
{
System.out.println(“4”);
}
static void test()
{
System.out.println(“5”);
}
}

11. write the output for this program


class A
{
{
System.out.println(“1”);
}
A()
{
System.out.println(“2”);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
{
System.out.println(“3”);
}
{
System.out.println(“4”);
}
void test()
{
System.out.println(“5”);
}
static void check()
{
System.out.println(“6”);
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A. check();
a1. test();
A a2=new A();
a1. test();
A . check();
A a3=new A();
A3. test();
A . check();
static
{
System.out.println(“begin”);
}
}

12. write the output for this program


class A
{
{
System.out.println(“1”);
}
A()
{
System.out.println(“2”);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(“3”);
}
{
System.out.println(“4”);
}
void test()
{
System.out.println(“5”);
}
static void check()
{
System.out.println(“6”);
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A. check();
a1. test();
A a2=new A();
a1. test();
A . check();
A a3=new A();
A3. test();
A . check();
static
{
System.out.println(“begin”);
}
}
class manager1
{
public static void main (String args[])
{
A.check();
}
}
class Manager2
{
public static void main (String args[])
{
Manager.main(args);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Manager1.main(args);
}
}
13.Write the output for this program:
class A
{
int i;
{
System.out.println(1);
}
int j;
{
System.out.println(2);
}
void test()
{
System.out.println(3);
}
A()
{
System.out.println(4);
}
{
System.out.println(5);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A();
}
}

14. Write the output for this program


class A
{
int i;
{
System.out.println(1);
}
int j;
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(2);
}
void test()
{
System.out.println(3);
}
A()
{
{
System.out.println(4);
}
{
System.out.println(5);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
}
}

15. Write the output for this program


class A
{
int i;
{
System.out.println(1);
}
int j;
{
System.out.println(2);
}
void test()
{
System.out.println(3);
}
A()
{
System.out.println(4);
}
{
System.out.println(5);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
A(int i)
{
System.out.println(6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A(10);
}
}
16. Write the output for this program
class A
{
int i;
{
System.out.println(1);
}
int j;
{
System.out.println(2);
}
void test()
{
System.out.println(3);
}
A()
{
System.out.println(4);
}
{
System.out.println(5);
}
A(int i)
{
System.out.println(6);
}
}
class Manager
{
public static void main(String[] args)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
A a1=new A();
}
}

17. Write the output for this program


class A
{
int i;
{
System.out.println(1);
}
int j;
{
System.out.println(2);
}
void test()
{
System.out.println(3);
}
A()
{
System.out.println(4);
}
{
System.out.println(5);
}
A(int i)
{
System.out.println(6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
A a3=new A(12,15);
}
}

18. Write the output for this program


class A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
A(int i)
{
System.out.println(4);
}
{
System.out.println(5);
}
A(int i,int j)
{
this(i);
System.out.println(6);
}
}

class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println("-------");
A a2=new A(10,12);
System.out.println("-------");
A a3=new A(10);
}
}

19. Write the output for this program


class A
{
{
System.out.println(1);
}
A()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(2);
}
{
System.out.println(3);
}
A(int i)
{
System.out.println(4);
}
{
System.out.println(5);
}
A(int i,int j)
{
this(i);
System.out.println(6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println("-------");
A a2=new A(10,12);
System.out.println("-------");
A a3=new A(10);
}
}
20. Write the output for this program
class A
{
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
A(int i)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(4);
}
{
System.out.println(5);
}
A(int i,int j)
{
this(i);
System.out.println(6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println("-------");
A a2=new A(10,12);
System.out.println("-------");
A a3=new A(10);
System.out.println("-------");
A a4=new A(10);
}
}
21. Write the output for this program
class A
{
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
A(int i,int j)
{
this();
System.out.println(4);
}
A(int i)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

super();
System.out.println(5);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println("-------");
A a2=new A(10,12);
System.out.println("-------");
A a3=new A(10);
System.out.println("-------");
A a4=new A(10,12);
}
}

22. Write the output for this program


class A
{
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
A(int i)
{

System.out.println(4);
}
A(int i,int j)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

this(i);
System.out.println(5);
}
{
System.out.println(6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println("-------");
A a2=new A(10,12);
System.out.println("-------");
A a3=new A(10);
System.out.println("-------");
A a4=new A(10,12);
System.out.println("-------");
A a5=new A(10);
}
}

23. Write the output for this program


class A
{
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
A(int i)
{

System.out.println(4);
}
A(int i,int j)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

this(i);
System.out.println(5);
}
{
System.out.println(6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println("-------");
A a2=new A(10,12);
System.out.println("-------");
A a3=new A(10);
System.out.println("-------");
A a4=new A(10,12);
System.out.println("-------");
A a5=new A(10);
}
}

24. Write the output for this program


class A
{
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
A(int i)
{
System.out.println("4");
}
A(int i,int j)
{
this(i);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println("5");
}
{
System.out.println("6");
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println("-------");
A a2=new A(10,12);
System.out.println("-------");
A a3=new A(10);
System.out.println("-------");
A a4=new A(10,12);
System.out.println("-------");
A a5=new A(10);
}
}

25. Write the output for this program


class A
{
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
A(int i)
{

System.out.println("4");
}
A(int i,int j)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

this(i);
System.out.println("5");
}
{
System.out.println("6");
}

}
class B extends A
{
{
System.out.println(7);
}
B()
{
System.out.println(8);
}
B(int i)
{
System.out.println(9);
}
{
System.out.println(10);
}
B(int i,int j)
{
super(i);
System.out.println("11");
}
B(double d)
{
super(10,17);
System.out.println("12");
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println("-------");
B b1=new B(10);
System.out.println("-------");
B b2=new B();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println("-------");
A a2=new A(12);
System.out.println("-------");
A a3=new A(12,10);
System.out.println("-------");
B b3=new B(1,2);
B b4=new B(10.3);
}
}

26. Write the output for this program


class Manager
{
static
{
System.out.println(1);
}
public static void main(String args[])
{
System.out.println(2);
Manager.test();
System.out.println(3);
}
static
{
System.out.println("4");
}
static void test()
{
System.out.println("5");
}
}

27. Write the output for this program


class A
{
static
{
System.out.println(1);
}

A()
{
System.out.println(2);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
{
System.out.println(3);
}
{
System.out.println(4);
}
void test()
{
System.out.println(5);
}
static void check()
{
System.out.println(6);
}
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
System.out.println("--------");
A.check();
System.out.println("--------");
a1.test();
System.out.println("--------");
A a2=new A();
System.out.println("--------");
a2.test();
System.out.println("--------");
A.check();
System.out.println("--------");
A a3=new A();
System.out.println("--------");
A.check();
System.out.println("--------");
a3.test();
}
static
{
System.out.println("begin");
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

28. Write the output for this program


class A
{
static
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
{
System.out.println(4);
}
void test()
{
System.out.println(5);
}
static void check()
{
System.out.println(6);
}
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
System.out.println("--------");
A.check();
System.out.println("--------");
a1.test();
System.out.println("--------");
A a2=new A();
System.out.println("--------");
a2.test();
System.out.println("--------");
A.check();
System.out.println("--------");
A a3=new A();
System.out.println("--------");
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A.check();
System.out.println("--------");
a3.test();
}
static
{
System.out.println("begin");
}
}
class Manager1
{
public static void main(String args[])
{
A.check();
}
}

29. Write the output for this program


class A
{
static
{
System.out.println(1);
}

A()

{
System.out.println(2);
}
{
System.out.println(3);
}
{
System.out.println(4);
}
void test()
{
System.out.println(5);
}
static void check()
{
System.out.println(6);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class Manager1
{
public static void main(String args[])
{
A.check();
}
}

30. Write the output for this program


class A
{
static
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
{
System.out.println(4);
}
void test()
{
System.out.println(5);
}
static void check()
{
System.out.println(6);
}
}
class Manager1
{
public static void main(String args[])
{
A.check();
}
}
class Manager
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String args[])


{
A a1=new A();
System.out.println("--------");
A.check();
System.out.println("--------");
a1.test();
System.out.println("--------");
A a2=new A();
System.out.println("--------");
a2.test();
System.out.println("--------");
A.check();
System.out.println("--------");
A a3=new A();
System.out.println("--------");
A.check();
System.out.println("--------");
a3.test();
}
static
{
System.out.println("begin");

}
}
class Manager2
{
public static void main(String args[])
{
Manager.main(args);
Manager1.main(args);
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Day 10

class A
{
static int count; //default value of count will be 0.
A()
{
}
A(int i)
{
}
A(int i, int j)
{
}
//IIB Starting.
{
System.out.println(“IIB called “ + ++count + “time”);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

class Manager
{
public static void main(String[] args)
{
A o1 = new A();
A o2 = new A(10);
A o3 = new A(10,20);
System.out.println(“No of objects created: ”+A.count);
}
}

Output:
IIB called 1time
IIB called 2time
IIB called 3time
No of objects created: 3

Here Instance initialization block is executed during each


instance creation. In program 18, we are calling same statement in all
the constructors. If we have requirement to execute same statements
during each object creation, then it is better to go for IIB.

class A
{
A()
{
//super();
//calling statements to IIB’s
System.out.println(“A()”);
}
A(int i)
{
this();
System.out.println(“A(int)”);
}
{
System.out.println(“IIB-A”);
}
}
class Manager
{
public static void main(String args[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
A a1 = new A();
A a2 = new A(10);
//B b1 = new B();
}
}

Output:
IIB-A
A()
IIB-A
A(int)

In the above program the commented statements will be kept


by compiler during compilation as byte code.When we are compiling,
compiler will ensure few contracts.

They are,
1) Compiler will make sure that every class is a sub class of
java.lang.Object class
2) It will ensure at least one constructor for every class.
3) It will check first line of every constructor. If there is no super()
or this() then it will keep one no arg super() as first statement.
4) If there are IIBs in class then compiler will keep calling
statements to IIBs just after super() statement inside constructor.
However if first statement is this() then compiler will not keep any
calling statements.

class A
{
A()
{
//super();
//calling statements to IIB’s
System.out.println(“A()”);
}
{
System.out.println(“IIB-A”);
}
}
}
class B extends A
{
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(“IIB-B”);
}
}
class Manager
{
public static void main(String args[])
{
B b1 = new B();
}
}
Output:
IIB-A
A()
IIB-B

In the above program when we create object to class B then


default constructor of class B will be called. The first statement in
that default constructor is super() which is kept by compiler. Thus
no-arg constructor of super class A will be called. During execution of
no-arg constructor of class A, first Object class no-arg constructor is
executed, then calling statements to IIBs of class A will be executed.
Rule 10 Always treat acronyms as words when applying other
naming rules.
Then other statements of constructor A() will be executed. Then IIBs
of class B will be executed after which other statements of
constructor B() if any will be executed.

Static Initialization Block(SIB):


SIBs will be executed when we refer a class for the first time.
SIBs will be executed only once in the order they have been written in
the program.

class A
{
static
{
System.out.println(“Inside static block”);
}
public static void main(String args[])
{
System.out.println(“Hello World!”);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Output:
Inside static block
Hello World!
In the above program when we use java command for
execution then we are referring class for first time through memory.
Thus static blocks will be executed first and then other statements.

class A
{
static
{
System.out.println(“Inside static block of A”);
}
}
class B extends A
{
static
{
System.out.println(“Inside static block of B”);
}
}

class Manager
{
public static void main(String[] args)
{
B b1 = new B();
}
}
O/P: Inside static block of A
Inside static block of B

In the program when we create object of B then we are


referring class B for first time. But class B extends class A thus static
block of super class will be executed first. Then static block of B is
executed.

Order of Executing SIBs & IIBs


1) First all SIBs will be executed, in the order they have been
written, only once.
2) All IIBs will be executed after the execution of SIBs, in the
order they have been written, every time during object creating.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Scenarios during which SIBs will execute:


1) Whenever running class.
2) Whenever calling static members
3) Whenever creating object to class
4) During running sub class
5) Whenever using sub class for creating object.

Overloading:
We can keep as many as constructors or methods as we wish as
long as the signature of constructor or method differs.

Method signature.
1) Method name
2) Argument list
3) Order of argument list.

Return type is not a part of method signature.


We have seen constructor overloading previously in program number
5.

class Person
{
int test()
{
System.out.println(“test()”);
}
int test(int a)
{
System.out.println(“test(int)”);
}

int test(int a, double b)


{
System.out.println(“test(int,int)”);
}
}
class PersonManager
{
public static void main(String args[])
{
Person per = new Person();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

per.test();
per.test(10);
per.test(10,20);
}
}
Output:
test()
test(int)
test(int,int)

class A
{
static
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
}
class B extends A
{
static
{
System.out.println(4);
}
B()
{
System.out.println(5);
}
{
System.out.println(6);
}
}
class C extends B
{
static
{
System.out.println(7);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

C()
{
System.out.println(8);
}
{
System.out.println(9);
}
}

class Manager
{
static
{
System.out.println(10);
}
public static void main(String args[])
{
C c1 =new C();
}
}
class Manager1
{
static
{
System.out.println(11);
}
public static void main(String args[])
{
A a1 =new A();
Manager.main(args);
}
}

class Manager
{
static
{
System.out.println(1);
}
public static void main(String args[])
{
System.out.println(2);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class Manager1 extends Manager


{
static
{
System.out.println(3);
}
public static void main(String args[])
{
System.out.println(4);
}
}

class Test
{
static void test()
{
System.out.println(1);
}
static void test(int i)
{
System.out.println(2);
}
static void test(byte b)
{
System.out.println(3);
}
static void test(double d)
{
System.out.println(4);
}
}
class Manager
{
public static void main(String args[])
{
Test.test(10);
Test.test(10.0);
Byte b = 10;
Test.test(b);
Float f1 = 10.0f;
Test.test(b);
Test.test(f1);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Long l = 10l;
Test.test(l);
}
}

Keeping more the one method with same name and different
signature in the same class is known as “Method Overloading”.
In the overloading concept, we don’t need to concentrate on the
return type of the method.

“Early Binding” and “Static Binding” are one and the same. Early
binding is happened at compilation itself. If the binding is done later
on at the runtime, then it is known as “Late Binding” or “Dynamic
Binding”

Auto casting or Auto widening:-

byte , short, int, long, float, double[increasing order]


conversion of one data type to another data type is called “Casting”.
Casting can be done automatically only in the case of Auto widening
i.e. auto casting is casting the narrow value into wider value or lower
value into higher.
Only numeric data types can be involved in Auto Widening.
In case of converting the upper data type to lower data type, the
casting is not done automatically, but it should be done explicitly.

byte b = 10;
double d = b; // upcasting done automatically i.e. Auto Casting.
class Manager
{
static void test(int i)
{
System.out.println(“ int “);
}
public static void main(String args[])
{
Manager.test(10.0);
}
}

class Manager
{
public static void main(String args[])
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

test((byte)10);
test((short)10);
test((int)10);
test((float)10);
test((long)10);
test((double)10);
}
static void test(float f)
{
System.out.println(“ float “);
}
}

class A
{
void test()
{
System.out.println(1);
}
void test(int i)
{
System.out.println(2);
}
void test(int i, double j)
{
System.out.println(3);
}
}

class Manager
{
public static void main(String args[])
{
A a1 = new A();
A1.test();
byte b = 10;
a1.test(b);
a1.test(10, 10.0);
}
}

In the above example, the methods are non static methods and so
these can be called only through the reference variable, binding in
this example is dynamic binding.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class A
{
void test()
{
System.out.println(“hello”);
}
}

class B extends A
{
void test()
{
System.out.println(“hello”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
A a2 = new B();
B b1 = new A();
a1.test();
a2.test();
b1.test();
}
}

Method Overriding:-
Overriding means giving the new definitions to the existing method
or inherited method.
Upcating can be happened automatically, but down casting should be
done explicitly.

Mystery with SIB, IIB and constructor with inheritance


Method overloading
Rules of method overloading
Method calling
Auto widening
Casting
Primitive casting(implicit)
Primitive casting(explicit)
Widening
Narrowing
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Static binding incase of static methods


Static binding incase of non-static methods
Method Overriding
Derived casting(implicit)

1. What do you mean by method overloading?


2. What do you mean by early binding?
3. What do you mean by late binding?
4. What is static binding?
5. What is dynamic binding?
6. What is compilation?
7. What is casting?
8. What is AutoCasting?
9. What is wideling?
10.What do you mean by method overriding?
11. Write the output for this program
class A
{
static
{
System.out.println(1);
}
A()
{
System.out.println(2);
}
{
System.out.println(3);
}
}
class B extends A
{
static
{
System.out.println(4);
}
B()
{
System.out.println(5);
}
{
System.out.println(6);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class C extends B
{
static
{
System.out.println(7);
}
C()
{
System.out.println(8);
}
{
System.out.println(9);
}
}
class Manager
{
static
{
System.out.println(10);
}
public static void main(String[] args)
{
C c1=new C();
}
}
class Manager1
{
static
{
System.out.println(11);
}
public static void main(String args[])
{
A a1=new A();
Manager.main(args);
}
}
12. Write the output for following program:
class Manager
{
static
{
System.out.println("1");
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
public static void main(String[] args)
{
System.out.println("2");
}
}
class Manager1 extends Manager
{
static
{
System.out.println("3");
}
public static void main(String[] args)
{
System.out.println(4);
}
}

13. Write the output for following program:


class Manager
{

public static void main(String[] args)


{
System.out.println("2");
}
}
class Manager1 extends Manager
{
static
{
System.out.println("3");
}
public static void main(String[] args)
{
System.out.println(4);
}
}

14. Write the output for following program:


class Manager
{
static
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println("1");
}
{
System.out.println("5");
}
public static void main(String[] args)
{
System.out.println("2");
}
}
class Manager1 extends Manager
{
static
{
System.out.println("3");
}
{
System.out.println("6");
}
public static void main(String[] args)
{
System.out.println(4);
}
{
System.out.println("7");
}
}

15. Write the output for following program:


class Manager
{
static
{
System.out.println("1");
System.out.println("8");
}
{
System.out.println("5");
}
public static void main(String[] args)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println("2");
}
}
class Manager1 extends Manager
{
static
{
System.out.println("3");
}
{
System.out.println("6");
}
public static void main(String[] args)
{
System.out.println(4);
}
{
System.out.println("7");
}
}

16. Write the output for following program:

class Manager
{
static
{
System.out.println("1");
System.out.println("8");
}
public static void main(String[] args)
{
System.out.println("2");
System.out.println("5");
}
}

class Manager1 extends Manager


{
static
{
System.out.println("3");
}
public static void main(String[] args)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println("4");
System.out.println("6");
}

17 .Write the output for following program:


class Manager
{
static
{
System.out.println("1");
System.out.println("8");
}
Manager ()
{
System.out.println("9")
}

public static void main(String[] args)


{
Manager m1=new Manager()
System.out.println("2");
System.out.println("5");
}
}

class Manager1 extends Manager


{
static
{
System.out.println("3");
}
public static void main(String[] args)
{
System.out.println("4");
System.out.println("6");
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

18 .Write the output for following program:


class Test
{
static void test()
{
System.out.println(1);
}
static void test(int i)
{
System.out.println(2);
}
static void test(byte b)
{
System.out.println(3);
}
static void test(double d)
{
System.out.println(4);
}
}
class Manager
{
public static void main(String[] args)
{
Test.test(10);
Test.test(10.9);
byte b=10;
float f1=10.00f;
Test.test(b);
Test.test(f1);
long l1=123l;
Test.test(l1);
}
}

19. Write the output for following program:


class Test
{
void test()
{
System.out.println(1);
}
static void test(int i)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(2);
}
static void test(short b)
{
System.out.println(3);
}
static void test(double d)
{
System.out.println(4);
}
}
class Manager
{
public static void main(String[] args)
{
Test.test(10);
Test.test(10.9);
byte b=10;
float f1=10.00f;
Test.test(b);
Test.test(f1);
long l1=123l;
Test.test(l1);
}
}

20 .Write the output for following program:

class Manager
{
static void test(int i)
{
System.out.println("int");
}
public static void main(String args[])
{
Manager.test((int)10.0);
}
}

21. Write the output for following program:

class Manager
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
static void test(int i)
{
System.out.println("int");
}
public static void main(String args[])
{
Manager.test(10.0);
}
}

22. Write the output for following program:

class Manager
{
public static void main(String[] args)
{
test((byte)10);
test((short)10);
test((int)10);
test((float)10);
test((long)10);
test((double)10);
}
static void test(float f)
{
System.out.println("float");
}
}

23. Write the output for following program:

class Manager
{
public static void main(String[] args)
{
test((byte)10);
test((short)10);
test((int)10);
test((float)10);
test((long)10);
test((byte)10);
}
static void test(float f)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println("float");
}
}

24. Write the output for following program:

class Manager
{
public static void main(String[] args)
{
test((byte)10);
test((short)10);
test((int)10);
test((float)10);
test((long)10);
test((short(double))10);
}
static void test(float f)
{
System.out.println("float");
}
}

25. Write the output for following program:

class A
{
void test()
{
System.out.println(1);
}
void test(int i)
{
System.out.println(2);
}
void test(int k,double j)
{
System.out.println(3);
}
}

class Manager
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
public static void main(String[] args)
{
A a1=new A();
a1.test();
byte b=10;
a1.test(b);
a1.test(10,10.00);
}
}

26. Write the output for following program:

class A
{
void test()
{
System.out.println(1);
}
void test(int i)
{
System.out.println(2);
}
void test(int k,double j)
{
System.out.println(3);
}
}

class Manager
{
public static void main(String[] args)
{
A a1=new A();
a1.test();
byte b=10;
a1.test((int)b);
a1.test(10,10.00);
}
}

27. Write the output for following program:

080-41310124 http://www.javaeasytoall.com
Lara Technologies

class A
{
public void m1()
{
m2();
}
public void m2()
{
m1();
}

public static void main(String args[])


{
A a1 = new A();
a1.m1();
}
}

SCJP Oriented

Question 19
Given:
1. class TestA {
2. public void start() { System.out.println(”TestA”); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println(”TestB”); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B

Question 12
12. Given:
13. public class Pass {
14. public static void main(String [1 args) {
15. int x 5;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

16. Pass p = new Pass();


17. p.doStuff(x);
18. System.out.print(” main x = “+ x);
19. }
20.
21. void doStuff(int x) {
22. System.out.print(” doStuff x = “+ x++);
23. }
24. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuffx = 6 main x = 6
D. doStuffx = 5 main x = 5
E. doStuffx = 5 main x = 6
F. doStuffx = 6 main x = 5
Answer: D

Question 28
Click the Exhibit button.
1. public class Test {
2. int x= 12;
3. public void method(int x) {
4. x+=x;
5. System.out.println(x);
6. }
7. }
Given:
34. Test t = new Test();
35. t.method(5);
What is the output from line 5 of the Test class?
A. 5
B. 10
C. 12
D. 17
E. 24
Answer: B

Question 41
41. Given:
10. class One {
11. public One foo() { return this; }
12. }
13. class Two extends One {
080-41310124 http://www.javaeasytoall.com
Lara Technologies

14. public One foo() { return this; }


15. }
16. class Three extends Two {
17. // insert method here
18. }
Which two methods, inserted individually, correctly complete the
Three
class? (Choose two.)
A. public void foo() { }
B. public int foo() { return 3; }
C. public Two foo() { return this; }
D. public One foo() { return this; }
E. public Object foo() { return this; }
Answer: CD

Question 43
Click the Exhibit button.
1. public interface A {
2. public void doSomething(String thing);
3. }
1. public class AImpl implements A {
2. public void doSomething(String msg) { }
3. }
1. public class B {
2. public A doit() {
3. // more code here
4. }
5.
6. public String execute() {
7. // more code here
8. }
9. }
1. public class C extends B {
2. public AImpl doit() {
3. // more code here
4. }
5.
6. public Object execute() {
7. // more code here
8. }
9. }
Which statement is true about the classes and interfaces in the
exhibit?
A. Compilation will succeed for all classes and interfaces.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

B. Compilation of class C will fail because of an error in line 2.


C. Compilation of class C will fail because of an error in line 6.
D. Compilation of class AImpl will fail because of an error in line 2.
Answer: C

Question 45
Given:
1. public class A {
2. public void doit() {
3. }
4. public String doit() {
5. return “a”;
6. }
7. public double doit(int x) {
8. return 1.0;
9. }
10.}
What is the result?
A. An exception is thrown at runtime.
B. Compilation fails because of an error in line 7.
C. Compilation fails because of an error in line 4.
D. Compilation succeeds and no runtime errors with class A occur.
Answer: C

Question 93
Given:
11. public class Yikes {
12.
13. public static void go(Long n) {System.out.println(”Long “);}
14. public static void go(Short n) {System.out.println(”Short “);}
15. public static void go(int n) {System.out.println(”int “);}
16. public static void main(String [] args) {
17. short y= 6;
18. long z= 7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. int Long
B. Short Long
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Question 94
Given:
12. public class Wow {
13. public static void go(short n) {System.out.println(”short”); }
14. public static void go(Short n) {System.out.println(”SHORT”);}
15. public static void go(Long n) {System.out.println(” LONG”); }
16. public static void main(String [] args) {
17. Short y= 6;
18.int z=7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. short LONG
B. SHORT LONG
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: C

Question 151
Click the Exhibit button.
1. public class SimpleCalc {
2. public int value;
3. public void calculate() { value += 7; }
4. }
And:
1. public class MultiCalc extends SimpleCalc {
2. public void calculate() { value -= 3; }
3. public void calculate(int multiplier) {
4. calculate();
5. super.calculate();
6. value *=multiplier;
7. }
8. public static void main(String[] args) {
9. MultiCalc calculator = new MultiCalc();
10. calculator.calculate(2);
11. System.out.println(”Value is: “+ calculator.value);
12. }
13. }
What is the result?
A. Value is: 8
B. Compilation fails.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

C. Value is: 12
D. Value is: -12
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: A

Question 200
Given the command line java Pass2 and:
15. public class Pass2 {
16. public void main(String [] args) {
17.int x=6;
18. Pass2 p = new Pass2();
19. p.doStuff(x);
20. System.out.print(” main x = “+ x);
21. }
22.
23. void doStuff(int x) {
24. System.out.print(” doStuffx = “+ x++);
25. }
26. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuffx = 6 main x = 6
D. doStuffx = 6 main x = 7
E. doStuffx = 7 main x = 6
F. doStuffx = 7 main x = 7
Answer: B

Question 205
Click the Exhibit button.
1. public class Item {
2. private String desc;
3. public String getDescription() { return desc; }
4. public void setDescription(String d) { desc = d; }
5.
6. public static void modifyDesc(Item item, String desc) {
7. item = new Item();
8. item.setDescription(desc);
9. }
10. public static void main(String[] args) {
11. Item it = new Item();
12. it.setDescription(”Gobstopper”);
13. Item it2 = new Item();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

14. it2.setDescription(”Fizzylifting”);
15. modifyDesc(it, “Scrumdiddlyumptious”);
16. System.out.println(it.getDescription());
17. System.out.println(it2.getDescription());
18. }
19. }
What is the outcome of the code?
A. Compilation fails.
B. Gobstopper
Fizzylifting
C. Gobstopper
Scrumdiddlyumptious
D. Scrumdiddlyumptious
Fizzylifltng
E. Scrumdiddlyumptious
Scrumdiddlyumptious
Answer: B

Question 206
Given:
11. public class ItemTest {
12. private final mt id;
13. public ItemTest(int id) { this.id = id; }
14. public void updateId(int newId) { id = newId; }
15.
16. public static void main(String[] args) {
17. ItemTest fa = new ItemTest(42);
18. fa.updateId(69);
19. System.out.println(fa.id);
20. }
21. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The attribute id in the Item object remains unchanged.
D. The attribute id in the Item object is modified to the new value.
E. A new Item object is created with the preferred value in the id
attribute.
Answer: A

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 10 Always treat acronyms as words when applying other


naming rules.
Day 11
Method Overriding
Program 1:-
class A
{
void test()
{
System.out.println(“hello”);
}
}
Class B extends A
{
void test()
{
System.out.println(“Changed hello”);
}
}

class Manager
{
public static void main(String args[])
{
A a1 = new A();
A a2 = new B();
A all[] = new A[2];
all[0] = a1;
all[1] = a2;
for(int i = 0; i < all.length; i++)
{
all[i].test();
}
}
}

Super class array can take reference variable of the sub class objects
also.
Static binding can be done at the time of compilation and dynamic
binding is done at runtime. Only through method overriding we can
achieve polymorphism.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Polymorphism means exhibiting different behavior in different


situations or conditions. Dynamic binding and polymorphism is one
and the same.

Method overriding Rules:-


Signature must be same for all the methods
Return type must also be the same as the super class method possess.
Program 2:-
class Account
{
int getBalance()
{
Statements st1;

Rule 11 Always capitalize the first letter of a type.

Statements st2;
Statements st3;
_
_
_
Statements stn;

return 100;
}

class CreditAccount extends Account


{
int getBalance()
{
Statements st1;
Statements st2;
Statements st3;
_
_
_
Statements stn;

return 2000;
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

class DebitAccount extends Account


{
int getBalance()
{
Statements st1;
Statements st2;
Statements st3;
_
_
_
Statements stn;

return 500;
}
}

class Manager
{
public static void main(String args[])
{
DebitAccount da = new DebitAccount ();
CreditAccount ca = new CreditAccount ();

Rule 11 Always capitalize the first letter of a type.


Account all[] = new Account [2];
all[0] = da;
all[1] = ca;
for(int i = 0; i < all.length; i++)
{
all[i].getBalance();
}
}
}
POLYMORPHISM:

Overriding:
It is a mechanism in which super class methods can be
redefined in sub classes according to their usage.

Polymorphism is exhibit using method overriding. Static methods


doesn’t involve in polymorphism because static methods cannot be
overridden but just inherited to sub classes.
Program 3:-
class A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
void test()
{
System.out.println(“A”);
}
}

class B extends A
{
void test()
{
System.out.println(“B”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
B b1 = new B();
a1.test();
b1.test();
}
}
Output:
A
B
Here test() method of class A is overridden in class B. In case of
over riding return type, method signature must be same. Access level
in over riding should be same or wider.
Over riding of static methods is not possible however static methods
can be inherited and redefined. Depending on the type object test()
method will be called.

Basic overriding rule:


1) super class object reference can refer to object of sub class, where
as sub class object reference can’t refer to object of super class.

Program 4:-
class A
{
void test()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(“A”);
}
}
class B extends A
{
void test()
{
System.out.println(“B”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
A a2 = new B();
B b1 = new B();
//B b2 = new A();
a1.test();
a2.test();
b1.test();
//b2.test();
}
}
Output:
A
B
B

Here a1 is object reference of type class A, which is referring to


the object of class A.
a2 is object reference of type class A, which is referring to the object
of class B, which is a sub class of class A, according to our 1 st basic
rule super class object reference can refer sub class object . b1 is
object reference of type class B, which is referring to the object of
class B. Here our test() method is overridden when we call test() using
reference a2 & b1, test method of class B will be called since both
object references are referring to object of class B.

Program 5:-
class A
{
void test()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(“A”);
}
}
class B extends A
{
void test()
{
System.out.println(“B”);
}
}
class Manager
{
public static void main(String args[])
{
B b2 = new A();
b2.test();
}
}
Output:
Compile time Error.
We can’t refer super class object by using subclass
reference.
Program 6:-
class A
{
int i;
}
class B extends A
{
void test()
{
System.out.println(“B”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new B();
a1.test();
}
}
Output:
Compile time Error. Can’t find test() in class A.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

During compilation of this program, compiler will check for


calling members depending on the object reference type (ie class A)
but not depending on the type of object it is referring. (ie class B).

Basic overriding rule:


2) super class array can hold sub class object references. Vice-versa is
not possible.

Program 7:-
class A
{
void test()
{
System.out.println(“A”);
}
}
class B extends A
{
void test()
{
System.out.println(“B”);
}
}
class Manager
{
public static void main(String args[])
{
A all[] = new A[3];
A a1 = new A();
A a2 = new B();
B a1 = new B();
all[0] = a1;
all[1] = a2;
all[2] = b1;
for( int i=0; i<all.length; i++)
{
all[i].test();
}
}
}
Output:
A
B
080-41310124 http://www.javaeasytoall.com
Lara Technologies

In the above program we are storing object references of super


class as well as of sub class inside super class array. By using a for
loop we are iterating through the elements of array and we calling
test() method corresponding to each object reference. Thus the test
method is called dynamically here at run time corresponding to each
object references. Thus the output of the above program changes
dynamically. This behavior is polymorphism.
Thus same statement calling different methods (overridden)
from different classes in different scenario is called polymorphism.
Polymorphism is happening because of overriding.Polymorphism is
also called as dynamic binding or late binding.
Program 8:-
class A
{
static void test()
{
System.out.println(“A”);
}
}
class B extends A
{
static void test()
{
System.out.println(“B”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
B b1 = new B();
A a2 = new B();
a1.test();
b1.test();
a2.test();
}
}
Output:
A
B
A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Thus static methods can not be overridden. They can be


redefined as in above program.

Program 9:-
class A
{
void test()
{
System.out.println(“A”);
}
}
class B extends A
{
int test()
{
System.out.println(“B”);
return 100;
}
}
class Manager
{
public static void main(String args[])
{
A b1 = new B();
b1.test();
}
}
Output:
compile time error.

Here overriding is not happening, because to override any


method in sub class return type and method signature should be
same. In this program return type of both test() method is miss
matching.

Program 10:-
class A
{
public void test()
{
System.out.println(“A”);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class B extends A
{
void test()
{
System.out.println(“B”);
}
}

class Manager
{
public static void main(String args[])
{
A b1 = new B();
b1.test();
}
}
Output:
Compile time error.

Access level in over riding should be same or wider. In this


program access level of test() of class A is public and access level of
test() of class B is default ie access level of sub class test() method is
neither same nor wider than super class test() method so we can’t
override.

The above program is modified to achieve proper overriding of


test() method.

Program 11:-
class A
{
void test()
{
System.out.println(“A”);
}
}
class B extends A
{
public void test()
{
System.out.println(“B”);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class Manager
{
public static void main(String args[])
{
A b1 = new B();
b1.test();
}
}

Output:
B.

Method Overriding
Polymorphism

Day 11 Review Questions:-

1. What do you mean by classcast exceptions?


2. Content of object cannot be change only datatype is
change(T/F).
3. Static cannot be overrided(T/F)
4. What do you mean by polymorphism.
5. Whether override is possible in polymorphism?
6. What is method overriding?
7. Only through dynamic binding polymorphism occurs(T/F).

8. Write the output for the program:


:
class A
{
void test()
{
System.out.println("A");
}
}
class B extends A
{
void test()
{
System.out.println("B");
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class Manager
{
public static void main(String args[])
{
A a1=new A();
A a2=new B();
A all[]=new A[2];
all[0]=a1;
all[1]=a2;
for (int i=0;i<all.length ; i++ )
{
all[i].test();
}
}
}

9. Write the output for the program


class A
{
void test()
{
System.out.println("A");
}
}
class B extends A
{
void test()
{
System.out.println("B");
}
}
class Manager1
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
A all[]=new A[2];
all[0]=a1;
all[1]=a2;
for (int i=0;i<all.length ; i++ )
{
all[i].test();
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}

10. Write the output for the program


class A
{
void test()
{
System.out.println("A");
}
}
class B extends A
{
void test()
{
System.out.println("B");
}
}
class Manager2
{
public static void main(String args[])
{
A a1=new A();
B a2=new A();
A all[]=new A[2];
all[0]=a1;
all[1]=a2;
for (int i=0;i<all.length ; i++ )
{
all[i].test();
}
}
}

11. Write the output for the program


class A
{
void test()
{
System.out.println("A");
}
}
class B extends A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

void test()
{
System.out.println("B");
}
}
class Manager3
{
public static void main(String args[])
{
A a1=new B();
A a2=new A();
A all[]=new A[2];
all[0]=a1;
all[1]=a2;
for (int i=0;i<all.length ; i++ )
{
all[i].test();
}
}
}

12. Write the output for the program

class Account
{
int getBalance()
{
return 100;
}
}
class CreditAccount extends Account
{
int getBalance()
{
return 1000;
}
}
class DebitAccount extends Account
{
int getBalance()
{
return 2000;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class Manager4
{
public static void main(String args[])
{
Account all[]=new Account[2];
CreditAccount ca=new CreditAccount();
DebitAccount da=new DebitAccount();
all[0]=ca;
all[1]=da;
for (int i=0;i<all.length ; i++ )
{
System.out.println(all[i].getBalance());
}
}
}

13. Write the output for the program


class Account
{
int getBalance()
{
return 100;
}
}
class CreditAccount extends Account
{
int getBalance()
{
return 1000;
}
}
class DebitAccount extends Account
{
int getBalance()
{
return 2000;
}
}
class Manager5
{
public static void main(String args[])
{
Account all[]=new Account[2];
080-41310124 http://www.javaeasytoall.com
Lara Technologies

DebitAccount da=new DebitAccount();


CreditAccount ca=new CreditAccount();
all[0]=ca;
all[1]=da;
for (int i=0;i<all.length ; i++ )
{
System.out.println(all[i].getBalance());
}
}
}

14. Write the output for the program


class Account
{
int getBalance()
{
return 100;
}
}
class CreditAccount extends Account
{
int getBalance()
{
return 1000;
}
}
class DebitAccount extends Account
{
int getBalance()
{
return 2000;
}
}
class Manager6
{
public static void main(String args[])
{
Account all[]=new Account[2];
DebitAccount da=new DebitAccount();
CreditAccount ca=new CreditAccount();
all[0]=da;
all[1]=ca;
for (int i=0;i<all.length ; i++ )
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(all[i].getBalance());
}
}
}

15. Write the output for the program


class Account
{
int getBalance()
{
return 100;
}
}
class CreditAccount extends Account
{
int getBalance()
{
return 1000;
}
}
class DebitAccount extends Account
{
int getBalance()
{
return 2000;
}
}
class Manager7
{
public static void main(String args[])
{
Account all[]=new Account[2];
DebitAccount da=new DebitAccount();
CreditAccount ca=new CreditAccount();
all[0]=da;
all[1]=ca;
for (int i=1;i<all.length ; i++ )
{
System.out.println(all[i].getBalance());
}
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

16. Write the output for the program


class Account
{
int getBalance()
{
return 100;
}
}
class CreditAccount extends Account
{
int getBalance()
{
return 1000;
}
}
class DebitAccount extends Account
{
int getBalance()
{
return 2000;
}
}
class Manager8
{
public static void main(String args[])
{
Account all[]=new Account[2];
DebitAccount da=new DebitAccount();
CreditAccount ca=new CreditAccount();
all[0]=da;
all[1]=ca;
for (int i=1;i<all.length ; i-- )
{
System.out.println(all[i].getBalance());
}
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

17. Write the output for the program


class Account
{
int getBalance()
{
return 100;
}
}
class CreditAccount extends Account
{
int getBalance()
{
return 1000;
}
}
class DebitAccount extends Account
{
int getBalance()
{
return 2000;
}
}
class Manager8
{
public static void main(String args[])
{
Account all[]=new Account[2];
DebitAccount da=new DebitAccount();
CreditAccount ca=new CreditAccount();
all[0]=da;
all[1]=ca;
for (int i=0;i<all.length ; i-- )
{
System.out.println(all[i].getBalance());
}
}
}

18. Write the output for the program class A


{
static void test()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println("A");
}
}
class B extends A
{
static void test()
{
System.out.println("B");
}
}
class Manager10
{
public static void main(String args[])
{
A all[]=new A[2];
A a1=new A();
A a2=new B();

all[0]=a1;
all[1]=a2;
for (int i=0;i<all.length ; i++ )
{
all[i].test();
}
}
}

19. Write the output for the program


class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}

class Manager11
{
public static void main(String args[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
A a1=new A();
System.out.println(a1.i);
System.out.println(a1.j);
System.out.println(a1.k);
A a2=new B();
System.out.println(a2.i);
System.out.println(a2.j);
System.out.println(a2.k);
A a3=new C();
System.out.println(a3.i);
System.out.println(a3.j);
System.out.println(a3.k);
}
}

20. Write the output for the program

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}

class Manager12
{
public static void main(String args[])
{
A a1=new A();
System.out.println(a1.i);
A a2=new B();
System.out.println(a2.i);
A a3=new C();
System.out.println(a3.i);

}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

21. Write the output for the program

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}

class Manager13
{
public static void main(String args[])
{
A a1=new B();
B b1=new C();
System.out.println(a1.i);
System.out.println(b1.i);
System.out.println(b1.j);
C c1=new C();
System.out.println(c1.i);
System.out.println(c1.j);
System.out.println(c1.k);
}
}

22. Write the output for the program

class A
{
int i;
}
class B extends A
{
int j;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class C extends B
{
int k;
}

class Manager13
{
public static void main(String args[])
{
B b1=new A();
System.out.println(a1.i);
System.out.println(b1.j);
System.out.println(b1.k);
}
}

23. Write the output for the program

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}

class Manager15
{
public static void main(String args[])
{
B b1=(B) new A();
System.out.println(b1.i);
System.out.println(b1.j);
System.out.println(b1.k);
}
}

24. Write the output for the program


080-41310124 http://www.javaeasytoall.com
Lara Technologies

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}

class Manager16
{
public static void main(String args[])
{
B b1=(B) new A();
System.out.println(b1.i);
System.out.println(b1.j);
}
}

25. Write the output for the program


class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}

class Manager17
{
public static void main(String args[])
{
B b1= new C();
A a1=b1;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

C c1=(C)a1;
System.out.println(c1.k);
}
}

26. Write the output for the program

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}

class Manager17
{
public static void main(String args[])
{
B b1= new C();
A a1=b1;
C c1=(C)a1;
System.out.println(c1.k);
}
}

27. Write the output for the program

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class Manager19
{
public static void main(String args[])
{
B a= new B();
A a1=a;
B b2= (B)a1;
System.out.println(b2.j);
}
}
SCJP Oriented

Question 1
Given:
11. class Animal { public String noise() { return “peep”; } }
12. class Dog extends Animal {
13. public String noise() { return “bark”; }
14. }
15. class Cat extends Animal {
16. public String noise() { return “meow”; }
17. }
.....
30. Animal animal = new Dog();
31. Cat cat = (Cat)animal;
32. System.out.printIn(cat.noise());
What is the result?
A. peep
B. bark
C. meow
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: E

Question 2
Given:
11. abstract class Vehicle { public int speed() { return 0; } }
12. class Car extends Vehicle { public int speed() { return 60; } }
13. class RaceCar extends Car { public int speed() { return 150; }}
......
21. RaceCar racer = new RaceCar();
22. Car car = new RaceCar();
23. Vehicle vehicle = new RaceCar();
24. System.out.println(racer.speed() + “, ‘ + car.speed()
080-41310124 http://www.javaeasytoall.com
Lara Technologies

25. + “, “+ vehicle.speed());
What is the result?
A. 0, 0,0
B. 150, 60, 0
C. Compilation fails.
D. 150, 150, 150
E. An exception is thrown at runtime.
Answer: D

Question 3
Given:
10. abstract class A {
11. abstract void al();
12. void a2() { }
13. }
14. class B extends A {
15. void a1() { }
16. void a2() { }
17. }
18. class C extends B { void c1() { } }
and:
A x = new B(); C y = new C(); A z = new C();
Which four are valid examples of polymorphic method calls? (Choose
four.)
A. x.a2();
B. z.a2();
C. z.c1();
D. z.a1();
E. y.c1();
F. x.a1();
Answer: ABDF
Question 4
Given:
1. interface A { public void aMethod(); }
2. interface B { public void bMethod(); }
3. interface C extends A,B { public void cMethod(); }
4. class D implements B {
5. public void bMethod() { }
6. }
7. class E extends D implements C {
8. public void aMethod() { }
9. public void bMethod() { }
10. public void cMethod() { }
11. }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

What is the result?


A. Compilation fails because of an error in line 3.
B. Compilation fails because of an error in line 7.
C. Compilation fails because of an error in line 9.
D. If you define D e = new E(), then e.bMethod() invokes the version
of bMethod() defined in Line 5.
E. If you define D e = (D)(new E()), then e.bMethod() invokes the
version of bMethod() defined in Line 5.
F. If you define D e = (D)(new E()), then e.bMethod() invokes the
version of bMethod() defined in Line 9.
Answer: F

Question 5
Given:
10. interface A { public int getValue() }
11. class B implements A {
12. public int getValue() { return 1; }
13. }
14. class C extends B {
15. // insert code here
16. }
Which three code fragments, inserted individually at line 15, make
use
of polymorphism? (Choose three.)
A. public void add(C c) { c.getValue(); }
B. public void add(B b) { b.getValue(); }
C. public void add(A a) { a.getValue(); }
D. public void add(A a, B b) { a.getValue(); }
E. public void add(C c1, C c2) { c1.getValue(); }
Answer: BCD

Question 6
Given:
1. public class Blip {
2. protected int blipvert(int x) { return 0; }
3. }
4. class Vert extends Blip {
5. // insert code here
6. }
Which five methods, inserted independently at line 5, will compile?
(Choose five.)
A. public int blipvert(int x) { return 0; }
B. private int blipvert(int x) { return 0; }
C. private int blipvert(long x) { return 0; }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

D. protected long blipvert(int x) { return 0; }


E. protected int blipvert(long x) { return 0; }
F. protected long blipvert(long x) { return 0; }
G. protected long blipvert(int x, int y) { return 0; }
Answer: ACEFG

Question 7
Given:
10. public class Foo {
11. public int a;
12. public Foo() { a = 3; }
13. public void addFive() { a += 5; }
14. }
and:
20. public class Bar extends Foo {
21. public int a;
22. public Bar() { a = 8; }
23. public void addFive() { this.a +=5; }
24. }
invoked with:
30. Foo foo = new Bar();
31. foo.addFive();
32. System.out.println(”Value: “+ foo.a);
What is the result?
A. Value: 3
B. Value: 8
C. Value: 13
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: A

Question 8
Given:
10. public class SuperCaic {
11. protected static int multiply(int a, int b) { return a * b; }
12. }
and:
20. public class SubCalc extends SuperCalc {
21. public static int multiply(int a, int b) {
22. int c = super.multiply(a, b);
23. return c;
24. }
25. }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

and:
30. SubCalc sc = new SubCalc();
31. System.out.println(sc.multiply(3,4));
32. System.out.println(SubCalc.multiply(2,2));
What is the result?
A. 12
4
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 21.
E. Compilation fails because of an error in line 22.
F. Compilation fails because of an error in line 31.
Answer: E

Question 9
2. Given:
11. class Alpha{
12. public void foo(){System.out.println(“Afoo”);}
13. }
14. public class Beta extends Alpha{
15. public void foo(){System.out.println(”Bfoo”);}
16. public static void main(Sting args[]){
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();
20.b.foo();
What is the result?
A. Afoo Afoo.
B. Afoo Bfoo.
C. Bfoo Afoo.
D. Bfoo Bfoo.
E. Compilation fails.
F. An Excpetion is thrown at runtime.
Answer: D

Rule 11 Always capitalize the first letter of a type.


Day 12

class A{ }
class B extends A{ }
class C extends B{ }
class Manager
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String args[])


{
A all = new A[10];
B b1 = new B();
A a1 = new B();
C c1 = new C();
A a2 = new C();
B b2 = new C();
}
}

class A
{
static void test()
{
System.out.println(“A”);
}
}

class B extends A
{
static void test()
{
System.out.println(“B”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
A a2 = new B();
A all[] = new A[2];
all[0] = a1;
all[1] = a2;
for(int i = 0; i < all.length; i++)
{
all[i].test();
}
}
}
Rule 12 Do not include any prefix or suffix to indicate that a name is
for any specific type.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

At the time of compilation all[i].test(); is modified into A.test(); . so


they can’t be overridden(means static methods).

class A
{
int i;
}

class B extends A
{
int j;
}

class C extends B
{
int k;
}

class Manager
{
public static void main(String args[])
{
A a1 = new A();
System.out.println(a1.i);

A a2 = new B();
System.out.println(a2.i);
System.out.println(a2.j);

A a3 = new C();
System.out.println(a3.i);
System.out.println(a3.j);
System.out.println(a3.k);
}
}

Successful execution of above program:-

class A
{
int i;
}

class B extends A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
int j;
}

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.

class C extends B
{
int k;
}

class Manager
{
public static void main(String args[])
{
A a1 = new A();
System.out.println(a1.i);
System.out.println(a1.j);
System.out.println(a1.k);

A a2 = new B();
System.out.println(a2.i);
System.out.println(a2.j);
System.out.println(a2.k);

A a3 = new C();
System.out.println(a3.i);
System.out.println(a3.j);
System.out.println(a3.k);
}
}

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

int k;
}

class Manager
{
public static void main(String args[])
{
B b1 = (B) new A();
System.out.println(b1.i);
System.out.println(b1.j);
System.out.println(b1.k);
}
}

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.
While running the above program it will produce
ClassCastException.
Object data is not changed through casting but data type is only
being changed.
In the above case the object new A() is converted into data type of B.
But b1 refers only the same A class objects data.
Super class object can’t be referred by sub class reference variable.
We are converting non compatible i.e. A into B which is not possible
and so it raises an exception which is called as “ClassCastException”.

class A
{
int i;
}

class B extends A
{
int j;
}

class C extends B
{
int k;
}

class Manager
{
public static void main(String args[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
B b1 = new C();
A a1 = b1;
C c1 = (C ) a1;
System.out.println(c1.k);
}
}

class A
{
int i;
}

class B extends A
{
int j;
}

class C extends B
{
int k;
}

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.
class Manager
{
public static void main(String args[])
{
B b1 = new C();
A a1 = b1;
B b2 = (B ) a1;
System.out.println(b2.k);
}
}

class A
{
int i;
}
class B extends A
{
int j;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class C extends B
{
int k;
}

class Manager
{
public static void main(String args[])
{
A a = new B();
A a1 = a;
B b2 = (B ) a1;
System.out.println(b2.j);
}
}

class A
{
int i;
}

class B extends A
{
int j;
}

class C extends B
{
int k;
}

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.
class Manager
{
public static void main(String args[])
{
A a = new B();
A a1 = a;
B b2 = (B ) a1;
System.out.println(b2.j);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class A
{
int i;
}

class B extends A
{
int j;
}

class C extends B
{
int k;
}

class Manager
{
public static void main(String args[])
{
A a1 = new C ();
B b1 = (B) a1;
C c1 = (C) b1;
C c2 = (C) a1;
C c3 = (C) (B) a1;
C c4 = (C) (B) (A) (B) (C) b1;
}
}

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.
int k;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class Manager
{
public static void main(String args[])
{
A a1 = new B ();
C c1 = (C) a1;
System.out.println(a1.i);
System.out.println(c1.i);
System.out.println(c1.j);
System.out.println(c1.k);
B b1 = (B) a1;
A a2 = b1;
System.out.println(b1.i);
System.out.println(b1.j);
System.out.println(b1.k);
System.out.println(a2.i);
System.out.println(a2.j);
}
}

class A
{
int i;
}

class B extends A
{
int j;
}

class C extends B
{
int k;
}

class Manager
{
public static void main(String args[])
{
A a1 = new A ();
A a2 = (A) (A) a1;
B b1 = (B) a2;
C c1 = (C) b1;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

B b2 = (C) a1;
System.out.println(a1.i);
System.out.println(c1.k);

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.
System.out.println(b2.j);
System.out.println(a2.i);
}
}

class A
{
int i;
}

class B extends A
{
int j;
}

class C extends B
{
int k;
}

class Manager
{
public static void main(String args[])
{
C c1 = (C) new A ();
B b2 =(C) new B ();
C c1 = (B) (C) new A();
}
}

class A
{
int i;
}
class B extends A
{
int j;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class C extends B
{
int k;
}

class Manager
{
static void test(a a1)
{
System.out.println(a1.i);
System.out.println(a1.j);
System.out.println(a1.k);
Rule 12 Do not include any prefix or suffix to indicate that a name is
for any specific type.
}
static void test(b b1)
{
System.out.println(b1.i);
System.out.println(b1.j);
System.out.println(b1.k);
}
public static void main(String args[])
{
A a1 = new B();
test(a1);
test((B)a1);
}
}

class Manager
{
static void test1(A a1)
{
System.out.println(a1.i);
}
static void test2(B b1)
{
System.out.println(b1.i);
System.out.println(b1.j);
}
static void test3(C c1)
{
System.out.println(c1.i);
System.out.println(c1.j);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(c1.k);
}
public static void main(String args[])
{
A a1 = new C();
test1(a1);
test2((B)a1);
test3((C)a1);
test1((B)a1);
test2((C)a1);
test3((B)a1);
test1((B) (A) (C) a1);
test2((B) (C) a1);
test3((B) (C) (A) a1);
}
}

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.

class Manager
{
static void test(A a1)
{
B b1 = (B) a1;
C c1 = (C) a1;
System.out.println(b1.j);
System.out.println(c1.k);
}
public static void main(String args[])
{
A a1 = new A();
test(a1);
B b1 = new B();
test(b1);
C c1 = new C();
test(c1);
A a11 = new A();
test(a11);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

//new prog
Class Test
{
static void test1(A a1)
{
System.out.println(“A”);
}
static void test1(B b1)
{
System.out.println(“B”);
}
static void test1(C c1)
{
System.out.println(“A”);
}public static void main(String args[])
{
A a1 = new C();
test1(a1);
test1((B)a1);
test1((C)a1);
test1((B) (A) (C) (A) (B) a1);
}
}

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.

class Test
{
static void test1(B b1)
{
System.out.println(“A”);
}
public static void main(String args[])
{
A a1 = new C();
test1(a1);
test1((B) a1);
test1((C) a1);
test1((C) (B) (A) (B) (C) (Object) a1);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class Test
{
void test(double d)
{
System.out.println(“double”);
}
void test(Object obj)
{
System.out.println(“Object”);
}
}
class Manager
{Test t = new Test();
t.test(10);
t.test(10.0);
t.test(new A());
t.test(new B());
t.test(new C());
t.test(new Object());
t.test((B) new C());
}
}

Derived Casting
ClassCastException
Widening in case of derived arguments

1.

2.

class A
{

}
class B extends A
{

}
class C extends B
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class Manager1
{
public static void main(String agrs[])
{
A a1=new C();
B b1= (B)a1;
C c1= (C)b1;
C c2=(C) a1;
C c3=(C)(B)a1;
C c4=(C)(B)(A)(B)(C)b1;
}
}

3.

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager2
{
public static void main(String agrs[])
{
A a1=new B();
C c1= (C)a1;
System.out.println(a1.i);
System.out.println(c1.i);
System.out.println(c1.j);
System.out.println(c1.k);
B b1=(B) a1;
System.out.println(b1.i);
System.out.println(b1.j);
System.out.println(b1.k);
System.out.println(a3.i);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(a3.j);
}
}

4.

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager3
{
public static void main(String agrs[])
{
A a1=new A();
B b1=(B) a2;
C c1=(C) a1;
b2=(C) a1;
System.out.println(b1.i);
System.out.println(b1.j);
//System.out.println(b1.k);
System.out.println(a3.i);
//System.out.println(a3.j);
}
}

5.
class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
int k;
}
class Manager4
{
public static void main(String agrs[])
{
A a1=new A();
A a2=A A a1;
B b1=(B) a2;
C c1=(C) a1;
B b2=(C) a1;
System.out.println(a1.i);
System.out.println(c1.k);
System.out.println(b2.j);
System.out.println(a2.i);
}
}

6.
class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager5
{
public static void main(String agrs[])
{
A a1=new A();
A a2=(A) (A) a1;

System.out.println(a1.i);

System.out.println(a2.i);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

7.

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager6
{
public static void main(String agrs[])
{
C c1=(C) new A();
B b1=(C) new B();
C c1=(B)(C) new A();
}
}

8.
class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager8
{
{
static void test(A a1)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
static void test( a1)
{

}
public static void main(String agrs[])
{
C c1=(C) new A();
B b1=(C) new B();

}
}

9.

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager8
{

static void test(A a1)


{
System.out.println(a1.i);
System.out.println(a1.j);
System.out.println(a1.k);
}
static void test1(B b1)
{
System.out.printl(b1.i);
System.out.printl(b1.j);
System.out.printl(b1.k);
}
public static void main(String agrs[])
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
A a1=new B();
test(a1);
test1((B)a1);
}
}

10.

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager10
{

static void test1(A a1)


{
System.out.println(a1.i);
}
static void test2(B b1)
{
System.out.println(b1.i);
System.out.println(b1.j);
}
static void test3(B b1)
{
System.out.println(c1.i);
System.out.println(c1.j);
System.out.println(c1.k);
}
public static void main(String agrs[])
{
A a1=new C();
test1(a1);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

test2((B)a1);
test3(()a1);
test3()a1);

test1((B)a1);
}
}

11.

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager10
{
static void test1(A a1)
{
System.out.println(a1.i);
}

static void test2(B b1)


{
System.out.println(b1.i);
System.out.println(b1.j);
}

static void test3(C c1)


{
System.out.println(c1.i);
System.out.println(c1.j);
System.out.println(c1.k);
}
public static void main(String agrs[])
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A a1=new C();
test1(a1);
test2((B)a1);
test3((C)a1);
test1((B)a1);
test2((C)a1);
test3((B)a1);
test1((B)(A)(C)a1);
test2((B)(C)a1);
test3((B)(C)(A)a1);
}
}

12.

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
}
class Manager11
{
static void test1(A a1)
{
System.out.println(a1.i);
}

static void test2(B b1)


{
System.out.println(b1.i);
System.out.println(b1.j);
}

static void test3(C c1)


080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(c1.i);
System.out.println(c1.j);
System.out.println(c1.k);
}
public static void main(String agrs[])
{
A a1=new C();
test1(a1);
test2((B)a1);
test3((C)a1);
test1((B)a1);
test2((C)a1);
test1((B)(A)(C)a1);
test2((B)(C)a1);

}
}

13.

class A
{
int ;
}
class B extends A
{
int ;
}
class C extends B
{
int ;
}
class Manager12
{
static void test(A a1)
{
B B1=(B) a1;
C c1=(C) a1;
System.out.println("b1.j");
System.out.println("c1.k");
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String agrs[])


{
A a1=new C();
test1(a1);
B b1=new B();
test1( b1);
C c1=new C();
test1(c1);
A a11=new A();
test1(a1);
}
}

14.

class A
{
int i=0;
}
class B extends A
{
int j=0;
}
class C extends B
{
int k=0;
}
class Test8
{
void test(double d)
{
System.out.println("double");
}
void test(Object obj)
{
System.out.println("object ");
}
}
class Manager13
{
public static void main(String agrs[])
{
Test8 t=new Test8();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

t.test(10);
t.test(10.0);
t.test(new A());
t.test(new B());
t.test(new C());
t.test(new Object());
t.test((B) new C());
}
}

15.

class A
{
int i=0;
}
class B extends A
{
int j=0;
}
class C extends B
{
int k=0;
}
class Test9
{
void test(double d)
{
System.out.println("double");
}
void test(Object obj)
{
System.out.println("object ");
}
}
class Manager14
{
public static void main(String agrs[])
{
Test8 t=new Test8();
t.test(10);
t.test(10l);
t.test(10d);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

t.test(10.0);
t.test(new A());
t.test(new B());
t.test(new C());
t.test(new Object());
t.test((B) new C());
}
}

16.

class A
{
int i=0;
}
class B extends A
{
int j=0;
}
class C extends B
{
int k=0;
}
class Test
{
static void test1(A a1)
{
System.out.println("A");
}
static void test2(B b1)
{
System.out.println("B");
}
static void test3(C c1)
{
System.out.println("C");
}
public static void main(String agrs[])
{
A a1=new C();
test1(a1);
test1((B) a1);
test1((C)a1);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

test1((B)(A)(C)(A)(B)a1);
}
}

17.

class A
{
int i=0;
}
class B extends A
{
int j=0;
}
class C extends B
{
int k=0;
}
class Test2
{
static void test1(A a1)
{
System.out.println("A");
}
static void test1(B b1)
{
System.out.println("B");
}
static void test1(C c1)
{
System.out.println("C");
}
public static void main(String agrs[])
{
A a1=new C();
test1(a1);
test1((B) a1);
test1((C)a1);
test1((B)(A)(C)(A)(B)a1);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

18.

class A
{
int i=0;
}
class B extends A
{
int j=0;
}
class C extends B
{
int k=0;
}
class Test3
{
static void test1(A a1)
{
System.out.println("A");
}
static void test1(B b1)
{
System.out.println("B");
}

public static void main(String agrs[])


{
A a1=new C();
test1(a1);
test1((B) a1);
test1((C)a1);
test1((B)(A)(C)(A)(B)a1);
}
}

19.

class A
{
int i=0;
}
class B extends A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

int j=0;
}
class C extends B
{
int k=0;
}
class Test4
{
static void test1(A a1)
{
System.out.println("A");
}

public static void main(String agrs[])


{
A a1=new C();
test1(a1);
test1((B) a1);
test1((C)a1);
test1((B)(A)(C)(A)(B)a1);
}
}

20.

class A
{
int i=0;
}
class B extends A
{
int j=0;
}
class C extends B
{
int k=0;
}
class Test5
{
static void test1(B b1)
{
System.out.println("A");
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
public static void main(String agrs[])
{
A a1=new C();
test1(a1);
test1((B) a1);
test1((C)a1);
test1((B)(A)(C)(A)(B)a1);
}
}

21.

class A
{
int i=0;
}
class B extends A
{
int j=0;
}
class C extends B
{
int k=0;
}
class Test6
{
static void test1(B b1)
{
System.out.println("A");
}
public static void main(String agrs[])
{
A a1=new C();
test1(a1);
test1((B) a1);
test1((C)a1);
test1((B)(A)(C)(A)(B)a1);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

22.

class A
{
int i=0;
}
class B extends A
{
int j=0;
}
class C extends B
{
int k=0;
}
class Test7
{
static void test1(B b1)
{
System.out.println("A");
}
public static void main(String agrs[])
{
A a1=new C();
test1((B) a1);
test1((C)a1);
test1((B)(A)(C)(A)(B)a1);
}
}

SCJP Oriented

Question 1
Given:
10. interface Foo {}
11. class Alpha implements Foo { }
12. class Beta extends Alpha {}
13. class Delta extends Beta {
14. public static void main( String[] args) {
15. Beta x = new Beta();
16. // insert code here
17. }
18. }
Which code, inserted at line 16, will cause a
080-41310124 http://www.javaeasytoall.com
Lara Technologies

java.lang.ClassCastException?
A. Alpha a = x;
B. Foo f= (Delta)x;
C. Foo f= (Alpha)x;
D. Beta b = (Beta)(Alpha)x;
Answer: B

Question 2
Given:
11. class ClassA {}
12. class ClassB extends ClassA {}
13. class ClassC extends ClassA {}
and:
21. ClassA p0 = new ClassA();
22. ClassB p1 = new ClassB();
23. ClassC p2 = new ClassC();
24. ClassA p3 = new ClassB();
25. ClassA p4 = new ClassC();
Which three are valid? (Choose three.)
A. p0 = p1;
B. p1 =p2;
C. p2 = p4;
D. p2 = (ClassC)p1;
E. p1 = (ClassB)p3;
F. p2 = (ClassC)p4;
Answer: AEF

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 12 Do not include any prefix or suffix to indicate that a name is


for any specific type.
Day 13

Field hiding
class A
{
int i = 9;
}
class B extends A
{
int i = 19;
}
class C extends B
{
int i = 29;
}
class D extends C
{
}
class E extends B
{
}
class F extends A
{
int i = 39;
}

class Manager
{
public static void main(String args[])
{
A a1 = new A();
System.out.println(a1.i);
A a2 = new B();
System.out.println(a2.i);
A a3 = new C();
System.out.println(a3.i);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A a4 = new D();
System.out.println(a4.i);
A a5 = new E();
System.out.println(a5.i);
A a6 = new F();
System.out.println(a6.i);

B b1 = new B();
System.out.println(b1.i);
B b2 = new C();
System.out.println(b2.i);
B b3 = new D();
System.out.println(b3.i);
B b4 = new E();
Rule 13 Do not include anything in a name to indicate that the name
is for a class.
System.out.println(b4.i);
B b5 = new F();
System.out.println(b5.i);

C c1 = new C();
System.out.println(c1.i);
C c2 = new D();
System.out.println(c2.i);
C c3 = new E();
System.out.println(c3.i);
C c4 = new F();
System.out.println(c4.i);

D d1 = new D();
System.out.println(d1.i);
D d2 = new E();
System.out.println(d2.i);
D d3 = new F();
System.out.println(d3.i);

E e1 = new E();
System.out.println(e1.i);
E e2 = new F();
System.out.println(e2.i);

F f1 = new F();
System.out.println(f1.i);

080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}

Only methods will be overridden, variables or attributes can’t be


overridden.
When an object has more than one attribute with the same name
which is possible only when inherited, jvm to avoid ambiguity, it
considers the variable respective to the object’s reference variable on
which it is called.

Co-variants

class A{ }
class B extends A{ }
class C extends B{ }

class X
{
A test()
{
Statements1;
_
_
_

Rule 13 Do not include anything in a name to indicate that the name


is for a class.
Statements n;
return null;
}
}
class Y extends X
{
B test()
{
Statements1;
_
_
_
Statements n;
return null;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class Z extends Y
{
C test()
{
_Statements1;
_
_
_Statements n;
return null;
}
}

Default literal to derived datatype is ‘null’. ‘null’ is not a keyword.

class X extends A
{
A test()
{
_
_
_
return new Y();
}
}
class Y extends X
{
Y test()
{
_
_
_
return new Y();
}
}
Rule 13 Do not include anything in a name to indicate that the name
is for a class.
Whenever we are a method in sub classes, we have to concentrate on
the return type of the derived variables, which should put same
derived data type or sub class to the derived data type class which is
known as “Co-variants”.

Valid cases:-
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class X extends A
{
X test()
{
return new Y();
}
}
class Y extends X
{
Y test()
{
return new Y();
}
}

class X extends A
{
Object test()
{
return new Y();
}
}
class Y extends X
{
C test()
{
return new C();
}
}

class C{}

While overriding a method, it should have same signature as the


super class method and return type should be the same.

class X
{
Object test()
{
return new A();
}
}
class Y extends X
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

B test()
{
Rule 13 Do not include anything in a name to indicate that the name
is for a class.

return new C();


}
class A {}
class B extends A{}
class C extends B{}

class A
{
int test()
{
return 0;
}
}
class B extends A
{
double test(int i)
{
return 0.0;
}
}
Above program will be compiled success because both methods are
different in signature and so they are overloaded but not overridden.

class X
{
int test()
{
return 0;
}
double test(int i)
{
return 0.0;
}
Object test(double d)
{
return null;
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Initialization Block Constructors


1) Initialization block can be non- 1) Constructors cannot be a static
static or static
2) Common to all the objects 2) Specific to one object
created of the same class
3) We can’t give a call to other 3) Constructor can be called from
initialization block. We can’t call other constructor explicitly in
initialization block from same class or implicitly with the
anywhere explicitly super class once in the execution
4) Whenever object is created, 4) Any one of the constructor will
then all the IB’s are executed in be called while creating an object
order from top to bottom of that class
5) We cannot give any access level 5) we can go for any access level
to initialization block for constructors like private,
protected, default and public
6) No concept of default 6) If developer doesn’t keep any
Initialization block constructor, compiler will
generate a default constructor.

Rule 13 Do not include anything in a name to indicate that the name


is for a class.

Method Constructor
Methods can be static as well as Constructors can only be non-
non-static members static members
These can be inherited to sub These are no inherited to sub-
classes classes
Methods can be overridden as Constructors cannot be
well as overloaded in sub classes overridden as well as overloaded
because, they are not inherited to
sub classes
It will be involved in It will not be involved in
polymorphism as it can be polymorphism as it cannot be
overridden overridden
Methods can be declared final Constructors cannot be declared
as final
Methods can be declared abstract Constructors cannot be declared
as an abstract
A class can be there without a A class cannot be there without a

080-41310124 http://www.javaeasytoall.com
Lara Technologies

method, hence there is no concept constructor, atleast one


of default method. i.e. compiler constructor should be there, if
will never bother if there no developer doesn’t keep it
method compiler will look for a
constructor, if it doesn’t finds, it
will generate a constructor which
is called a default constructor or
no-arg constructor
There is no concept of auto Constructor chaining is
chaining in case of methods automatically formed without
developers intervention
We can put calling statements to We can put a calling statement to
super class methods anywhere in same class constructor or super
other sub class methods class constructor only as the first
statement, if compiler doesn’t
find it, then compiler will put a
calling statement to super class
no-arg method i.e. super();
Methods can be called from any Constructors can be called only
where from other constructors, if we are
not creating any object
Recursion of methods is possible Recursion of constructors is not
and also allowed by compiler allowed by compiler
Main goal of the method is to find Main goal of the constructor is to
out or change the state of an initialize state of an object at the
object or exhibit behavior creation with the desired state
Should have a return type Should not have a return type( a
constructor with a return type
will become a method rather than
a constructor)
No restriction in the case of name Constructors name must and
of the method should be same as the class name

We cannot keep more than one member with same name whether it
might be 2 attributes or 2 methods or 2 files or 2 classes.

Rule 13 Do not include anything in a name to indicate that the name


is for a class.

class A
{
A A; // Attribute
A(A A)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

// Constructor
}
A A(A A)
{
// Method
return A;
}
}

Naming Collision:-

class A
{
}
class A
{
}

Open a java file and write the above 2 classes in the same file with the
name “A.java”. Compilation got failed because it will not compile if it
finds more one class with the same name. If we try to separate those 2
classes in to separate files, those will be compiled but there will be
only one “A.class” file generated. We are unable to choose same name
for more than one class in the same source folder. The above
condition is known as Naming collision.

Field hiding
Co-variants
Overriding
Differences between IB & Constructors
Differences between method and constructors
Naming collision

1. What are co-variants?


2. What is literal?
3. Literal is a keyword(t/f)
4. Difference between initilization block and constructor.
5. Difference between method and constructor.
6. methods can be overrided(t/f)
7. attributes can be overrided
8. Method can be final(t/f).
9. Constructor is involved in polymorphism(t/f).
10.Super calling statments can be anywhere inside a method(t/f).
080-41310124 http://www.javaeasytoall.com
Lara Technologies

11.Methods can be called explicitly from anywhere(t/f).


12.In methods their is concept of auto method chaining(t/f)
13.Methods should not have return type(t/f).
14.Methods should take only class name(t/f).
15.What is package?
16.First valid statment should be import,if package is their(t/f).
17.Second valid statment should be package,if import is thier(t/f).
18.If package is not their, then import should be the first
statment(t/f)
19.Public can be accessed only inside the class(t/f)
20.Private can be accessed only inside the package(t/f)
21.Default is the widermost access(t/f)
22.Protected can be accessed in the sub class which are outside
packages also(t/f)
23.Private cannot be used in subclass(t/f)
24.Default is accessed within package and not outside the
package(t/f)
25.Protected inherited member should be used only inside the
inherited class(t/f)
26.1. class A
{
int i=9;
}
class B extends A
{
int i=19;
}
class C extends B
{
int i=29;
}
class D extends C
{

}
class E extends B
{

}
class F extends A
{
int i=39;
}
class Manager1
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
public static void main(String[] args)
{
A a1=new A();
System.out.println(a1.i);
A a2=new B();
System.out.println(a2.i);
A a3=new C();
System.out.println(a3.i);
A a4=new D();
System.out.println(a4.i);
A a5=new E();
System.out.println(a5.i);
A a6=new F();
System.out.println(a6.i);
B b1=new B();
System.out.println(b1.i);
A b2=new C();
System.out.println(b2.i);
B b3=new D();
System.out.println(b3.i);
B b4=new E();
System.out.println(b4.i);
C c1=new C();
System.out.println(c1.i);
C c2=new C();
System.out.println(c2.i);
D d1=new D();
System.out.println(d1.i);
E e1=new E();
System.out.println(e1.i);
F f1=new F();
System.out.println(f1.i);
}
}

2.

class A
{
int i=9;
}
class B extends A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
int i=19;
}
class C extends B
{
int i=29;
}
class D extends C
{

}
class E extends B
{

}
class F extends A
{
int i=39;
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println(a1.i);
A a2=new B();
System.out.println(a2.i);
A a3=new C();
System.out.println(a3.i);
A a4=new D();
System.out.println(a4.i);
A a5=new E();
System.out.println(a5.i);
A a6=new F();
System.out.println(a6.i);
B b1=new B();
System.out.println(b1.i);
A b2=new C();
System.out.println(b2.i);
B b3=new D();
System.out.println(b3.i);
B b4=new E();
System.out.println(b4.i);
B b5=new F();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(b5.i);
C c1=new C();
System.out.println(c1.i);
C c2=new C();
System.out.println(c2.i);
C c3=new E();
System.out.println(c3.i);
C c4=new F();
System.out.println(c4.i);
D d1=new D();
System.out.println(d1.i);
D d2=new E();
System.out.println(d2.i);
D d3=new F();
System.out.println(d3.i);
E e1=new E();
System.out.println(e1.i);
E e2=new F();
System.out.println(e2.i);
F f1=new F();
System.out.println(f1.i);
}
}

3.

class A
{
int i=9;
}
class B extends A
{
int i=19;
}
class C extends B
{
int i=29;
}
class D extends C
{

}
class E extends B
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class F extends A
{
int i=39;
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println(a1.i);
A a2=new B();
System.out.println(a2.i);
A a3=new C();
System.out.println(a3.i);
A a4=new D();
System.out.println(a4.i);
A a5=new E();
System.out.println(a5.i);
A a6=new F();
System.out.println(a6.i);
B b1=new B();
System.out.println(b1.i);
A b2=new C();
System.out.println(b2.i);
B b3=new D();
System.out.println(b3.i);
B b4=new E();
System.out.println(b4.i);
C c1=new C();
System.out.println(c1.i);
C c2=new C();
System.out.println(c2.i);
D d1=new D();
System.out.println(d1.i);
E e1=new E();
System.out.println(e1.i);
F f1=new F();
System.out.println(f1.i);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

4.
class A
{
int i=9;
}
class B extends A
{
int i=19;
}
class C extends B
{
int i=29;
}
class D extends C
{

}
class E extends B
{

}
class F extends A
{
int i=39;
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
System.out.println(a1.i);
A a2=new B();
System.out.println(a2.i);
A a3=new C();
System.out.println(a3.i);
A a4=new D();
System.out.println(a4.i);
A a5=new E();
System.out.println(a5.i);
A a6=new F();
System.out.println(a6.i);
B b1=new B();
System.out.println(b1.i);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A b2=new C();
System.out.println(b2.i);
B b3=new D();
System.out.println(b3.i);
B b4=new E();
System.out.println(b4.i);
B b5=new F();
System.out.println(b5.i);
C c1=new C();
System.out.println(c1.i);
C c2=new C();
System.out.println(c2.i);
D d1=new D();
System.out.println(d1.i);
E e1=new E();
System.out.println(e1.i);
F f1=new F();
System.out.println(f1.i);
}
}

5.

class A
{
}
class B extends A
{
}
class C extends B
{
}
class X extends A
{
A test()
{
return new A();
}
}
class Y extends X
{
Y test()
{
return new Y();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}

6.

class A
{
}
class B extends A
{
}
class C extends B
{
}
class X extends A
{
X test()
{
return new A();
}
}
class Y extends X
{
Y test()
{
return new X();
}
}

7.

class A
{
}
class B extends A
{
}
class C extends B
{
}
class X extends A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
Object test()
{
return new C();
}
}
class Y extends X
{
C test()
{
return new C();
}
}

8.

class A
{
}
class B extends A
{
}
class C extends B
{
}
class X
{
Object test()
{
return new B();
}
}
class Y extends X
{
B test()
{
return new C();
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

9.

class A
{
}
class B extends A
{
}
class C extends B
{
}
class X
{
int test()
{
return 0;
}
}
class B extends A
{
double test()
{
return 0.0;
}
}

10.

class A
{
}
class B extends A
{
}
class C extends B
{
}
class A
{
int test()
{
return 0;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
class B extends A
{
double test(int i)
{
return 0;
}
}

11.

class A
{
}
class B extends A
{
}
class C extends B
{
}
class X
{
int test()
{
return 0;
}

double test(int i)
{
return 0.0;
}
Object test(double d)
{
return null;
}
}

12.
class A
{
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class C extends B
{
}
class X
{
int test()
{
return 0;
}
}
class B extends A
{
double test()
{
return 0.0;
}
}

Rule 13 Do not include anything in a name to indicate that the name


is for a class.
Day 14

Packages

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Without packages we cannot develop 2 classes with same name either


in same or in different file. Package is keyword should be first
statement of ‘.java’ file, if we want to develop classes in packages.
Package statement is applied with file wise rather than class file.

Program1:-

src A.java
pack1 package pack1;

A.java class A{ }

pack2 class B{ }
D.java

classese
D.java
pack1
A.class
B.class package pack2;

class B{ }
B.class
pack2 D.class class D{ }

For avoiding naming collision, we have to go for a concept called


“Package”. The need of packages is to avoid naming collision and to
maintain our application development in an organized fashion.

In the above program, we are trying to create two packages called


pack1 and pack2.
We are developing ‘A.java’ file in pack1 and ‘D.java’ file in pack2
package. Total of 4 classes are being created in those 2 ‘.java’ files,
class A and class B in ‘A.java’ and another classes i.e. class B and
class D is being written ‘D.java’ file.

src> javac –d ../classes pack1/A.java


src> javac –d ../classes pack2/A.java

Even though we don’t develop packages in ‘classes’ folder, compiler


will automatically generate the pack1 and pack2 packages.
In this we are able to resolve naming collision and able to compile
and run very successfully.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 14 Do not include underscores in names, except after a


member’s prefix.

A.java
src
package com.pack1;
com
class A{ }
pack1
class B{ }
A.java
B.java
classese
B.java
pack2
package com;
C.java
com
class A{ }
pack1 A.class class B{ }
B.class
A.class class C{ }
B.class
C.class
C.java
A.class package com.pack2;
pack2
B.class
C.class class A{ }

class B{ }

class C{ }

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 14 Do not include underscores in names, except after a


member’s prefix.

A.java
src
package com.lara.io;
com class A { }

lara class B { }

io class C { }
A.java

classes B.java
db B.java
C.java
com package com.lara.db;

lara class B
{
io A.class
int i;
B.class
}
C.class

C.java
package com.lara.db;
B.class class C
C.class {
db Psvm(String args[])
{
B b1 = new B();
080-41310124 S.o.p( b1.i );
http://www.javaeasytoall.com
}
}
Lara Technologies

Rule 14 Do not include underscores in names, except after a


member’s prefix.

src A.java
package com.lara.pack1;
com
A.java public class A
lara {
B.java int i;
C.java }
pack1

B.java
package com.lara;
import com.C;

class B
classes {
A.class
C c1 = new C();
080-41310124 com } http://www.javaeasytoall.com
B.class
C.class
lara pack1
Lara Technologies

C.java
package com;
import com.lara.A;
import com.lara.B;

class C
{
Psvm(String args[])
{
A a1 = new A();
B b1 = new B();
}
}

Rule 14 Do not include underscores in names, except after a


member’s prefix.

A.java
B.java
D.java
E.java

src
A.class
com B.class
080-41310124 C.class http://www.javaeasytoall.com
lara
D.class
E.class
Lara Technologies

classes

com

lara

Rule 14 Do not include underscores in names, except after a


member’s prefix.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

src

com

lara
A.java
B.java
C.java
classes

com
A.class
lara B.class
C.class

src
com
A.java

lara B.java

classes
A.class
com

B.class
lara

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 14 Do not include underscores in names, except after a


member’s prefix.

src

com

lara

server
Person.java

client Manager.java
classes

com

lara
Person.class
server

client Manager.class

package com.lara.client;
import com.lara.server.Person;
Person.java
package com.lara.server; Manager.java
class Employee extends Person
public class Person {
{ public static void main(String args[])
protected void test() {
{ Employee e = new Employee()
080-41310124
S.o.p(“Person”); e.test(); http://www.javaeasytoall.com
} }
} }
Lara Technologies

Rule 14 Do not include underscores in names, except after a


member’s prefix.
Packages and Access Levels :
We use packages in order to avoid naming collision and also to
organize code according to task (purpose).

src A.java
A.java class A
{
}
class B
{
}
X.java
X.java
class X
{
}
class B
{
}

In the above example we have created two java source files in


src directory. Both files contain class B in them. We cannot compile
these files using *.java since compiler will go in ambiguity because of

080-41310124 http://www.javaeasytoall.com
Lara Technologies

multiple definition of class B. Thus to avoid these types of naming


collision we use packages.
E:\
LAB
13th-batch
package-dev

src
pack1

A.java

classes

pack1

A.class

35) package pack1;


class A
{
public static void main(String[] args)
{
System.out.println(“Hello World !”);
}
}

We declare package structure of a java file using package


keyword. package keyword must be the first line of our source file
always. When we compile above source file using –d option, class files
will be created in classes folder with the package structure as defined
using package keyword. Since we have declared package as pack1,
.java file must be saved inside pack1 folder. pack1 will now qualify as
a package. Now class file of class A is a part of package pack1.

During compiling we must use


E:\LAB\13th-batch\package-dev\src>javac –d ../classes pack1/A.java

While running
E:\LAB\13th-batch\package-dev\classes> java pack1/A OR
E:\LAB\13th-batch\package-dev\classes> java pack1.A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

If there are two files belonging to same package pack1 then


they will share a common package as shown below.

E:\
LAB
13th-batch
package-dev

src
pack1

A.java

B.java

classes

pack1

A.class

B.class

36) A.java

package com.pack1;
class A
{
void show()
{
System.out.println(“Inside A”);
}
}

B.java

package com.pack1;
class B
{
public static void main(String[] args)
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A a1 = new A();
a1.show();
}
}
O/P : Inside A

37) A.java

package com.pack1;
class A
{
private void show()
{
System.out.println(“Inside A”);
}
}

B.java

package com.pack1;
class B
{
public static void main(String[] args)
{
A a1 = new A();
a1.show();
}
}
O/P: Compiler Error

Since private members can be accessed only within the class,


we get compiler error.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Now let us create package stream as shown.

E:\
LAB
13th-batch
package-dev

src
com
pack1
A.java

B.java

classes

com
pack1
A.class

B.class
In the above program both class A and class B belong to same
package pack1. Class B shares a HAS-A relation with class A. Thus
class B is using class A for object creation. By default compiler will
search for class A in the present package. Here we have class A in
same package. Thus the program will compile and run successfully.
A.java and B.java shares a common package stream i.e
com.pack1.

Access Specifiers :
Access specifiers are the keywords which specifies the
accessibility of class and class members.In java we have four access
levels and three access specifiers.

The four access levels are


1) Accessibility within same class.
2) Accessibility within same package
3) Accessibility within same package and sub classes of other
package.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

4) Accessibility from anywhere.

The three access specifiers are


1) private.
2) protected.
3) public.

private : If we use private to class members as access


specifier, we can access those members only within the same class.

default access : In default access we don’t specify any access


specifier. In case of default access we can access those members
within the package only.

protected : If we use protected to class members as access


specifier, we can access those members within same package as
well as within inheritted classes present in other packages.

public : If we use public to class members as access specifier, we


can access those members from anywhere.

We know that when we use any class inside other by default


compiler will search that file within the same package.

Now let us create source files in different packages.

E:\
LAB
13th-batch
package-dev
package
stream
src
com
pack1
A.java

pack2
B.java
classes

com
pack1
A.class
080-41310124 http://www.javaeasytoall.com
Lara Technologies

pack2
B.class

38) A.java

package com.pack1;
public class A
{
public void show()
{
System.out.println(“Inside A”);
}
}

B.java

package com.pack2;
import com.pack1.A;
class B
{
public static void main(String[] args)
{
A a1 = new A();
a1.show();
}
}
In the above program we are creating source files in different
packages. Class B is having HAS-A relation with class A. We can not
compile different package classes at once. We need to be inside src
directory to compile these source files since our package stream starts
from there. By default while compiling class B, compiler will check
for class A in the default package for resolving dependency. But class
A is in other package but in same stream. To specify where class A is
in the current stream, we use import keyword. We must use import
keyword after defining package. Now compiler will not check present
package but it will go to the specified package in order to use class A.
When we compile B.java , compiler will produce A.class first and
then B.class since dependencies will be resolved first.
One important point to remember here is that whenever we are
accessing a class and class member outside the package, both class
and class member should be declared as public as shown in above
program. When we define package stream in a source file it applies to
all classes defined within that source file.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Packages
Need of import statement
Access levels

SCJP Oriented

Question 1
Given:
10. class One {
11. void foo() {}
12. }
13. class Two extends One {
14. //insert method here
15. }
Which three methods, inserted individually at line 14, will correctly
complete class Two? (Choose three.)
A. int foo() { /* more code here */ }
B. void foo() { /* more code here */ }
C. public void foo() { /* more code here */ }
D. private void foo() { /* more code here */ }
E. protected void foo() { /* more code here */ }
Answer: BCE

Question 2
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. public static void process(byte[]) { /* more code here */ }
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process
method
of BitUtils?
A. process(bytes);
B. BitUtils.process(bytes);
C. util.BitUtils.process(bytes);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

D. SomeApp cannot use methods in BitUtils.


E. import util.BitUtils.*; process(bytes);
Answer: C

Question 3
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. private static void process(byte[] b) { }
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process
method
of BitUtils?
A. process(bytes);
B. BitUtils.process(bytes);
C. app.BitUtils.process(bytes);
D. util.BitUtils.process(bytes);
E. import util.BitUtils. *; process(bytes);
F. SomeApp cannot use the process method in BitUtils.
Answer: F

Question 4
Given a file GrizzlyBear.java:
1. package animals.mammals;
2.
3. public class GrizzlyBear extends Bear {
4. void hunt() {
5. Salmon s = findSalmon();
6. s.consume();
7. }
8. }
and another file, Salmon.java:
1. package animals.fish;
2.
3. public class Salmon extends Fish {
4. void consume() { /* do stuff */ }
5. }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Assume both classes are defined in the correct directories for theft
packages, and that the Mammal class correctly defines the
findSalmon() method. Which two changes allow this code to compile
correctly? (Choose two.)
A. add public to the start of line 4 in Salmon.java
B. add public to the start of line 4 in GrizzlyBear.java
C. add import animals.mammals.*; at line 2 in Salmon.java
D. add import animals.fish.*; at line 2 in GrizzlyBear.java
E. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java
F. add import animals.mammals.GrizzlyBear.*;at line 2 in
Salmon.java
Answer: AD

Question 5
Given a correctly compiled class whose source code is:
1. package com.sun.sjcp;
2. public class Commander {
3. public static void main(String[] args) {
4. // more code here
5. }
6. }
Assume that the class file is located in /foo/com/sun/sjcp/, the current
directory is /foo/, and that the classpath contains “.“ (current
directory).
Which command line correctly runs Commander?
A. java Commander
B. java com. sim. sjcp.Commander
C. java com/sun/sjcp/Commander
D. java -cp com.sun.sjcp Commander
E. java -cp com/sun/sjcp Commander
Answer: B

Question 6
Click the Exhibit button.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Given the fully-qualified class names:


com.foo.bar.Dog
com.foo.bar.blatz.Book
com.bar.Car
com.bar.blatz.Sun
Which graph represents the correct directory structure for a JAR file
from which those classes can be used by the compiler and JYM?
A. Jar A
B. Jar B
C. Jar C
D. Jar D
E. Jar E
Answer: A

Question 7
A developer is creating a class Book that needs to access class Paper.
The Paper class is deployed in a JAR named myLib.jar. Which three,
taken independently, will allow the developer to use the Paper class
while compiling the Book class? (Choose three.)
A. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar.
C. The JAR file is located at /foo/myLib.jar and a classpath
environment variable is set that includes /foo/myLib.jar/Paper.class.
D. The JAR file is located at /foo/myLib.jar and a classpath
080-41310124 http://www.javaeasytoall.com
Lara Technologies

environment variable is set that includes /foo/myLib.jar.


E. The JAR file is located at /foo/myLib.jar and the Book class is
compiled using javac -cp /foo/myLib.jar/Paper Book.java.
F. The JAR file is located at /foo/myLib.jar and the Book class is
compiled using javac -d /foo/myLib.jar Book.java.
G. The JAR file is located at /foo/myLib.jar and the Book class is
compiled using javac -classpath /foo/myLib.jar Book.java.
Answer: BDG

Question 8
Given:
1. package com.company.application;
2.
3. public class MainClass {
4. public static void main(String[] args) { }
5. }
And MainClass exists in the /apps/com/company/application
directory.
Assume the CLASSPATH environment variable is set to “.“ (current
directory). Which two java commands entered at the command line
will run MainClass? (Choose two.)
A. java MainClass if run from the /apps directory
B. java com.company.application.MainClass if run from the /apps
directory
C. java -classpath /apps com.company.application.MainClass if run
from any directory
D. java -classpath . MainClass if run from the
/apps/com/company/application directory
E. java -classpath /apps/com/company/application:. MainClass if run
from the /apps directory
F. java com.company.application.MainClass if run from the
/apps/com/company/application directory
Answer: BC

Question 9
A UNIX user named Bob wants to replace his chess program with a
new one, but he is hot sure where the old one is installed. Bob is
currently able to run a Java chess program starting from his home
directory /home/bob using the command:
java -classpath /test:/home/bob/downloads/* .jar games.Chess
Bob’s CLASSPATH is set (at login time) to:
/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/* .jar
What is a possible location for the Chess.class file?
A. /test/Chess.class
080-41310124 http://www.javaeasytoall.com
Lara Technologies

B. /home/bob/Chess.class
C. /test/games/Chess.class
D. /usr/lib/games/Chess.class
E. /home/bob/games/Chess.class
F. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)
G. inside jarfile /home/bob/downloads/Games.jar (with a correct
manifest)
Answer: C

Question 10
Given:
21. class Money{
22. private String country = “Canada”;
23. public String getC(){return country;}
24. }
25. class Yen extends Money{
26. public String getC(){return super.country;}
27.}
28. public class Euro extends Money{
29. public String getC(int x){return super.getC();}
30. public static void main(String args[]){
31. System.out.println(new Yen().getC()+””+new Euro().getC());
32. }
33. }
What is the result?
A.Canada.
B.Null Canada.
C.Canada null.
D.Canada Canada.
E.Compilation fails due to an error on line 26.
F.Compilation fails due to an error on line 29.
Answer: E

Question 11
3. Given:
2. public class Hi{
3. void m1(){}
4. protected void m2{}
5. }
6. class Lois extends Hi{
7. //insert code here.
8. }
Which four code fragments ,inserted independently at line
7,will compile?(choose four)
080-41310124 http://www.javaeasytoall.com
Lara Technologies

A. public void m1(){}


B. protected void m1(){}
C. private void m1(){}
D. void m2(){}
E. public void m2(){}
F. protected void m2(){}
G. private void m2(){}
Answer: A,B,E,F

Rule 14 Do not include underscores in names, except after a


member’s prefix.
Day 15
Abstract classes
abstract class A
{
int i, j;
abstract void test();
}

We have to use ‘abstract’ keyword, when we have any


unimplemented method in class, and then the method must be
declared as “abstract method” and the class must be declared as
“abstract class”. Whenever a class is having any declared method
means which doesn’t have the definition, then that is to be declared
as abstract method and the class which is containing an abstract
method must and should be declared as “Abstract Class”. Abstract
method is an unimplemented method means we have no idea of how
to implement that method. A method should have either abstract or
definition attached to the method. Abstract class cannot be used for
object creation meaning we can never create an object which is
incomplete.

abstract class Animal


{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

abstract void eat();


}
Inside an abstract class we can define as many methods as we wish.
Concrete class is a class which can have all implementation. We can
use that class directly for object creation. Only abstract class cannot
be used and we have to implement all the abstract methods in the sub
classes and then we can make use of it otherwise, it is useless and that
is the reason we can declare it as final. We will come across the final
in the next chapters but just remember that we can’t use them at
once.

abstract class A
{
void test1()
{
_
_
_
}
abstract void test2();
}
class B extends A
{
void test2()
{
_
_
_
}
}

Rule 15 Do not pluralize type names except collections.

Inside class B, test2() method had implemented which is having all


implemented methods in it and hence it called Concrete class and we
can make use of it for an object creation of class B.

abstract class A
{
void test1()
{
_
_
_
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
abstract void test2();
}
abstract class B extends A
{
void test3()
{
_
_
_
}
}

If any class is inheriting abstract method, then its responsibility is to


implement that abstract methods. If it fails to implement then the
class must be declared as an abstract class.

Which of the following are compiled successfully and which are not?
a)
class A
{
abstract void test()
{
}
}

b)
class A
{
void test();
}

c)
abstract class A
{
abstract void test();
}

Rule 15 Do not pluralize type names except collections.


d)
abstract class A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

void test()
{
}
}

f)
abstract class A
{
void abstract test()
}

g)
abstract class A
{
}
class B extends A
{
}

h)
class A
{
}
abstract class B extends A
{
}

i)
abstract class A
{
void test();
{
}
class B extends A
{
}

k)
abstract vlass A
{
abstract void test1();
}
Class B extends A
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

void test1()
{
_

Rule 15 Do not pluralize type names except collections.


_
_
}

l)
abstract class A
{
abstract void test1();
}
abstract class B extends A
{}
class C extends B
void test1();
{
_
_
_
}
}

Even though a class not having single abstract method it can also be
declared as abstract. Nobody can create an object to these types of
classes. We are restricting to creation of an object. We can keep
attributes, methods, instance initialization block and constructor in
the abstract class. An abstract class can also contain concrete
methods. Even though we are unable to create object to the abstract
class, the compiler creates the constructor chain [i.e creates default
constructor]. Constructor cannot be abstract, while methods may be
abstract. Constructors are not inherited to subclasses but only
methods are created.

Abstract Classes:

A class is said to be complete when all of its methods have


defined in it. A complete class is called as concrete class which is a
ready to use class. Thus a concrete class has concrete methods in it.
A class which is not concrete will have some methods which
have only declaration and no definition inside it. This class is called
abstract class.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Suppose in a project for designing a class for one entity if we


don’t know some behavior which we will know later then we have to
just declare that method and definition to it will be given later.
Methods which have only declaration and no definition are
called as abstract methods. A class which is having at least one
abstract method is called abstract class and it must be declared as
abstract. Abstract classes are incomplete classes which must be
extended in order to give definition to the declared methods. We can
not create an instance of abstract class since it is incomplete. Thus it
is not possible to create object of an abstract class.

41) abstract class A


{
int i;
abstract void test();
}
class B extends A
{
void test()
{
System.out.println(“test() defined”);
}
public static void main(String[] args)
{
B b1 = new B();
b1.test();
}
}
O/P : test() defined

In the above program we have an abstract class A, which is


having an abstract method test() in it which is just declared and not
defined inside class A. A method which is not having body must be
declared as abstract. In order to define test() method we should
extend class A and thus create a sub class to it and define inside it.
Here test() method of abstract class A is overridden to give body to it.
An abstract class have incomplete methods in it and except that
it is a normal class and will have all the properties and members as a
normal class will have.

42) abstract class A


{
int i;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

abstract void test();


static void disp()
{
System.out.println(“Inside static method of
abstract class”);
}
public static void main(String[] args)
{
A.disp();
}
}

O/P : Inside static method of abstract class

We can directly access static methods since there is no need to


create object for accessing them.

43) abstract class A


{
int i;
abstract void test();
abstract void disp();
}
class B extends A
{
void test()
{
System.out.println(“test() defined”);
}
public static void main(String[] args)
{
B b1 = new B();
b1.test();
}
}
O/P : Compiler error.

A class which is giving implementations to abstract class must


have to define all abstract methods present in the abstract class. Else
the subclass must also be declared as abstract if it is not giving
implementations to all abstract methods.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

44) abstract class A


{
int i;
void test()
{
System.out.println(“Inside test()”);
}
}

We can declare a concrete class as abstract. By doing this we


are restricting creation of objects to that class.
Class can’t be 100% abstract, because every class is having
atleast ione constructor in it and constructor is a concrete method.

Interfaces :
Interfaces are 100% abstract classes i.e an interface will not
have even a single implementation. We can not create objects of an
interface.

45) interface A
{
int i;
void show();
void test();
}

By default every method inside an interface is public and


abstract. And every attribute is public static and final. In order to
give implementations to the methods declared in an interface we need
to implement interface in other class. Inside constructors we can only
declare methods and attributes. We can not define constructors and
initialization blocks inside interfaces.
We can not have a non-static attribute in an interface. Also we
can not have a static method inside an interface.
We can not achieve 100% abstractness using abstract classes.
Thus we go for interfaces. Interfaces are for fixing rules i.e how a
particular behavior should be and not how it should be implemented.
One interface can extend more than one interface.

45) interface A
{
int i;
void show();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

void test();
}

interface B
{
void show1();
void test1();
}

interface C extends A,B


{
void show();
void test();
void show1();
void test1();

Multiple inheritance is possible in interface. As we can see in


above program.

46) interface A
{
void show();
}

class C implements A
{
void show()
{
System.out.println(“show() implemented”);
}
}
O/P: Compile time error.

Interface methods have access modifier by default as public.


But in our program we have overridden show() method in class C,
but access modifier is differing for show() method, hence the compile
error. Implements is the keyword used in between class and interface.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

1) class to class extends


2) class to interface implements
3) interface to class Not Possible
4) interface to interface extends.

47) interface A
{
void show();
}
class B{
void show()
{

System.out.println(“show()
implemented”);
}
}

class C implements A
extends B //Syntax error
{
public static void
main(String[] args)
{
C ob = new C();
ob.show();
}

O/P: Compile time error

Here implements keyword should come after extends, like

48) interface A
{
void show();
}
class B {
void show()
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
System.out.println(“show() implemented”);
}
}

class C extends B implements A


{
public static void main(String[] args)
{
C ob = new C();
ob.show();
}

}
O/P: show() implemented

For developing a perfect abstract, we have to go for interface. Inside


an interface we can keep attributes and method declarations. By
default interfaces are 100% abstract. In interfaces it is not possible to
keep constructors and methods with definition.

interface Test
{
int i = 10;
void test();
}
Or
interface Test
{
public int i =10;
abstract void test();
}

By default, every attribute inside an interface is public static and


final.
By default, every method in an interface is an abstract and public.

Abstract class or an interface means it should have a subclass and it


should be implemented in the subclass.

Interface Test
{
public int i = 10;
abstract void test();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class A implements Test
{
void test()
{
_
_
_
_
}
}

We have to keep only abstract members in the interface and which


will not allows us to develop concrete members inside.

class A
{
void test()
{
_
_
_
}
}
class B extends A
{
private void test()
{
}
}
Here private is more restricted than default, and when we try to
reduce the level of accessibility which is same as narrowing the access
level will not be allowed by compiler for successful compilation. In
the case of overriding the methods the access level must and should
be the same or else broader to that access will be allowed.

For an interface also, a “.class” file will be generated with the name
of [interfacename.class]. But there will be no constructor in the .class
file. As similar to abstract class for interface also, we cannot create
the object.

class A { }
interaface B { }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

abstract class C { }

While compiling the above file there will be 3 ‘.class’ files generated.
There is a default constructor in both ‘A.class’ and as well as
‘C.class’.

Static methods cannot be overridden or implemented in subclasses.


Inside an interface we should keep only non static methods. Abstract
can be used only for non-static methods but not for class members.
Unimplemented static methods are not possible.

Concrete class Abstract class Interface


Concrete class is a Abstract is neither Interface is an 100%
fully implemented 100% abstract nor abstract
class i.e. 100% 100% concrete
It can be instantiated It cannot be It also cannot be
instantiated instantiated
Concrete class can Abstract class can Interface can have
contain only have only abstract only Abstract methods
implemented methods or partially but it can never have
methods or concrete abstract and concrete implemented or
methods but none of or only concrete concrete methods
them can be abstract methods
It may or may not It may or may not It can have only
have attributes, have attributes, attribute(constants)
methods, methods, and abstract method
initialization blocks, initialization blocks,
constructors. constructors.
Attributes can have Attributes can have Attributes can have
any of the access any of the access only public access
levels and either levels and either levels which are only
static or non-static static or non-static static
Methods defined in Methods defined in Methods declared in
concrete class can abstract class is interfaces must and
either static or non- either static or non- should be non-static
static static only

Interfaces are only for external programmers, and they are not
supposed to be exposed with abstract classes and concrete classes.
They have to communicate only with interfaces.

Abstract classes
Interfaces
Differences between concrete, abstract classes and interfaces
080-41310124 http://www.javaeasytoall.com
Lara Technologies

1. What are concrete class?


2. What do you mean by abstraction?
3. How will u declare a method inside a class?
4. Object can be created only for concrete class not for abstract
class(T/F).
5. We can use concrete class to create objects(T/F)
6. Inside a concrete class, abstract calss we can keep attributes,
methods,initialization blocks and constructor(T/F)
7. abstract means which is unimplemented(T/F)
8. why do we need to create constructor inside a abstract class?
9. we can achieve 100% abstract class through interface(T/F).
10.what is interface?
11.by default every member inside member inside a interface is
public, static, final(T/F)
12.by default every meyhod declaration is public abd abstract
inside interface(T/F)
13.inside interface we should not have constructor, method
definition(T/F)
14.all attribute should be public and all method should be
abstract(T/F).
15.we should not keep concrete members inside interface(T/F).
16.we can develop a class by implementing interfaces(T/F).
17.static methods can be overided(T/F)
18.inside interface we should not put static method
declaration(T/F)
19.write difference between concrete class and abstract class
20.write difference between abstract class and interface.
21.what is a final?
22.in a class we cannot go for both final and abstract (T/F)
23.whether final can be used for method?
24.if method is final then they cannot be overrided in the
subclass(T/F)
25.method should not be both final and abstract(T/F)
26.final attributes should not be reinitialized(T/F).
27.where the final attribute should get value?
28.Interfaces should not have any implementation and should no
have any declaration (T/F)
29.Concrete class have only definition(T/F)
30.In concrete calss static and non static methods is possible(T/F)

080-41310124 http://www.javaeasytoall.com
Lara Technologies

27.Default is accessed within package and not outside teh


package(t/f)
28.Protected inherited member should be used only inside the
inherited class(t/f)
28. Write the output for the following programs:

1.
class A
{
abstract void test()
{
System.out.println("Hello World!");
}
}

2.
class B
{
void test();
}

3.
abstract class C

{
abstract void test();
}

4.
abstract class D

{
void abstract test();

5
abstract class E

080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
void test()
{
}

6
abstract class F

{
}
class B extends F
{
};

7
abstract class G

{
abstract void test();
}
class B extends G
{
}

8.
class H
{
}
abstract class B extends H
{
}

9.
abstract class I
{
void test()
{
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class A extends I
{
}

SCJP Oriented

Question 1
Given:
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. }
Which three are valid on line 12? (Choose three.)
A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected
Answer: ABD

Question 2
Given:
11. public abstract class Shape {
12. int x;
13. int y;
14. public abstract void draw();
15. public void setAnchor(int x, int y) {
16. this.x = x;
17. this.y = y;
18. }
19. }
and a class Circle that extends and fully implements the Shape class.
Which is correct?
A. Shape s = new Shape();
s.setAnchor(10,10);
s.draw();
B. Circle c = new Shape();
c.setAnchor(10,10);
c.draw();
C. Shape s = new Circle();
s.setAnchor(10,10);
s.draw();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

D. Shape s = new Circle();


s->setAnchor(10,10);
s->draw();
E. Circle c = new Circle();
c.Shape.setAnchor(10,10);
c.Shape.draw();
Answer: C

Question 3
Given:
10. abstract public class Employee {
11. protected abstract double getSalesAmount();
12. public double getCommision() {
13. return getSalesAmount() * 0.15;
14. }
15. }
16. class Sales extends Employee {
17. // insert method here
18. }
Which two methods, inserted independently at line 17, correctly
complete the Sales class? (Choose two.)
A. double getSalesAmount() { return 1230.45; }
B. public double getSalesAmount() { return 1230.45; }
C. private double getSalesAmount() { return 1230.45; }
D. protected double getSalesAmount() { return 1230.45; }
Answer: BD

Question 4
Given:
1. interface DoStuff2 {
2. float getRange(int low, int high); }
3.
4. interface DoMore {
5. float getAvg(int a, int b, int c); }
6.
7. abstract class DoAbstract implements DoStuff2, DoMore { }
8.
9. class DoStuff implements DoStuff2 {
10. public float getRange(int x, int y) { return 3.14f; } }
11.
12. interface DoAll extends DoMore {
13. float getAvg(int a, int b, int c, int d); }
What is the result?
A. The file will compile without error.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

B. Compilation fails. Only line 7 contains an error.


C. Compilation fails. Only line 12 contains an error.
D. Compilation fails. Only line 13 contains an error.
E. Compilation fails. Only lines 7 and 12 contain errors.
F. Compilation fails. Only lines 7 and 13 contain errors.
G. Compilation fails. Lines 7, 12, and 13 contain errors.
Answer: A

Rule 15 Do not pluralize type names except collections.


Day 16
Final class

080-41310124 http://www.javaeasytoall.com
Lara Technologies

It is a keyword, which will be used for classes, methods and with


attributes.

final class A
{
__
__
__
}

The above code will make note to compiler that it should not allow to
develop any sub classes for it. So, compiler will not compile the java
files if we try to create any sub class for a final class and hence the
above code will not compile. We can never create any sub classes to
final classes. A class can never be abstract and final at once.

Abstract class or method means those should be sub-classed and


abstract methods should be implemented in the sub classes. Final
keyword means it should never be sub-classed and so we cannot use
both abstract and final keyword at once

Final method

class A
{
final void test()
{
}
}
The above says to compiler to allow any sub-class creation to the
class “A” but don’t allow to override the test method in the sub-
classes which is declared as final. If a class is declared as final, then
automatically methods of that class can never be changed and hence
we can say the methods are final even though its methods are not
declared as final.

A method cannot be both final as well as abstract at once.

Final variable
class A
{
final int i = 10;
void test()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

i++;
}
}

Rule 16 Always add the "Exception" suffix to the name of types


derived from java.lang.Exception.

A variable can also be declared as final, it means that the variable


can never change its value in its life time. A variable can never be
abstract i.e. its meaning less in case of java. Final variables can never
be reinitialized with a new value. Final variables must and should be
initialized. Final variables can get the values either at declaration or
in constructor or in initialization block.

All the following cases will be compiled successfully. (Read the above
paragraph)
(a)
class A
{
final int i = 10;
}

(b)
class A
{
final int i;
A()
{
i=10;
}
}

(c)
class A
{
final int i;
{
i = 10;
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Which of the following will be compiled successfully and which are


not?

A)
class A
{
final int i;
}

B)
class A
{
final int i=9;
void test()
{
i = 9;
}
Rule 16 Always add the "Exception" suffix to the name of types
derived from java.lang.Exception.
}

C)
class A
{
final int i=9;
A()
{
i = 9;
}
}

D)
class A
{
final int i;
A()
{
i = 9;
}
}

E)
class A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
final int i;
A()
{
i = 9;
}
A(int i)
{
}
}

F)
class A
{
final int i =25;
A()
{
i = 20;
}
}

G)
class A
{
final int i;

Rule 16 Always add the "Exception" suffix to the name of types


derived from java.lang.Exception.
A()
{
i = 9;
}
A(int k)
{
}
{
i = 9;
}
}

H)
class A
{
static final int i;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
i = 9;
}
}

I)
class A
{
static final int i;
static {
i = 90;
}
}

Compilation success cases: D, I

write the output for the following program


1

class A
{
void test()
{
}
}
class B extends A
{
private void test()
{
}
}

2.
class B
{
public void test()
{
}
}
class C extends A
{
private void test()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}

3.

class C
{
public void test()
{
}
}
class D extends C
{
public void test()
{
}
}

4.

class D
{
final int i=0;
void test()
{
}
}

5. class E
{
final int i=0;
void test();
{
i++;
}
}

6.

class G
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

final int i;
G()
{
i=9;
}
}

7
class H

{
final int i;
}

8.

class I

{
final int i=9;
viod test()
{
i=10;
}
}

9.

class J

{
final int i=9;
J()
{
i=10;
}
}

10
class K

{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

final int i;
K()
{
i=10;
}
}

11
class L

{
final int i;
L()
{
i=10;
}
L(int i)
{
}
{
}
}

12
class M

{
final int i=10;
{
i=10;
}
}

13
class N

{
final int i;
{
i=10;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

14

class P

{
final int i;
{
i=10;
}
P(int k)
{
}
{
i=10;
}
}

15
class Q

{
final int i;
Q()
{
i=10;
}
Q(int k)
{
}
{
i=9;
}
}

16
class R
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
static final int i;
{
i=10;
}
}

17

class S

{
static final int i;
static
{
i=10;
}
}

18

class T

{
static final int i=10;
static
{
i=10;
}
}

19

interface Test
{
public int i=0;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

protected void test();


}
class A implements Test
{
public void test()
{
}
}

20
interface Test1
{
public int i=0;
public void test();
}
class A implements Test1
{
public void test()
{
}
}

21 dev1
interface Test2
{
public
src int i=0;
private void test();
} com
class A implements Test2
lara
{ classes
public void test() Person.java
{ com
}
dev2 lara

Person.class
src

com

lara
classes
Manager.java
com
080-41310124 http://www.javaeasytoall.com
lara
Manager.class
Lara Technologies

Person.java Manager.java
package com.lara; package com.lara;
import com.lara.Person;
public class Person
{ public class Manager
void test() {
{ public void static main(String args[])
S.o.p(“ Hello“); {
} Person p1 = new Person();
} p1.test();
}
}

Rule 16 Always add the "Exception" suffix to the name of types


derived from java.lang.Exception.
In the above example, we are developing 2 applications; one is dev1
and another one is dev2 in the same drive. We are creating 2
packages one is com and another one is lara in com, developing a
class in “Person.java” with Person.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

dev1\src>javac –d ../classes com/lara/*.java

Whenever we execute the above command in command prompt, the


javac command will go to com.lara package and looks for all .java
files then compiler will compile and generate as it independent of
another user defined classes.
If at all for compilation of any java file, there must be either class or
interface declared in it and then if we are using any 3 rd party classes
in any of the class then it requires or looks for the .class file of that
class or that classname.java file in the current source stream. Person
class file is generated as it is independent class.

dev2\src>javac –d ../classes com/lara/*.java

Whenever we execute the above command, the javac command will


go to com.lara package in the dev2 application and looks for all java
files and compiler tries to compile all files. When it finds that there is
a dependency of a class called person in come.lara package for
compiling Manager class, it will be searching inside the com.lara
package in the same application development i.e. dev2, but neither
Person.class files nor the Person.java file is available in the current
source stream.

Compiler when it finds class dependency for a class and when it is not
available it will look for another system environment variable like
path which is named ‘classpath’ entries with the delimiter ‘;’ for
multiple values. And hence we have to update ‘classpath’ variable.
We have 3 ways of setting the ‘classpath’ variable.

First way of setting is command wise setting

dev2\src>lavac –d ../classes –classpath ../../dev1/classes


com/lara/*.java

compilation is successful and it has generated the .class files in the


classes location.

For Execution
dev2\classes>java com.lara.Manager
NoClassDefinitionFoundError: Person

With the above command, execution will not be successful because


for execution also it requires the .class file to be loaded into the
memory while executing.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

dev2\classes>java –classpath ../../dev1/classes com.lara.Manager


NoClassDefinitionFoundError: Manager

the above command will not be compiled even though the ‘classpath’
is updated to the Person class file location, because when the
classpath is updated then it will look for all the all the files in the
updated ‘classpath’ entries, if it doesn’t find it will not be compiled
successfully and says NoClassDefinitionFoundError for Manager.

Rule 16 Always add the "Exception" suffix to the name of types


derived from java.lang.Exception.

dev2\classes>java –classpath ../../dev1/classes;. com.lara.Manager


NoClassDefinitionFoundError: Manager

Execution of the above statement will be successful be classpath is


updated with the Person class file’s location as well as Manager’s
location. Because, ‘.’ represents current directory relatively and ‘;’ is
the delimiter for updating any environment variable while referring
more than one entry.

Final class
Final method
Final variable
Importance of classpath variable

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Rule 16 Always add the "Exception" suffix to the name of types


derived from java.lang.Exception.
Day 17
Classpath
With the same example in the yesterday’s lesson, the second way of
updating the ‘classpath’ variable is using ‘set’ command i.e. for
setting Command window wise setting.

dev2\classes>set classpath=../../dev1/classes
dev2\classes>java com.lara.Manager
NoClassDefinitionFoundError: Manager

the above command will not be compiled even though the ‘classpath’
is updated to the Person class file location, because when the
classpath is updated then it will look for all the all the files in the
updated ‘classpath’ entries, if it doesn’t find it will not be compiled
successfully and says NoClassDefinitionFoundError for Manager.

dev2\classes>set classpath=../../dev1/classes;.
dev2\classes>java com.lara.Manager
Or
dev2\classes>set classpath=.;../../dev1/classes;
dev2\classes>java com.lara.Manager

080-41310124 http://www.javaeasytoall.com
Lara Technologies

the above execution is run successfully, but if at all some other


developer of the same system has already set with the other
application and if it is necessary for us to run with our current
execution this will not be successful because set command will not
append new location to existing variable’s value but it replaces with
new entry. So we should retrieve the existing values and we have to
update it like

dev2\classes>set classpath=%classpath%;../../dev1/classes;.
dev2\classes>java com.lara.Manager
Or
dev2\classes>set classpath=.;../../dev1/classes;%classpath%;
dev2\classes>java com.lara.Manager

The order is not important to be maintained in updation of any of the


environment variables, but values should be present after successful
updation with the new entries.

Third way of updating the classpath is system wise setting

1) Right click on my computer folder on the desktop of our


computer or go to control panel and double click on system
folder, if you don’t find mycomputer on the desktop.
2) Select the advanced tab
3) Click on the button which is named Enviroment variables
4) Click on the new button if there is no ‘classpath’ variable
existing already or if it is existing click on edit button
5) Give ‘classpath’ in the variable name textfield
6) Give all entires delimited with ‘;’ in the variable value textfield
7) Open the new window for changes to be applied
Note: - In this way of updating ‘classpath’ also it is necessary for
mentioning ‘.’ either as the first entry or last entry.
Rule 17 Always start a method name with a lowercase letter.

Jar
Consider a situation that in a company there are multiple projects
are under processing. In all or some of the projects, there are some
common tasks like calculations in all accounts related projects, then
instead of developing the calculations part in all the projects, develop
it once and make a jar file which is similar to zipped file add it to
‘classpath’ variable. This makes available for all the projects under
development.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Jar stands for “Java Archive” file. Using a command called ‘jar’ only
it is possible to make a jar file. Jar content will be only classes folder
content and is enough because for compiler to compile dependent
classes it requires .class files and if .class files are not available then it
requires .java files. So, ‘classes’ folder content is enough for making a
jar file.

Note: - jar command should be triggered from classes folder only,


because we require class files only to be in the jar file. Jar file is
meant for packing all .class files into a file.
Jar command options

jar cf filename.jar

C  stands for creation of the jar file


F  stands for filename with .jar as extension with absolute path or
relative path location where jar file is to be generated and kept (if we
don’t mention relative or absolute it will generate in the classes folder
only.

Calculator.java

Calculator.class

Rule 17 Always start a method name with a lowercase letter.


AccountsManager.java

080-41310124 AccountsManager.class
http://www.javaeasytoall.com
Lara Technologies

calci-dev

src

com

lara
classes
calci
com
Accounts-dev lara

calci
src

com

lara
classes
accounts
com

lara

accounts

As the above explanation and figure represents, traverse up to classes


folder of

calci-dev\src>javac –d ../classes com/lara/calci/*.java


calci-dev\src>set classpath=%classpath%;../../com/lara/calci/classes;.

Make a jar file:-


Create a lib folder in calci-dev folder

calci-dev\classes>jar cf ../lib/calci.jar *

Rule 17 Always start a method name with a lowercase letter.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Open another command prompt window and also update with the jar
files location. Create a folder called lib in accounts-dev folder. Copy
the jar file to lib accounts-dev application folder.

accounts-dev\src>set classpath=%classpath%;../../lib/calci.jar;.
accounts-dev\src>javac –d ../classes com/lara/accounts/*.java
accounts-dev\src>java com.lara.accounts.AccountsManager

Whenever we create a jar file, it contains one more folder other than
classes folder content i.e. META-INF and inside that there will be a
file name MANIFEST.mf with 2 mainfest attributes one is jre version
and author name generated by the jar command automatically. An
when we say java command to execute jar with –jar option, the
command looks for execution and it is looking for a class to start
execution which is identified by an attribute named Main-Class
attribute.

How to make an executable jar file?

src

com

lara

Manager.java package com.lara;

public class Manager


classes {
com public void static main(String args[])
{
lara Person p1 = new Person();
p1.test();
lib Manager.class }
}

1) Create a lib folder in the application folder


2) Create a text file with any name in src ( mf.txt )
3) Write the following code
a. Main-Class: com.lara.Manager

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Give a space after colon i.e. ‘: ‘and press an enter


key after Manager
4) Go to classes folder
5) Run the following command
a. jar cfm ../lib/test.jar ../src/mf.txt *
6) for running, use the following command lib folder
a. java –jar test.jar

Commands

‘javap’ is one of the java related commands which is used for parsing
the class and lets us know the class members. 'java’ and ‘javap’
requires only .class files. If some developer has developed some java
files, and exposed to external world through internet without any
documentation or help documents. Then ‘javap’is used for knowing
the members of a ‘.class’ file.

‘javadoc’ is a command that is used by developer and develop


browser related help documentation.

Classpath
Jar
Javadoc
Javap

1. How to make a jar files?


2. What is the use of jar files?
3. How to make .class file into jar files?
4. What is the command to make .class files in the classes folder
into jar file?
5. Where the jar files are located?
6. Which are the sub folder obtained if we extract jar folder?
7. What does meta-inf folder contain?
8. Wheather meta-inf is automatically created while extracting
jar folder or created by the developer at the time of extracting
the jar folder?
9. What are maniferst attributes?
10.What is the use of javap command?
11.What is cf?
12.What is fc?
13.Why cf or fc is used?

080-41310124 http://www.javaeasytoall.com
Lara Technologies

14.How to make docs?


15.What is the use of “javap java.lang.manager”?
16.What is the use of “ javap java.lang.system”?
17.What is the use of “ java.lang.string”?
18.For what java doc is used?
19.How to doc for private and public?
20.how to write the documentation in programs?
21.which are the three types of comments?
22.how to represent single line comment, multiline comment and
javadoc comments?

SCJP Oriented

Question 1
A class games.cards.Poker is correctly defined in the jar file
Poker.jar.
A user wants to execute the main method of Poker on a UNIX system
using the command:
java games.cards.Poker
What allows the user to do this?
A. put Poker.jar in directory /stuff/java, and set the CLASSPATH to
include /stuff/java
B. put Poker.jar in directory /stuff/java, and set the CLASSPATH to
include /stuff/java/*.jar
C. Put Poker.jar in directory /stuff/java, and set the CLASSPATH to
include /stuff/java/Poker.jar
D. put Poker.jar in directory /stuff/java/games/cards, and set the
CLASSPATH to include /stuff/java
E. put Poker.jar in directory /stuff/java/games/cards, and set the
CLASSPATH to include /stuffijava/*.jar
F. put Poker.jar in directory /stuff/java/games/cards, and set the
CLASSPATH to include /stuff/java/Poker.jar
Answer: C

Rule 17 Always start a method name with a lowercase letter.

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Day 19
1.what is ‘instanceof’ ?
2. write the output
Class A
{
}
Class B
{
}
Class C
{
}

Class Manager
{
Public static void main(String args[])
{
A a1=new A();
A a1=new A();
A a2=new A();
A a3=new A();
B b1=new B();
B b2=new B();
C c1=new C();
System.out.println(a1 instanceof A);
System.out.println(a1 instanceof B);
System.out.println(a1 instanceof C);
System.out.println(a2 instanceof A);
System.out.println(a2 instanceof B);
System.out.println(a2 instanceof C);
System.out.println(a3 instanceof A);
System.out.println(a3 instanceof B);
System.out.println(a3 instanceof C);
System.out.println(b1 instanceof A);
System.out.println(b1 instanceof B);
System.out.println(b1 instanceof C);
System.out.println(b2 instanceof A);
System.out.println(b2 instanceof B);
System.out.println(b2 instance of C);

080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(c1 instance of A);


System.out.println(c1 instance of B);
System.out.println(c1 instance of C);

}
}

3.
Class A
{
}
Class B
{
}
Class Manager
{
Public static void main(string args[]
{
A a1= new A();
If(a1 instance of A)
{
System.out.println(“type of A”);
}
Else
{
System.out.println(“ not a type of A”);
}

If(a1 instance of B)
{
System.out.println(“type of B”);
}
Else
{
System.out.println(“ not a type of B”);
}
}
}

4.
Class A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

{
}
Class B extends A
{
}
Class Test
{
Void test(object obj)
{
If(obj instance of B)
{
B a1=(B) obj;
}
}
}
Class Manager
{
Public static void main(String args[])
{
A a1=new A();
A a1=new A();
B b1=new B();
Test t1=new test();
t.test(a1);
t.test(a2);
t.test(b1);
}
}

5. what is command line argument?

6.
Class Manager
{
Public static void main(String agrs[])
{
System.out.println(args[0]);
System.out.println(args[1]);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

7.
Class Manager
{
Public static void main(String agrs[])
{
System.out.println(“hello to all” + args[0]);
}
}
Classes>java Manager world
Classes>java Manager

8.
Class Manager
{
Public static void main(String agrs[])
{
System.out.println(“hello to all” + args[0]);
}
}
Classes>java Manager abc xyz
Classes>java Manager abc

SCJP Oriented

Question 1
A programmer needs to create a logging method that can accept an
arbitrary number of arguments. For example, it may be called in
these
ways:
logIt(”log message 1 “);
logIt(”log message2”,”log message3”);
logIt(”log message4”, “log message5”, “log message6);
Which declaration satisfies this requirement?
A. public void logIt(String * msgs)
B. public void logIt(String [] msgs)
C. public void logIt(String... msgs)
D. public void logIt(String msg1, String msg2, String msg3)
Answer: C

Question 2
Click the Exhibit button.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

1. public class A {
2. public String doit(int x, int y) {
3. return “a”;
4. }
5.
6. public String doit(int... vals) {
7. return “b”;
8. }
9. }
Given:
25. A a=new A();
26. System.out.println(a.doit(4, 5));
What is the result?
A. Line 26 prints “a” to System.out.
B. Line 26 prints ‘b” to System.out.
C. An exception is thrown at line 26 at runtime.
D. Compilation of class A will fail due to an error in line 6.
Answer: A

Question 3
Given:
11. public class Counter {
12. public static void main(String[] args) {
13. int numArgs = /* insert code here */;
14. }
15. }
and the command line:
java Counter one fred 42
Which code, inserted at line 13, captures the number of arguments
passed into the program?
A. args.count
B. args.length
C. args.count()
D. args.length()
E. args.getLength()
Answer: B

Question 4
Given:
15. public class Yippee {
16. public static void main(String [] args) {
17. for(int x = 1; x < args.length; x++) {
18. System.out.print(args[x] +“ “);
19. }
080-41310124 http://www.javaeasytoall.com
Lara Technologies

20. }
21. }
and two separate command line invocations:
java Yippee
java Yippee 1234
What is the result?
A. No output is produced.
123
B. No output is produced.
234
C. No output is produced.
1234
D. An exception is thrown at runtime.
123
E. An exception is thrown at runtime.
234
F. An exception is thrown at rijntime.
1234
Answer: B

Question 5
Given:
12. public class Yippee2 {
13.
14. static public void main(String [] yahoo) {
15. for(int x= 1; x<yahoo.length; x++) {
16. System.out.print(yahoo[x] + “ “);
17. }
18. }
19. }
and the command line invocation:
java Yippee2 a b c
What is the result?
A.a b
B.b c
C.a b c
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: B

Question 6
Given:
11. public class Commander {
12. public static void main(String[] args) {
080-41310124 http://www.javaeasytoall.com
Lara Technologies

13. String myProp = /* insert code here */


14. System.out.println(myProp);
15. }
16. }
and the command line:
java -Dprop.custom=gobstopper Commander
Which two, placed on line 13, will produce the output gobstopper?
(Choose two.)
A. System.load(”prop.custom”);
B. System.getenv(”prop.custom”);
C. System.property(”prop.custom”);
D. System.getProperty(”prop.custom”);
E. System.getProperties().getProperty(”prop.custom”);
Answer: DE

Question 7
Given:
11.class Mud{
12. //insert code here
13. System.out.println(“hi”);
14. }
15. }
And the following five fragments
public static void main(String…a);{
public static void main(String.*a);{
public static void main(String… a);{
public static void main(String[]… a);{
public static void main(String…[] a);{
How many of the code fragments,inserted independently at line 12
,compile?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5
Answer: D

Question 8
Given:
1. public class Barn{
2. public static void main(String args[]){
3. new Barn().go(“hi”,1);
4. new Barn().go(“hi”,”world”,2);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

5. }
6. public void go(String…y,int x){
7. System.out.println(y[y.length-1]+””);
8. }
9. }
What is the result?
A. hi hi
B. hi world
C. world world
D. compilation fails
E. An Exception is thrown at runtime.
Answer: D

Expertise With Core Intensive Questionare - 1

1. While installing JDK 5.0, which option can’t be filtered?


2. Is it possible to install JDK without public JRE?
3. What are the different features will install while installing
JDK 5.0?
4. Which feature is mandatory while installing JDK 5.0
5. What stands for JDK?
6. What stands for J2SE?
7. What stands for JRE?
8. What is the latest version of Java?
9. Is it mandatory to install public JRE for development?
10. JDK is an open source. True/false?
11. What are the folders will be getting inside installed location
of JDK?
12. By default where JDK will try to install?

080-41310124 http://www.javaeasytoall.com
Lara Technologies

13. Is it advisable to install JDK inside program files of your


OS?
14. In which folder java related utilities/tools will be available
inside JDK installation?
15. What is the jar file name and its location, which contains
utilities(commands) related libraries?
16. What is the jar file name and its location, which contains
core libraries of Java?
17. What are the differences between bin folder of development
kit and Runtime Environment?
18. Without updating PATH environment variable, where shall
I develop my Java Programs?
19. What is the command used for compilation?
20. What is the command used for running?
21. What is the extension of java program?
22. What is the extension of compiled java file?
23. Which block is mandatory inside a java program?
24. To compile & run java program from any location, which
environment variable should be updated?
25. By default, where compiled .class file will be created?
26. How to represent one directory up to the present directory
in command window (windows OS)?
27. How to compile all java files which are there in one
directory in a single compilation?
28. How to direct all compiled .class files in to different
directory rather than mixing with .java files?
29. How to set an environment variable, which should be
available for all commands which are running in the same
command window?
30. How to set PATH environment variable which should be
available to all command windows?
31. Why do we require to set PATH environment variable?
32. How to read an environment variable from command
window?
33. How to update an existing environment variable from the
command window?
34. How to set environment variable which should be available
to all command windows for one user?
35. How to represent current directory in the command window
(windows OS)?
36. How to set environment variable which should be available
to all command windows for all users of the system?
37. What is the delimiter while setting more than one entry in
environment variable?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

38. Is it possible to keep more than one class in the same file?
39. No of .class files will depend on no of class declarations in a
file T/F?
40. How many classes can declare as public in one file?
41. How many classes can declare as a package scope in a file?
42. Is it possible to save a file with other than class name, if only
one public class is there inside that file?
43. If all classes are non public classes in one file, then is it
possible to save that file with Abc.java?
44. Is it possible to keep main method in more than one class in
the same file?
45. Is it possible to keep main method as follows, static public
void main (String args [])?
46. public static void main (String params [])?
47. public static void main (String [] test)?
48. public void static main (String args [])?
49. public static void main (String param)?
50. public void main (String args [])?
51. private static void main (String args [])?
52. static void main (String args [])?
53. protected static void main (String args [])?
54. Is it possible to call main method of one class from another
class main method?
55. Is it possible to run a class which doesn’t have main
method?
56. How to find an array is a not null or an empty?
57. String array, which is passing to main method will contain
class name by default True/false?
58. By default string array which is passing to main method is
empty. True/false?
59. What is the delimiter while passing more than one
command line argument?
60. How to pass space as a part of command line argument?
61. How to count command line arguments?
62. How to represent an entity in java?
63. How to represent properties of an entity in Java?
64. How to represent behaviors of an entity in Java?
65. What are members of a class?
66. What are members of a class, which are having executable
code?
67. What are the members of a class, which should use to
represent state of an object?
68. Which members of a class will become part of an object?
69. Class is a blue print for similar objects. T/F?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

70. Where objects will be stored (Heap/Stack)?


71. Where methods will be executed (Heap/Stack)
72. Where local variables will be stored (Heap/Stack)
73. What is the default value for int primitive variable which is
declared as an attribute to a class?
74. What is the default value for boolean variable, which is
declared as an attribute to a class?
75. What is the default value for derived data type, which is
declared as an attribute to a class?
76. What is the default value for local variable?
77. Static attributes are common to all objects. T/F?
78. Attributes of a class will be used to store state of an object.
T/F?
79. Is it mandatory to initialize local variables before use?
(Yes/no)?
80. Is it possible to declare a method with an identifier as class
name?
81. Is it possible to write a method without return type?
82. Method will be used to find behavior of an object T/F?
83. Return type of a method will come into method signature or
not?
84. While overloading a method, return type should be same or
not?
85. How to differentiate local variable and global variable
inside method?
86. Is it possible to declare local variable and global variable
with the same name?
87. Constructors can have return type T/F?
88. Default constructor is a no arg constructor T/F?
89. What is the access level of default constructor?
90. How many members will be in an empty class?
91. Constructors will be inherited to sub class. T/F?
92. What could be the first statement inside a constructor?
93. Is it possible to call constructor explicitly?
94. When constructor will be executed?
95. What is constructor chaining?
96. Is it possible to call one constructor from another
constructor?
97. Which block of a class will be called for every object
creation immaterial of which constructor we are using?
98. Is it possible to keep more than one initialization block in
the same class?
99. Is it possible to call initialization block explicitly?
100. Initialization blocks will be inherited to sub classes. T/F?
080-41310124 http://www.javaeasytoall.com
Lara Technologies

101. Initialization blocks will be executed after all super class


constructors. T/F?
102. Is it possible to keep super(); inside initialization block?
103. When method behavior will not change from one object to
another?
104. Is it possible to keep this(); in the second statement inside a
constructor?
105. Is it possible to use super or this in side a static members?
106. Is it possible to call static members from a constructor?
107. Is it possible to declare constructors as a static?
108. Is it possible declare a class itself a static?
109. Is it possible to access instance members by class name?
110. What are the functionalities of dot (.) operator?

111. I have directory structure as follows


class Hello
E:\lab {
dev
}
src
Hello.java

If I compile like
E:\lab\dev\src> javac Hello.java
1. Where compiled .class file will be kept?
2. What could be the package for Hello?
112. In the above program, is it possible to access Hello any
where?
113. If I want to access in another development what are all the
changes should I made?
114. I have directory structure as follows
Hello.java
E:\lab
package com;
dev
class Hello
src {
com
}
Hello.java

If I compile like
1. E:\lab\dev\src>javac Hello.java
2. E:\lab\dev\src>javac com/Hello.java
3. E:\lab\dev\src>javac com/*.java
1. Where compiled .class file will be in above scenarios, if
compiled successfully?
What could be the package for Hello?

080-41310124 http://www.javaeasytoall.com
Lara Technologies

115. If I want to access class Hello in above process from any


where, what are all the things I should take care?
116. I have directory structure as follows
A.java

package pack1;
E:\lab class A
{
dev
}
src
pack1

A.java
B.java
pack2
package pack2;
B.java class B
{

}
If I compile like
1. E:\lab\dev\src>javac pack1/*.java
How many compiled class will be generated and where they
kept?
2. E:\lab\dev\src>javac pack2/*.java
How many compiled class will be generated and where they
kept?
3. E:\lab\dev\src>javac *.java
How many compiled class will be generated and where they
kept?
117. Is it possible to compile A.java & B.java together in the
above program?
118. I have directory structure as follows

Hello.java

E:\lab package pack1;


public class Hello
dev {
src
}
pack1
Hello.java Hello.java

package pack2;
pack2 import pack1.Hello;
Hello.java class Hello
{
080-41310124 http://www.javaeasytoall.com
}
Lara Technologies

1. E:\lab\dev\src>javac pack2/*.java
How many classes will be created and where they will be kept?
2. E:\lab\dev\src>javac pack1/*.java
How many classes will be created and where they will be kept?
119. How many classes will be created and where they will be A.java
kept?
package pack1;
E:\lab class A
{
dev
}
src
pack1
B.java
A.java

sub1 package pack1.sub1;


class B
B.java {
A a1 = new A();
}
If I compile like
1. E:\lab\dev\src>javac pack1/*.java
How many classes will be created and where they will be kept?
2. E:\lab\dev\src>javac pack1/sub1/*.java
How many classes will be created and where they will be kept?

120.
E:\lab A.java
dev package src.pack1;
class A
src {
pack1
}
A.java class B
{

classes }

If I compile
1. E:\lab\dev\src>javac –d classes pack1/*.java
2. E:\lab\dev\src>javac pack1/*.java
3. E:\lab\dev\src>javac –d src/classes src/pack1/*.java
In the above cases
1. Compilation success or not
2. If compilation success, then what about the package for both
class A & B
080-41310124 http://www.javaeasytoall.com
Lara Technologies

3. If compilation success, then where compiled .class files will be


kept

121.
A.java
E:\lab
private class Hello
dev {
src
}
A.java

1. E:\lab\dev\src>javac –d ../../ *.java


1. Whether compilation success or not?
2. If compilation success, then where compiled .class files will be
kept
122. In the above program if classes A is declared as package
scope (True/ False)?
123. In the above program if classes A is declared as protected
scope (True/ False)?
124. In the above program if classes A is declared as public scope
(True/ False)?
125. How many .class files will be created while compiling one
.java file?
126. Is it possible to divert some class to one place and some to
another place from the same compilation?
127. class is a blue print. True/false?
128. object can be created with new operator. True/false?
129. new is a key word and is used for creating object.
130. Variables are two types. Primitive and instance variable.
True/false?
131. Instance variables are called instance references. True/false
132. How many instances can be referred by one instance
reference at any moment?
133. Is it possible to have more than one instance reference to
one object?
134. What is the default literal to instance reference?
135. null is a key word. True/false?
136. true is a keyword. True/false
137. false is a key word. True/false?
138. main is a key word. True/false?
139. args is a key word. True/false?
140. Is it possible to give name to object?
141. Is it possible to have instance references in side an object?

080-41310124 http://www.javaeasytoall.com
Lara Technologies

142. Is it possible to have a class as one of the attribute of same


class type?
Ex: class A { A a1;}
143. Is it possible to refer an object by one of its member?
144. Is it possible to count references of an object?
145. Is it possible to refer any type of reference to any object?
146. Is it possible to have a reference from another object?
147. Casting is converting one data type to another. True/false
148. public class Manager1 {
int age;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
System.out.printl(m1.age);
}
}
149. public class Manager1 {
int age;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
System.out.printl(age);
}
}
150. public class Manager1 {
int static age;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
System.out.printl(age);
}
}
151. public class Manager1 {
static int age;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
System.out.printl(age);
}
}
152. public class Manager1 {
int age;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
m1.age = 29;
System.out.printl(m1.age);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

153. public class Manager1 {


int age;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
m1.age = 29;
System.out.printl(m1.age);
}
}
154. public class Manager1 {
static int age;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
m1.age = age = 29;
System.out.printl(m1.age);
System.out.printl(age);
}
}
155. public class Manager1 {
static int age = 15;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
m1.age = age = 29;
System.out.printl(m1.age);
System.out.printl(age);
}
}
156. public class Manager1 {
static int age = 15;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
Manager1 m2 = m1;
System.out.printl(m1.age);
System.out.printl(m2.age);
System.out.printl(age);
}
}
157. public class Manager1 {
int age = 15;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
Manager1 m2 = m1;
System.out.printl(m1.age);
System.out.printl(m2.age);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
How may objects and references are creating in the above
program and what could be the output?
158. public class Manager1 {
int age = 15;
Manager m1;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
Manager1 m2 = m1;
System.out.printl(m1.age);
System.out.printl(m2.age);
}
}
How may objects and references are creating in the above
program and what could be the output?

How may objects and references are creating in the above


program and what could be the output?
159. public class Manager1 {
int age = 15;
Manager m1;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
Manager1 m2 = m1.m1;
m1.m1 = m1;
System.out.printl(m1.age);
System.out.printl(m1.m1.age);
System.out.printl(m2.age);
}
}
How may objects and references are creating in the above
program and what could be the output?
160. public class Manager1 {
int age = 15;
Manager m1;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
Manager1 m2 = m1.m1 = m1;
System.out.printl(m1.age);
System.out.printl(m1.m1.age);
System.out.printl(m2.age);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

How may objects and references are creating in the above


program and what could be the output?
161. public class Manager1 {
int age = 15;
Manager m1, m2;
public static void main(String[] args) {
Manager1 m1 = new Manager1();
Manager1 m2 = m1.m1 = m1.m2 = m1;
m1.age = m1.m2.age – m2.m1.age
System.out.printl(m1.age);
System.out.printl(m1.m1.age);
System.out.printl(m2.age);
}
}
How may objects and references are creating in the above
program and what could be the output?
162. Hiding unnecessary details and showing necessary details is
called as?
163. Is abstract class is 100% abstractness?
164. Is interface is a 100% abstract
165. is it possible to keep static with abstract?
166. Is it possible to keep final with abstract?
167. is it possible to keep a class as abstract without having any
abstract methods()?
168. abstract methods are inherited to subclass or not ?
169. Is it possible to keep concrete methods in side interface?
170. is it possible to keep concrete methods in side abstract
class?
171. Is it possible to create the object to abstract class?
172. is it possible to keep constructors in side interface?
173. Is it possible to keep constructors in side abstract class?
174. is it possible to create the object to interface?
175. if u keep the non static concrete methods in side the abstract
class ,how to acces that
methods by using same class object or sub class object?
176. Is it possible to give the definition for abstract methods at
time of declaration itself?
177. Abstract is used for constructors?
178. Abstract is used for initialization blocks?
179. can u able to override the abstract methods in side subclass?
180. is it possible to declare interface is a abstract ?
181. is it possible to declare the abstract to variables?
182. Class A{
void m(){
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
abstract void m1();
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
183. Abstract class A{
void m(){
System.out.println("A");
}
static abstract void m1(){
System.out.println("abstract");
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
184. abstract interface A{
void m1();
public abstract void m2();
public void m3();
abstract void m4();
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
185. interface B{
void m();
}
class A implements B{}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
186. interface B{
void m();
}
class A implements B{
void m()
{
System.out.println("hello ");
public static void main(String args[]){
new A().m();
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello
187. Abstract class A{
public void methedA(){
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println("helloA");
}
public static void main(String args[]){
A a1=new A();
A1.methodA();
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints helloA.
188. interface B{
void m1();
}
class A implements B{}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
189. interface B{
private abstract void m1();
}
class A extends B{
public void m1(){
System.out.println("hi");
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully &
prints hi
190. interface B{
abstract void m1();
}
class A implements B{
protected void m1(){
System.out.println("hello");
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
&prints hello.
191. interface B{
abstract void methodB();
}
class A implements B{
public abstract void methodB(){
System.out.println("hi");
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello.
192. interface B{
public static abstract void interfaceMethod();
}
abstract class A implements B{
public void interfaceMethod (){
System.out.println("hi");
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello.
193. interface B{
public final abstract void interfaceMethod();
}
abstract class A implements B{
public void interfaceMethod (){
System.out.println("hello");
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello.
194. interface B{
public static abstract void interfaceMethod();
}
abstract class A implements B{
public void interfaceMethod ()
{
System.out.println("hello");
}
public static void main(String args[]){
B b1=new A();
b1.interfaceMethod();
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello.
195. interface B{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public abstract void interfaceMethod();


}
abstract class A implements B{
public void interfaceMethod (){
System.out.println("hi");
}
public static void main(String []args){
new B().interfaceMethod();
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello.
196. interface B{
void interfaceMethod();
}
class A implements B{
public void interfaceMethod (){
System.out.println("hello");
}
public static void main(String args[]){
new A().interfaceMethod();
}
}
what is the result ?
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello.
197. interface B{
void interfaceMethod();
void interfaceMethod1();
void interfaceMethod2();
}
class A implements B{
public void interfaceMethod (){
System.out.println("hello");
}
protected void interfaceMethod1(){
System.out.println("hello to all");
}
private void interfaceMethod2(){
System.out.println("hi");
}
public static void main(String args[]){
new A().interfaceMethod();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

new A().interfaceMethod1();
}
}
what is the result ?
a.CTE b. RTE c. Compile success d. prints hello and helloto all.
198. abstract class A{
public void main(){
System.out.println("A");
}
}
class B implements A{
public static void main(String args[]){
new B().main();
}
}
a.CTE b. RTE c. Compile success d. prints A;
199. abstract class A {
void main();
}
Class C extends A{
public void main()
{
System.out.println("good");
}
public static void main(String args[]){
new C().main();
}
}
a.CTE b. RTE c. Compile success d. prints good.
200. abstract class A{
abstract void m1();
public void main(){
System.out.println("test");
}
}
abstract class B extends A{
abstract void m2();
}
class C extends B{
public void m2(){
System.out.println("class C")
}
}
a.CTE b. RTE c. Compile success d. prints class C;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

201. interface I{
abstract int i=10;
public abstract void test();
}
abstract class A implements I{
public void test(){
System.out.println("test");
}
public static void main(String ... args) {
System.out.println(new A().i);
}
}
a.CTE b. RTE c. Compile success d. prints 10;
202. interface Test{
int i;
public void test();
}
abstract class A implements Test {
public void test(){
System.out.println("test");
}
public static void main(String ... args) {
Test t=new A();
t.i=20;
System.out.println(new A().i);
}
}
a.CTE b. RTE c. Compile success d.CS&Run succes & prints 20
e.CS&Run succes & prints 0;
203. abstract class Test{
abstract Test();
abstract void Test(int i);
}
class ExtendsTest extends Test{
public Test(){
System.out.println("this is test");
}
public void Test(int i){
System.out.println("this id test int test");
}
static public void main(String args[]) {
int i=20;
new ExtendsTest().Test(i++);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
a.CTE b. RTE c. Compile success d.CS&Run succes & prints 20
e.CS&Run succes & prints 0;
f. prints 21;
204. interface I{
void testA();
}
class A implements I{
void testA(){
System.out.println("One");
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints One
205. interface Test{
public void test(){
System.out.println("Test");
}
}
class C implements Test{}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints Test
206. interface I{
void testA(){
System.out.println("One");
}
}
class A implements I{
public static void main(String args[]){
void testA(){
System.out.println("Two");
}
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints One. f.prints Two.
207. Interfac A{
void test();
System.out.println("A");
}
Interface B extends A{
void test();
System.out.println("B");
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

class C implements B{
public static void main(String args[]){
void test(){
System.out.println("C");
}
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints B. f.prints C
208. By default every method inside a interface is "public static
abstract "(true/false)
209. in side interface every variable is "public abstract static
final "(true/false)
210. interface can implements the other abstract class?
(true/false)
211. in side one abstract class u are able to keep other inter face
defination?(true/false).
212. interface should have method implementation.
213. which access level to be followed while overriding the
method. If method is declared as protected in super class.
214. abstract class A{
public void testA{
System.out.println("Lara");
}
}
class B extends A{
public static void main(String args[]){
public void testA{
System.out.println("Java");
}
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.Java f. Lara
215. class A{
abstract void testA(){
System.out.println("hello");
}
}
class B {
protected void testA(){
System.out.println("Java");
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

216. interface A{
void testA();
}
class B{
void testB(){
System.out.println("B");
}
}
interface C{
void testC();
}
class Manager extends B implements A,C{
public static void testA(){
System.out.println("A");
}
public static void testB(){
System.out.println("B");
}
public static void testC(){
System.out.println("C");
}
public static void main(String args[]){
System.out.println("main");
Manager.testA();
Manager.testB();
Manager.testC();
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints A. f.prints B. g.prints C
217. public class A extends B{
void testC(){
System.out.println("Hello To All");
}
public static void main(String args[]){
new A().testc();
}
}
abstract class B{
void testC();
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints Hello To All.
218. class One implements Two{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public int test(){


return 9;
}
}
interface Two{
int test();
}
class Manager{
public static void main(String one[]){
One one = new One();
System.out.println(one.test());
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints 9.
219. class True{
abstract void test1();
}
class False extends True{
public void test1(){
System.out.println("hello");
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello
220. abstract class A{
void testA(){
System.out.println("hello");
}
}
class B extends A{
public static void main(String one[]){
System.out.println(new B.testA());
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints hello.
221. protected class Hello{
public static void main(String one[]){
public void hi(){
System.out.println("Hi");
}
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

a.cte b. RTE c. Compile success d.compile and run successsully


e.prints Hi.
222. public abstract class A{
String test();
}
class B extends A{
public String test(){
return Java;
}
public static void main(String one[]){
B b1 = new B();
System.out.println(b1.test());
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints Java.
223. interface A {
void test();
}
interface B implements A{
void test();
}
abstract class C implements B{
public static void main(String one[]){
System.out.println("Yes");
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints Yes.
224. class One{
void test();
}
class Two extends One{
void test(){
System.out.println("one");
}
}
a.cte b. RTE c. Compile success d.compile and run successsully
e.prints one.
225. interface A{}
class B{
public static void test(){System.out.println("test");}
}
abstract public class Abc extends B implements A {
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String[] args) {


new Abc().test();
Abc.test();
}

}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints test test.
226. interface A{}
class B{
public final static void test(){System.out.println("test");}
}
public class Abc extends B implements A {
public static void main(String[] args) {
new Abc().test();
Abc.test();
}

}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints test test.
227. interface A{ void test();}
class B{
void test(){ System.out.println("test"); }
}
public class Abc extends B implements A {
public static void main(String[] args) {
new Abc().test();
}
}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints test.
228. interface A{ void test();}
class B{
public void test(){ System.out.println("test"); }
}
public class Abc extends B implements A
{
public static void main(String[] args) {
new Abc().test();
}
}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints test.
080-41310124 http://www.javaeasytoall.com
Lara Technologies

229. interface A{void test();}


interface B(void test();)
abstract class C(void test();)
class D extends C implements A,B{
public void test(){}
}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints one.
230. interface A{void test();}
interface B(void test();)
abstract class C(void test();)
class D extends C {
public void test(){}
}
class E implements A,B{
public void test(){}
}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints one.
231. interface A{void test();}
interface B(void test();)
abstract class C(abstract void test();)
class D extends C {
public void test(){}
}
class E implements A,B{
public void test(){}
}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints one
232. interface X {
public A test();
}
class Y implements X{
public B test();
}
if u want to compile this program successfully what is the
releationship between A and B;
a. A extends B
b. B extends A
c. B extends X and A extends X
d. B implements X and A implements X
e.CTE
f.RTE
080-41310124 http://www.javaeasytoall.com
Lara Technologies

233. interface X {
public static final int i;
final float f;
static public String S;
}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints none
234. abstract class A{
int i=20;
A(int i){
System.out.println(i);
}
}
class B extends A
{
public static void main(String args[]){
new B();
}
}
a.CTE b. RTE c. Compile success d.compile and run successsully
e.prints 20;
235. abstract class A{
int i=20;
A(int i) {
System.out.println(i);
}
}
class B extends A{
B() {
super(40);
}
public static void main(String args[]){
new B();
}
}
a.CTE b. RTE c. Compile success d.prints 40 e.prints 20;
236. abstract class A{
int i=20;
A(){
this(70);
}
A(int i){
System.out.println(i);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
class B extends A{
B(){
this(50);
}
B(int k) {
super(60);
}
public static void main(String args[]){
A a = new B();
}
}
a.CTE b. RTE c. prints 50 d.prints 60 e.prints 20 f.prints 70;
237. abstract class A{
int i=20;
A(int i) {
System.out.println(i);
}
abstract static {};
}
class B extebds A{
B(){
super(40);
}
public static void main(String args[]) {
new B();
}
}
a.CTE b. RTE c. Compile success d.prints 40 e.prints 20;
238. abstract class A{
int i=20;
A(int i) {
System.out.println(i);
}
static {
Syetm.out.println("static ");
}
}
class B extends A{
B(){
super(40);
}
public static void main(String args[]){
new B();
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
a.CTE b. RTE c. Compile success d.prints static and 40 e.prints
static and 20;
F. prints 40 and static g.prints 20 and static
239. abstract classA {
int i=20;
A(int i){
System.out.println(i);
}
static {
Syetm.out.println("static ");
}
}
class B extends A{
B(){
super(40);
}
abstract B(int j){
A(j);
}
public static void main(String args[]){
A a1= new B(50);
}
}
a.CTE b. RTE c. Compile success d.prints static and 40 e.prints
static and 20;
F. prints 40 and static g.prints 20 and static h.prints 50 20 40 static
;
240. abstract class A{
int i=20;
A(int i){
System.out.println(i);
}
static {
Syetm.out.println("static ");
}
{
System.out.println("instance");
}
}
class B extends A
{
B(){
080-41310124 http://www.javaeasytoall.com
Lara Technologies

super(40);
}
public static void main(String args[]){
A a=new B();
}
}
a.CTE b. RTE c. Compile success d.prints static , intance and 40
e.prints static,instance and 20;
f. prints 40 ,instance and static g.prints instance static and 20;

241. abstract class A {


int i=20;
A(){
System.out.printnln("hello");
}
A(int i){
this();
System.out.println(i);
}
static {
Syetm.out.println("static ");
}
{
System.out.println("instance");
}
}
class B extends A{
B(){
super(40);
}
public static void main(String args[]){
A a=new B();
}
}
a.CTE b. RTE c. Compile success d.prints static , intance, hello and
40 e.prints hello,static,instance and 20;
f. prints 40 ,instance and static , hellog.prints instance static and 20;

080-41310124 http://www.javaeasytoall.com
Lara Technologies

Expertise With Core Intensive Questionare - 2

Note:
1. CTE = Compile Time Error
2. RTE = Run Time Error
3. CS = Compilation Success

1. public class Manager {


int i = 9;
public static void main(String[] args) {
System.out.println(i++);
}
}

080-41310124 http://www.javaeasytoall.com
Lara Technologies

a. CTE b. RTE c. CS and prints 10


d. CS and prints 11
2. public class Manager {
public static void main(String[] args) {
int i;
System.out.println(i++);
}
}
a. CTE b. RTE c. CS and prints 0 d.
CS and prints 1
3. public class Manager {
public static void main(String[] args) {
int i;
i++;
System.out.println(i++);
}
}
a. CTE b. RTE c. CS and prints 0 d.
CS and prints 1
4. public class Manager {
public static void main(String[] args) {
int i, j;
i = j;
System.out.println(i);
}
}
a. CTE b. RTE c. CS and prints 0 d.
CS and prints 1
5. public class Manager {
public static void main(String[] args) {
int i, j;
i = j = 0;
System.out.println(i);
}
}
a. CTE b. RTE c. CS and prints 0 d.
CS and prints 1
6.
7. public class Manager {
static int i = 0;
public static void main(String[] args) {
int i;
this.i = i;
System.out.println(++i);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
a. CTE b. RTE c. CS and prints 0 d.
CS and prints 1
8. public class Manager {
static int i = 0;
public static void main(String[] args) {
int i = 2;
System.out.println(++i);
}
}
a. CTE b. RTE c. CS and prints 1 d.
CS and prints 3
9. public class Manager {
boolean flag;
public static void main(String[] args) {
System.out.println(new Manager().flag);
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
10.public class Manager {
boolean flag;
public static void main(String[] args) {
if(flag=true){
System.out.println(flag);
}
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
11.public class Manager {
boolean static flag;
public static void main(String[] args) {
if(flag=true){
System.out.println(flag);
}
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
12.public class Manager {
static boolean flag;
public static void main(String[] args) {
080-41310124 http://www.javaeasytoall.com
Lara Technologies

if(flag=true){
System.out.println(flag == false);
}
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
13.public class Manager {
static boolean flag = !true;
public static void main(String[] args) {
if(flag=true){
System.out.println(flag = false);
}
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
14.public class Manager {
String name = null;
static{
name = "abc";
}
public static void main(String[] args) {
System.out.println(name.length());
}
}
a. CTE b. CS and Gives nothing. c. CS and
Gives NPE
d. CS and prints 3
15.public class Manager {
static String name = null;
static{
name = "abc";
}
public static void main(String[] args) {
System.out.println(name.length);
}
}
a. CTE b. CS and Gives nothing. c. CS and
Gives NPE
d. CS and prints 3
16.public class Manager {
static final String name;
public static void main(String[] args) {
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println(name.length());
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 3
17.public class Manager {
static final String name;
static{
name = "abc";
}
public static void main(String[] args) {
System.out.println(name.length());
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 3

18.public class Manager {


static final String name = "init";
static{
name = "abc";
}
public static void main(String[] args) {
System.out.println(name.length());
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 3
19.public class Manager {
static final String name;
static void test(){
name = "abc";
}
public static void main(String[] args) {
System.out.println(name.length());
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 3
20.public class Manager {
080-41310124 http://www.javaeasytoall.com
Lara Technologies

static final int all[];


static{
all = new int[10];
}
public static void main(String[] args) {
System.out.println(all.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 10 e. CS and gives AIOBE

21.public class Manager {


static final int all[];
static{
all = new int[]{1,2,3};
}
public static void main(String[] args) {
System.out.println(all.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 3 e. CS and gives AIOBE
22.public class Manager {
static final int all[] = new int[]{0};
public static void main(String[] args) {
System.out.println(all.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and gives AIOBE
23.public class Manager {
static final int all[] = new int[]{0};
static{
all = {0,1,3};
}
public static void main(String[] args) {
System.out.println(all.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
080-41310124 http://www.javaeasytoall.com
Lara Technologies

d. CS and prints 1 e. CS and prints 3


24.public class Manager {
static final int all[] = new int[]{0};
static{
all = new int[]{0,1,3};
}
public static void main(String[] args) {
System.out.println(all.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 3

25.public class Manager {


static{
main(new String[]{"0"});
}
public static void main(String[] args) {
System.out.print(args.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 10 e. CS and prints 01 f. CS and
Gives AIOBE
26.public class Manager {
static void test(){
System.out.println(main(String[]{"12"}));
}
public static void main(String[] args) {
System.out.print(args.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 0f. CS and Gives
AIOBE

27.public class Manager {


static int counter = 0;
Manager(){
counter ++;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String[] args) {


new Manager();new Manager();new
Manager();
System.out.println(counter);
new Manager();new Manager();new
Manager();
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 3 e. CS and prints 0f. CS and prints 6
28.public class Manager {
static int counter = 0;
static Manager(){
counter ++;
}
public static void main(String[] args) {
new Manager();new Manager();new
Manager();
new Manager();new Manager();new
Manager();
System.out.println(counter);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 3 e. CS and prints 0f. CS and prints 6
29.public class Manager {
static final char c1;
static{
c1 = '1';
}
public static void main(String[] args) {
char c1 = 'a';
System.out.println(c1);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints af. CS and prints 46

30.public class Manager {


public static void main(String p[]) {
System.out.println(p.length);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 0f. CS and prints null
31.public class Manager {
static public void main(String main[]) {
System.out.println(main.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 0f. CS and prints null
32.public class Manager {
static void public main(String main[]) {
System.out.println(main.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 0f. CS and Gives
NSME
33.public class Manager {
public void main(String main[]) {
System.out.println(main.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 0f. CS and Gives
NSME
34.public class Manager {
static void main(String main[]) {
System.out.println(main.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives main method is not public d. CS and prints 1 e. CS
and prints 0 f. CS and Gives NSME
35.public class Manager {
protected static void main(String main[]) {
System.out.println(main.length);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

a. CTE b. CS and prints nothing. c. CS and


Gives main method is not public d. CS and prints 1 e. CS
and prints 0 f. CS and Gives NSME
36.public class Manager {
static private void main(String main[]) {
System.out.println(main.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives main method is not public d. CS and prints 1 e. CS
and prints 0 f. CS and Gives NSME
37.public class Manager {
public static void main(String ... args) {
System.out.println(args.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 0f. CS and Gives
NSME
38.public class Manager {
public static void main(String args) {
System.out.println(args.length());
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 0f. CS and Gives
NSME
39.public class Manager {
public static void main(String args) {
System.out.println(args.length());
}
public static void main(String []args) {
System.out.println(args.length);
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 1 e. CS and prints 0f. CS and Gives
NSME
40.public class Manager {
public static void main(String []args) {
System.out.println(“Manager”);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
class Test{
public static void main(String []args) {
System.out.println(“Test”);
}
}
a. It’s not possible to keep two classes with the main method in
the same java file.
b. CS and prints Manager, if we run Manager.class
c. CS and prints Test, if we run Test.class
d. CS and prints only Manager, if we run either Manager.class
or Test.class
e. None of the above
41.public class Manager {
public static void main(String []args) {
Test.main(args);
}
}
class Test{
public static void main(String []args) {
System.out.println(“Test”);
}
}
a. It’s not possible to keep two classes with the main method in
the same java file.
b. CS and prints Manager, if we run Manager.class
c. CS and prints Test, if we run Test.class
d. CS and prints only Manager, if we run either Manager.class
or Test.class
e. CS and prints only Test, if we run either Manager.class or
Test.class
f. None of the above
42.public class Manager {
public static void main(String []args) {
System.out.println("Manager");
}
}
class Test extends Manager{
public static void main(String []args) {
System.out.println("Test");
}
}
a. CS and prints Manager, if we run Manager.class
080-41310124 http://www.javaeasytoall.com
Lara Technologies

b. CS and prints Test, if we run Test.class


c. CS and prints only Manager, if we run either Manager.class
or Test.class
d. CS and prints only Test, if we run either Manager.class or
Test.class
e. CS and prints Manager and Test, if we run Test.class
f. None of the above
43.public class Manager {
public static void main(String []args) {
System.out.println("Manager");
}
}
class Test extends Manager{
public static void main(String args) {
System.out.println("Test");
}
}
a. CS and prints Manager, if we run Manager.class
b. CS and prints Test, if we run Test.class
c. CS and prints only Manager, if we run either Manager.class
or Test.class
d. CS and prints only Test, if we run either Manager.class or
Test.class
e. CS and prints Manager and Test, if we run Test.class
f. CS and prints only Manager, if we run either Manager.class
or Test.class
g. None of the above
44.public class Manager {
public static void main(String []args) {
System.out.print(args.length);
}
}
// D:\dev\classes>java Manager Lara Technologies
a. 0 b. 1 c. NPE d. 2
45.public class Manager {
public static void main(String []args) {
System.out.print(args.length);
}
}
// D:\dev\classes>java Manager “Lara Technologies”

a. 0 b. 1 c. NPE d. 2

080-41310124 http://www.javaeasytoall.com
Lara Technologies

46.public class Manager {


public static void main(String []args) {
System.out.print(args[1]);
}
}
// D:\dev\classes>java Manager Lara Technologies
a. 0 b. 1 c. NPE d. 2
e. None of the above

47.public class Manager {


public static void main(String []args) {
System.out.print(args[2]);
}
}
// D:\dev\classes>java Manager Lara Technologies
a. 0 b. 1 c. NPE d. 2
e. AIOBE f. None of the above
48.public class Manager {
public static void main(String []args) {
System.out.print(args.length());
}
}
// D:\dev\classes>java Manager
a. 0 b. 1 c. NPE d. 2
e. AIOBE f. None of the above
49.public class Manager {
public static void main(String ... args)
System.out.print(args.length);
}
}
// D:\dev\classes>java Manager Manager
a. 0 b. 1 c. NPE d. 2
e. AIOBE f. None of the above
50.public class Manager {
private Manager(){
System.out.println("private");
}
public static void main(String ...args ) {
new Manager();
}
}
a. CTE b. CS and prints private c. RTE
d. None of the above
080-41310124 http://www.javaeasytoall.com
Lara Technologies

51.public class Manager {


private Manager(){
System.out.println("pc");
}
private void Manager(){
System.out.println("pm");
}
public static void main(String ...args ) {
new Manager();
}
}
a. CTE b. CS and prints pc c. CS and prints pm

d. None of the above

52.public class Manager {


private Manager(String msg){
System.out.println(msg);
}
private void Manager(){
System.out.println("pm");
}
public static void main(String ...args ) {
new Manager();
}
}
a. CTE b. CS and prints msg c. CS and prints pm

d. None of the above

53.public class Manager {


private Manager(){
System.out.println("private");
return;
}
public static void main(String ...args ) {
new Manager();
}
}
a. CTE b. CS and prints private c. CS and
prints nothing d. None of the above

54.public class Manager {


080-41310124 http://www.javaeasytoall.com
Lara Technologies

private Manager(int i){


System.out.println("i");
}
Manager(int j){
System.out.println("j");
}
public static void main(String ...args ) {
new Manager(10);
}
}
a. CTE b. CS and prints i c. CS and prints j d. None
of the above
55.class Test{
String msg;
Test(){
System.out.println(msg.length());
}
}
public class Manager extends Test{
Manager(String msg){
this.msg = msg;
}
public static void main(String ...args ) {
new Manager("hello");
}
}
a. CTE b. CS and prints 5
c. CS and prints nothing d. CS and produces NPE
56.class Test{
String msg;
Test(String msg){
this.msg = msg;
}
}
public class Manager extends Test{
Manager(String msg){
this.msg = msg;
System.out.println(msg.length());
}
public static void main(String ...args ) {
new Manager("hello");
}
}
a. CTE b. CS and prints 5
080-41310124 http://www.javaeasytoall.com
Lara Technologies

c. CS and prints nothing d. CS and produces NPE


57.public class Manager{
Manager(){
System.out.print(1);
}
Manager(int i){
this();
System.out.print(2);
}
public static void main(String args[]){
new Manager(10);
}
}
a. CTE b. CS and prints 12
c. CS and prints 1 d. CS and prints 2
58.public class Manager{
Manager(){
this(10);
System.out.print(1);
}
Manager(int i){
this();
System.out.print(2);
}
public static void main(String args[]){
new Manager(10);
}
}
a. CTE b. CS and prints 1221
c. CS and prints 1122 d. CS and prints 2211

59.public class Manager{


Manager(){
this();
System.out.print(1);
}
public static void main(String args[]){
new Manager();
}
}
a. CTE b. CS and prints 1
c. CS and prints 11 d. CS and prints 1111

60.public class Manager{


080-41310124 http://www.javaeasytoall.com
Lara Technologies

Manager mgr;
Manager(){
System.out.print(mgr);
}
public static void main(String args[]){
new Manager();
}
}
a. CTE b. CS and prints mgr
c. CS and prints null d. CS and gives NPE
e. CS and gives Stack Overflow
61.public class Manager{
Manager mgr = new Manager();
Manager(){
System.out.print(1);
}
public static void main(String args[]){
new Manager();
}
}
a. CTE b. CS and prints mgr
c. CS and prints null d. CS and gives NPE
e. CS and gives Stack Overflow
62.public class Manager{
int i;
Manager mgr;
public static void main(String args[]){
System.out.println(mgr.i);
}
}
a. CTE b. CS and prints 0
c. CS and prints null d. CS and gives NPE
e. CS and gives Stack Overflow
63.public class Manager{
int i;
public static void main(String args[]){
Manager mgr;
System.out.println(mgr.i);
}
}
a. CTE b. CS and prints 0
c. CS and prints null d. CS and gives NPE
e. CS and gives Stack Overflow
64.public class Manager{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

static Manager mgr = new Manager();


static int i = 90;
public static void main(String args[]){
System.out.println(mgr.i);
}
}
a. CTE b. CS and prints 0
c. CS and prints null d. CS and gives NPE
e. CS and gives Stack Overflow
65.public class Manager{
final int i;
{
i = 90;
}
static Manager mgr = new Manager();
public static void main(String args[]){
System.out.println(mgr.i);
}
}
a. CTE b. CS and prints 90
c. CS and prints 0 d. CS and gives NPE
e. CS and gives Stack Overflow
66.public class Manager{
final int i = 9;
{
i = 90;
}
static Manager mgr = new Manager();
public static void main(String args[]){
System.out.println(mgr.i);
}
}
a. CTE b. CS and prints 90
c. CS and prints 9 d. CS and gives NPE
e. CS and gives Stack Overflow
67.public class Manager{
final int i;
Manager(){
i = 90;
}
static Manager mgr = new Manager();
public static void main(String args[]){
System.out.println(mgr.i);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
a. CTE b. CS and prints 90
c. CS and prints 0 d. CS and gives NPE
e. CS and gives Stack Overflow
68.public class Manager{
final int i = 9;
Manager(){
i = 90;
}
static Manager mgr = new Manager();
public static void main(String args[]){
System.out.println(mgr.i);
}
}
a. CTE b. CS and prints 90
c. CS and prints 9 d. CS and gives NPE
e. CS and gives Stack Overflow
69.public class Manager{
final int i;
static Manager mgr = new Manager();
public static void main(String args[]){
System.out.println(mgr.i);
}
}
a. CTE b. CS and prints 90
c. CS and prints 0 d. CS and gives NPE
e. CS and gives Stack Overflow
70.public class Manager{
static int i;
static{
i = 9;
}
{
i ++;
}
Manager(){
i++;
}
static Manager mgr = new Manager();
public static void main(String args[]){
new Manager();
System.out.println(mgr.i);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

a. CTE b. CS and prints 9


c. CS and prints 10 d. CS and gives NPE
e. CS and prints 11 f. CS and prints 12
g. e. CS and prints 13 g. none of the above
71.public class Manager{
int i;
{
i = 9;
}
{
i ++;
}
Manager(){
i++;
}
static Manager mgr = new Manager();
public static void main(String args[]){
new Manager();
System.out.println(mgr.i);
}
}
a. CTE b. CS and prints 9
c. CS and prints 10 d. CS and gives NPE
e. CS and prints 11 f. CS and prints 12
g. e. CS and prints 13 g. none of the above

72.class Test{
int i = 10;
{
i++;
}
Test(){
--i;
}
}
public class Manager extends Test{
Manager(){
i--;
}
{
++i;
}
public static void main(String args[]){
System.out.println(new Manager().i);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
a. CTE b. CS and prints 9
c. CS and prints 10 d. CS and gives NPE
e. CS and prints 11 f. CS and prints 12
g. e. CS and prints 13 g. none of the above

73.class Test{
static int i = 10;
static {
i++;
}
{
i ++;
}
Test(){
--i;
}
}
public class Manager extends Test{
Manager(){
i--;
}
static{
i++;
}
{
++i;
}
public static void main(String args[]){
System.out.println(new Manager().i);
}
}
a. CTE b. CS and prints 9
c. CS and prints 10 d. CS and gives NPE
e. CS and prints 11 f. CS and prints 12
g. e. CS and prints 13 g. none of the above
74.class Test{
static {
System.out.print(1);
}
{
System.out.print(2);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

Test(){
System.out.print(3);
}
}
public class Manager extends Test{
Manager(){
System.out.print(4);
}
static{
System.out.print(5);
}
{
System.out.print(6);
}
public static void main(String args[]){
new Manager();
}
}
a. CTE. b. CS and prints 152364
c. CS and prints 123456 d. CS and prints 132465
d. none of the above
75.class Test{
Test(){
System.out.println("1");
}
}
public class Manager extends Test{
Manager(){
System.out.println("0");
super();
}
public static void main(String args[]){
new Manager();
}
}
a. CTE. b. CS and prints 01
c. CS and prints 10 d. RTE
e. none of the above
76.public class Manager{
static void test(){
if(true){
return;
}
else{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

return;
}
System.out.println("the end");

}
public static void main(String args[]){
test();
}
}
a. CTE b. RTE c. CS and prints the end
d. none of the above
77.public class Manager{
static void test(){
if(true){
return;
}
else{
return;
System.out.println("the end");

}
}
public static void main(String args[]){
test();
}
}
a. CTE b. RTE c. CS and prints the end
d. none of the above
78.public class Manager{
static void test(int i){
System.out.println("int");
}
static void test(byte i){
System.out.println("byte");
}
public static void main(String args[]){
test(20);
}
}
a. CTE b. RTE c. CS and prints int
d. CS and prints nothing e. CS and prints byte

79.public class Manager{


static void test(int i){
080-41310124 http://www.javaeasytoall.com
Lara Technologies

System.out.println("int");
}
static void test(short i){
System.out.println("short");
}
public static void main(String args[]){
byte b = 25;
test(b);
}
}
a. CTE b. RTE c. CS and prints int
d. CS and prints nothing e. CS and prints short

80.public class Manager{


static void test(int i){
System.out.println("int");
}
static void test(double i){
System.out.println("double");
}
public static void main(String args[]){
test(20.0);
}
}
a. CTE b. RTE c. CS and prints int
d. CS and prints nothing e. CS and prints double

81.public class Manager{


static void test(byte i){
System.out.println("byte");
}
public static void main(String args[]){
test(20);
}
}
a. CTE b. RTE c. CS and prints int
d. CS and prints nothing e. CS and prints byte

82.public class Manager{


static void test(double i){
System.out.println("double");
}
static void test(byte i){
System.out.println("byte");
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
public static void main(String args[]){
test(20);
}
}
a. CTE b. RTE c. CS and prints double
d. CS and prints nothing e. CS and prints byte

83.public class Manager{


static void test(int i){
System.out.println("int");
}
static void test(byte i){
System.out.println("byte");
}
public static void main(String args[]){
test(20f);
}
}
a. CTE b. RTE c. CS and prints int
d. CS and prints nothing e. CS and prints byte

84.public class Manager{


static void test(double i){
System.out.println("double");
}
static void test(float i){
System.out.println("float");
}
public static void main(String args[]){
test(20f);
}
}
a. CTE b. RTE c. CS and prints double
d. CS and prints nothing e. CS and prints float

85.public class Manager{


static void test(byte i){
System.out.println("byte");
}
public static void main(String args[]){
test((byte)(double)(int) 20f);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

a. CTE b. RTE c. CS and prints int


d. CS and prints nothing e. CS and prints byte

86.class A{
static void test(){
System.out.println("A");
}
}
public class Manager extends A{
static void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
87.class A{
void test(){
System.out.println("A");
}
}
public class Manager extends A{
static void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
88.class A{
static void test(){
System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String args[]){


A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
89.class A{
void test(){
System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
90.class A{
void test(){
System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
((Manager)a1).test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
91.class A{
void test(){
System.out.println("A");
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public class Manager extends A{


void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
((A)a1).test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
92.class A{
void test(){
System.out.println("A");
}
}
public class Manager extends A{
private void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
93.class A{
public void test(){
System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
94.class A{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

private void test(){


System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
95.class A{
private void test(){
System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
96.class A{
void test(){
System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
97.class A{
private final void test(){
System.out.println("A");
}
}
public class Manager extends A{
public void test(){
System.out.println("Manager");
}
public static void main(String args[]){
Manager mgr = new Manager();
mgr.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager

98.abstract class A{
private abstract void test();
}
public class Manager extends A{
public void test(){
System.out.println("Manager");
}
public static void main(String args[]){
Manager mgr = new Manager();
mgr.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
99. class A{}
class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = null;
B b1 = null;
a1 = b1;
}
}
a. CTE b. RTE c. CS and prints A
080-41310124 http://www.javaeasytoall.com
Lara Technologies

d. CS and prints nothing e. CS and prints B

100. class A{}


class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = null;
B b1 = null;
b1 = a1;
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
101. class A{}
class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = null;
B b1 = null;
b1 = (B) a1;
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
102. class A{}
class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = null;
B b1 = null;
b1 = (A) a1;
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B

103. class A{}


class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = null;
B b1 = null;
a1 = (B) b1;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B

104. class A{}


class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = null;
B b1 = null;
a1 = (A) b1;
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
105. class A{}
class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = null;
B b1 = null;
b1 = (B)(B)(A)(B)(A) a1;
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B

106. class A{}


class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = new A();
B b1 = null;
b1 = (B) a1;
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
107. class A{}
class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = new B();
B b1 = null;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

b1 = (B) a1;
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
108. class A{}
class B extends A{}
public class Manager extends A{
public static void main(String args[]){
A a1 = new B();
B b1 = null;
b1 = (A)(B) a1;
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
109. class A{}
class B extends A{}
class Test{
void test(B b1){
System.out.println("B");
}
void test(A a1){
System.out.println("A");
}
}
public class Manager{
public static void main(String args[]){
new Test().test(new A());
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
110.class A{}
class B extends A{}
class Test{
void test(B b1){
System.out.println("B");
}
void test(A a1){
System.out.println("A");
}
}
public class Manager{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String args[]){


new Test().test((B)new A());
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
111.class A{}
class B extends A{}
class Test{
void test(B b1){
System.out.println("B");
}
}
public class Manager{
public static void main(String args[]){
new Test().test(new A());
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
112.class A{}
class B extends A{}
class Test{
void test(A a1){
System.out.println("A");
}
}
public class Manager{
public static void main(String args[]){
new Test().test((B)new A());
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
113.class A{}
class B extends A{}
class Test{
void test(A a1){
System.out.println("A");
}
}
public class Manager{
public static void main(String args[]){
new Test().test(new B());
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
114.class A{
int i = 9;
}
class B extends A{
int i = 19;
}
public class Manager{
public static void main(String args[]){
A a1 = new B();
System.out.println(a1.i);
}
}
a. CTE b. RTE c. CS and prints 19
d. CS and prints nothing e. CS and prints 19

115.class A{
int i = 9;
}
class B extends A{
int i = 19;
}
public class Manager{
public static void main(String args[]){
A a1 = new B();
System.out.println((B)a1.i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 19
116.class A{
int i = 9;
}
class B extends A{
int i = 19;
}
public class Manager{
public static void main(String args[]){
A a1 = new B();
System.out.println(((B)a1).i);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 19
117.class A{
int i = 9;
}
class B extends A{
int i = 19;
}
public class Manager{
public static void main(String args[]){
A a1 = new A();
System.out.println((B)a1.i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 19
118.class A{
int i = 9;
}
class B extends A{
int i = 19;
}
class C extends B{
int i = 29;
}
public class Manager extends C{
public static void main(String args[]){
A a1 = new Manager();
System.out.println(((C)a1).i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 29
f. CS and prints 19
119.class A{
int i = 9;
}
class B extends A{
int i = 19;
}
class C extends B{
int i = 29;
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public class Manager extends C{


public static void main(String args[]){
A a1 = new Manager();
System.out.println(((Manager)a1).i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 29
f. CS and prints 19
120. class A{
int i = 9;
}
class B extends A{
int i = 19;
}
class C extends B{
int i = 29;
}
public class Manager extends C{
public static void main(String args[]){
Manager a1 = new Manager();
System.out.println(a1.i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 29
f. CS and prints 19
121. class A{
int i = 9;
}
class B extends A{
int i = 19;
}
class C extends B{
int i = 29;
}
public class Manager extends C{
public static void main(String args[]){
Manager a1 = new Manager();
System.out.println(((B)a1).i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 29
080-41310124 http://www.javaeasytoall.com
Lara Technologies

f. CS and prints 19
122. class A{
static abstract void test(){
System.out.println("A");
}
}
public class Manager{
public static void main(String args[]){
System.out.println("Manager");
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager

123. abstract class A{


static void test(){
System.out.println("A");
}
}
public class Manager{
public static void main(String args[]){
System.out.println(A.test());
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
124. abstract class A{
static void test(){
System.out.println("A");
}
}
public class Manager{
public static void main(String args[]){
A.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
125. abstract class A{
void test();
}
public class Manager {
public static void main(String args[]){
System.out.println(“Manager”);
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
126. abstract class A{
abstract void test();
}
public class Manager extends A{
public static void main(String args[]){
System.out.println(“Manager”);
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
127. public abstract class Manager {
public static void main(String args[]){
System.out.println(“Manager”);
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager

128. abstract class A{


void test(){
System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager

129. abstract class A{


void test(){
System.out.println("A");
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public class Manager extends A{


void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new A();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
130. abstract class A{
void test(){
System.out.println("A");
}
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
((A)a1).test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
131. final abstract class A{
void test(){
System.out.println("A");
}
}
public class Manager{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager

080-41310124 http://www.javaeasytoall.com
Lara Technologies

132. abstract interface A{


void test();
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager

133. interface A{
void test();
}
public class Manager extends A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
134. interface A{
void test();
}
public class Manager implements A{
void test(){
System.out.println("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
135. interface A{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

void test();
}
public class Manager implements A{
public void test(){
System.out.println("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
136. interface A{
void test();
}
public class Manager implements A{
public void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
137. interface A{
void test();
}
public class Manager implements A{
public void test(){
System.out.println("Manager");
}
public static void main(String args[]){
A a1 = new A();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
138. interface A{
void test();
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public class Manager implements A{


protected void test(){
System.out.println("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
a1.test();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
139. interface A{
int i;
void test();
}
public class Manager implements A{
public void test(){
System.out.println("Manager");
}
public static void main(String args[]){
System.out.println(A.i);
}
}
a. CTE b. RTE c. CS and prints 0
d. CS and prints nothing e. CS and prints Manager
140. interface A{
int i = 0;
void test();
}
public class Manager implements A{
public void test(){
System.out.println("Manager");
}
public static void main(String args[]){
System.out.println(A.i);
}
}
a. CTE b. RTE c. CS and prints 0
d. CS and prints nothing e. CS and prints Manager
141. interface A{
int i = 9;
}
interface B extends A{
int i = 19;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
interface C extends A, B{
int i = 29;
}

public class Manager implements A, B, C{


int i = 39;
public static void main(String args[]){
A a1 = new Manager();
System.out.println(a1.i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints 19 e. CS and prints 29
f. CS and prints 39

142. interface A{
int i = 9;
}
interface B extends A{
int i = 19;
}
interface C extends A, B{
int i = 29;
}

public class Manager implements A, B, C{


int i = 39;
public static void main(String args[]){
C a1 = new Manager();
System.out.println(a1.i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints 19 e. CS and prints 29
f. CS and prints 39
143. interface A{
int i = 9;
}
interface B extends A{
int i = 19;
}
interface C extends A, B{
int i = 29;
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public class Manager implements A, B, C{


int i = 39;
public static void main(String args[]){
A a1 = new Manager();
System.out.println((C)a1).i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints 19 e. CS and prints 29
f. CS and prints 39
144. interface A{
int i = 9;
}
interface B extends A{
int i = 19;
}
interface C extends A, B{
int i = 29;
}

public class Manager implements A, B, C{


int i = 39;
public static void main(String args[]){
A a1 = new Manager();
System.out.println(((B)(C)(D)
(Manager)a1).i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints 19 e. CS and prints 29
f. CS and prints 39
145. interface A{
int i = 9;
}
interface B extends A{
int i = 19;
}
interface C extends A, B{
int i = 29;
}

public class Manager implements A, B, C{


080-41310124 http://www.javaeasytoall.com
Lara Technologies

int i = 39;
public static void main(String args[]){
A a1 = new C();
System.out.println(((B)(C)(D)
(Manager)a1).i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints 19 e. CS and prints 29
f. CS and prints 39
146. public class Manager{
public static void main(String args[]){
//insert here
System.out.println(i);
}
}
which options should be inserted independently to
produce 0 output.
a. int i = 0; b. byte i = 0; c. short i = 0; d. long i = 0;
e . int i; f. byte g; c. short i; h. long i;
147. If two classes are there in the same file. Is it possible to
declare both classes as a public?
a. yes b. no c. that depends d. some times
ok
148. If two classes are there as non public, then which class
name should be given to file?
a. first class name b. second class name c. both
class names.
d. any name
149. Is it possible to give second class names as a file name, if
file contains 100 non public classes.
a. yes b. no c. that depends d. some times
ok
150. If 100 classes are there in a file and 99 classes are not
having compile time errors. All are independent classes. Any one
class is not using other classes. How many class files will be
created at the time of compilation?
a. 1 b.100 c. 99 d. 0 e. none of the above
151. In order to design 100 public classes, how many
minimum number of files should be developed?
a. 1 b.100 c. 99 d. 101 e. none of the above
152. In order to design 100 non public classes, how many
minimum number of files should be developed?
a. 1 b.100 c. 99 d. 101 e. none of the above
080-41310124 http://www.javaeasytoall.com
Lara Technologies

153. If a file is containing 100 classes, every class is using all


remaining classes. If one class is having compile time error.
Then how many class files will be created while compiling?
a. 1 b.100 c. 99 d. 0 e. none of the above
154. which option is true about local variables?
a. local variables will be initialized with default values. That
depends on data type of the local variable.
b. local variables will be initialized with default values.
c. Local variable can be used before initialization.
d. Local variable can’t be used before initialization
155. which option is true about attributes of a class?
a. Attributes will be used to find out state of the object.
b. Attributes will be used to store state of the object.
c. Attributes will be used to find out behavior of the object.
d. Attributes can de derived data type
e. Attributes can be self derived data type
156. which option is true about methods of a class?
a. method identifier can be a class identifier.
b. Methods can be recursive.
c. Methods should have a return type
d. Methods which are returning void can have optional
return statement before ending that method
157. which option is true about constructor of a class?
a. every class should have at least one constructor
b. constructors can be inherited to sub classes
c. constructors can be a static
d. constructors can be abstract
e. constructors can have a return type
f. constructor last statement can be a return statement.
158. which option is true about constructor of a class?
a. every constructor first statement should be only super()
b. every constructor of super class will be called while
creating an object
c. constructor can be called from any where by using
super() or this()
d. by default only one constructors will be called in the
same class while creating an object
159. public class Manager{

int i = 100;

public static void main(String args[]){


System.out.println(i);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
which option can be kept independently to compile and
run with an out put as 100
a. public b. static c. final d. none
160. public class Manager{
public static void main(String args[]){
int i = 0;
i = i++;
System.out.println(i);
}
}
a. CTE b. CS and prints 0 c. CS and prints 1
d. CS and prints 2

161. public class Manager{


public static void main(String args[]){
int i = 0;
i = ++i;
System.out.println(i);
}
}
a. CTE b. CS and prints 0 c. CS and prints 1
d. CS and prints 2
162. public class Manager{
public static void main(String args[]){
int i = 0;
i = ++i;
i = i++;
System.out.println(i);
}
}
a. CTE b. CS and prints 0 c. CS and prints 1
d. CS and prints 2
163. public class Manager{
public static void main(String args[]){
int i = 0;
i = ++ i;
i = i ++;
if(i = 2){
System.out.println(i);
}
else{
System.out.println(i++);
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

}
}
a. CTE b. CS and prints 0 c. CS and prints 1
d. CS and prints 2 e. CS and prints 3
164. public class Manager extends Object{
public static void main(String args[]){
new Manager();
}
}
a. CS b. CTE c. RTE d. none
165. public class Manager{
public static void main(String args[]){
new Manager().main(args);
}
}
a. CS b. CTE c. RTE d. none
166. public class Manager{
public static void main(String args[]){
int i = 0;
test(i++);
System.out.print(i);
}
static void test(int i){
System.out.print(i++);
}
}
a. CS and prints 1 b. CTE c. RTE
d. CS and prints 01 e. CS and prints 10
f. CS and prints 11 f. CS and prints 12
167. public class Manager{
final Manager mgr;
Manager(){
mgr = null;
}
public static void main(String args[]){
System.out.println(new Manager().mgr);
}
}
a. CTE b. RTE c. CS and prints nothing
d. CS and prints null
168. I have directory structure as follows

E:\lab class Hello


080-41310124 dev http://www.javaeasytoall.com
{
src
}
Hello.java
Lara Technologies

Hello.java
If I compile like
E:\lab\dev\src> javac Hello.java

Where compiled .class file will be kept?


a. src b.dev c. lab d. E
169. What could be the package for Hello in the above
program?
a. src b.dev c. lab d. E
170. public class Manager {
String s;
public static void main(String[] args) {
Manager m = new Manager();
System.out.println(m.s);
}
}
a. CTE b. RTE c. CS and prints null
d. CS and prints nothing
171. class Manager{
static int f1(int i) {
System.out.print(i + ",");
return 0;
}
public static void main (String[] args) {
int i = 0;
i = i++ + f1(i);
System.out.print(i);
}
}
a. CTE b. RTE c. CS and prints 0,0
d. CS and prints 0,1 e. CS and prints 1,1
172. public class Manager {
public static void main(String[] args) {
int i, j, k;
i = j = i = j = k = k = 9;
System.out.println(k = i/2);
}
}
a. CTE b. RTE c. CS and prints 4.5 d. CS
and prints 4

080-41310124 http://www.javaeasytoall.com
Lara Technologies

173. public class Manager {


static int i = 10;
static{i = 101;}
public static void main(String[] args) {
System.out.println(i = i/2);
}
}
a. CTE b. RTE c. CS and prints 50.5 d. CS
and prints 50
174. public class Manager {
public static void main(String[] args) {
int i = (byte)(float)(double)(char)10.000;
System.out.println(i);
}
}
a. CTE b. RTE c. CS and prints 10.000 d. CS
and prints 10
175. public class Manager {
static{
Manager.main(new String[0]);
}
public static void main(String[] args) {
System.out.print(args.length);
}
}
a. CTE b. RTE c. CS and prints 00 d. CS
and prints 10
176. public class Manager{
static double i = (float)10;
public static void main(String args[]){
System.out.println(i);
}
}
a. CTE b. RTE c. CS and prints 10.00 d. CS
and prints 10.0
177. public class Manager{
public static void main(String args[]){
long lon = 1257;
System.out.println(lon == 1257d);
}
}
a. CTE b. RTE c. CS and prints true d. CS
and prints false
080-41310124 http://www.javaeasytoall.com
Lara Technologies

178. public class Manager{


int i = 9;
public static void main(String args[]){
Manager m1, m2;
m1 = m2 = new Manager();
System.out.println(m1.i == m2.i);
}
}
a. CTE b. RTE c. CS and prints true d. CS
and prints false
179. class A{
A(){
this(this);
System.out.print(1);
}
A(A a1){
System.out.print(2);
}
}
public class Manager{
public static void main(String args[]){
new A();
}
}
a. CTE b. RTE c. CS and prints 12 d. none

180. public class Manager{


Manager(Manager mgr){
super();
System.out.println(1);
}
public static void main(String args[]){
new Manager(new Object());
}
}
a. CTE b. RTE c. CS and prints 1d. none
181. while installing JDK5.0, which options can be filtered?
a. Dev tools b. demos c. source code. e. public JRE
182. In the PATH env D:\abc\bin entry is there. I have opened
command window. I want to keep D:\JDK5.0\bin in the same.
What is the command I have to use.
a. echo PATH=%PATH%;D:\JDK\bin
b. set PATH=%PATH%;D:\JDK\bin
c update PATH=%PATH%;D:\JDK\bin d. none
080-41310124 http://www.javaeasytoall.com
Lara Technologies

183. How to open System properties without mouse?


a. alt +space+e+p b. window+r c. window +
pause
e. window + e f. window + m
184. which options are true?
a. static members can access any other members.
b. Non static members can access any other members.
c. static members can access only other non static members.
d. Static methods can access constructor through new
operator
185. which are the following are valid and industry standard
identifiers
a. creditCardNo // for attribute
b. findBalance //for method
c. CreditCard //for class
d. hdfcCard //for reference to object
e. my3rdAccountNo //for local variable
186. which are the following are valid
a. object reference to one class can refer to any object
b. object reference to one class can refer to object to that
class only
c. object reference to one class can refer to object to that
class or sub class object
d. object reference can refer to as many object to as you
wish at the same statement
e. object references can be static
f. object references can assign explicitly with null
187. which are the following are valid
1. object can be created without constructor
2. interfaces are providing access to external
world.
3. abstract class can be 100% abstract
4. concrete class can have abstractness.
5. abstract classes should not have constructors.

188. public class Manager{


public static void main(String args[]){
int i = 100 + 20;
i = i-- + 10;
System.out.println(i);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies

a. CTE b. RTE c. CS and prints 129 d. CS and


prints 130
189. src>javac –d ../main *.java
a. compiled classes will be kept in main folder which is
parallel to src. b. invocation error. c. class files
kept in the same directory. d. class files will be kept in
one directory up to the current dir e. none
190. public class Manager{
public static void main(String args[]){
int i = 100 + 01 + 00001;
i = ++i + 10;
System.out.println(i);
}
}
a. CTE b. RTE c. CS and prints 114 d. cs and
prints 113 e. CS and prints 115
191. class A{
String args[];
A(String args[]){
this.args = args;
System.out.println(args.length);
}
}
public class Manager extends A{
Manager(String args[]){
super(args);
main(args);
}
public static void main(String args[]){
new Manager(args);
}
}
a. CTE b. RTE c. CS and prints 0d. CS and
prints 00
192. class A{
static int i = 10;
{
i++;
}
A(){
-- i;
}
}
public class Manager extends A{
080-41310124 http://www.javaeasytoall.com
Lara Technologies

public static void main(String args[]){


new A();new A();new A();new A();new A();
System.out.println("Object count:" + (i -
10));
}
}
a. CTE b. RTE c. CS and prints Object count: 5
d. CS and prints Object count: 0. e. CS and prints Object
count: 10 f. CS and prints Object count: 20
193. Which are the correct options?
a. int can widen automatically into double
b. boolean can widen to double automatically
c. double can widen to float automatically
194. object can have multiple references from different places
of the stack.
a. true b. false c. no idea d. that
depends
195. derived attributes will get default value as
a. true b. false c. null d. empty e.
0

080-41310124 http://www.javaeasytoall.com

Você também pode gostar