Core Java Study Final
Core Java Study Final
Lara Technologies
SCJP practice
Questions
Volume-1
Become a Laraite
to become a
SCJPite
080-41310124 http://www.javaeasytoall.com
Lara Technologies
By
B.Ramesh
080-41310124 http://www.javaeasytoall.com
Lara Technologies
Core Java
Day 1
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
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?
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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.
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
080-41310124 http://www.javaeasytoall.com
Lara Technologies
SCJP Oriented
Question 1
Given the following Directory structure
bigProject
|- source
| |-Utils.java
|
|- classes
|-
080-41310124 http://www.javaeasytoall.com
Lara Technologies
080-41310124 http://www.javaeasytoall.com
Lara Technologies
Day4
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
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;
}
}
‘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
‘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
Execution Steps:
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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.
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();
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.
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.
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();
}
}
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.
Stack Heap
Object
age=24
weight=60
showDays() height=5.9
main()
age=23
per1 weight=65
per2 height=5.8
JRE
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
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.
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
}
}
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
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.
{
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.
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:
080-41310124 http://www.javaeasytoall.com
Lara Technologies
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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
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
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
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
080-41310124 http://www.javaeasytoall.com
Lara Technologies
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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
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.
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
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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;
}
}
Example 3:-
class Person
{
Person()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies
Example 4:-
class Person
{
Person per = new Person();
}
Output: StackOverFlow, Run time Error.
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
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.
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());
}
}
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.
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
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
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
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
{
}
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
{
}
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
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”.
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
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
}
}
Program 7:-
Class A extends Object
{
A()
{
Super();
System.out.println(“A”);
}
}
Class B extends A
{
B()
{
super(10);
System.out.println(“B”);
}
}
{
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();
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies
Program 9:-
class A
{
A()
{
this(10);
}
A(int i)
{
this();
}
}
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.
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)”);
}
}
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
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)
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();
}
}
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
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?
class B extends A
{
B()
{
System.out.println(A);
}
}
classs C extends B
{
C()
{
System.out.println(C);
}
}
System.out.println(A);
}
}
class B extends A
{
}
B()
{
System.out.println(B);
}
}
}
}
SCJP Oriented
Question 1
Given:
1. public class Plant {
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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
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
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
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
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
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.
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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);
}
}
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);
}
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.
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++;
}
}
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
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
{
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);
}
}
D d4=new D();
D d4=new D();
System.out.println (d5.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);
}
}
System.out.println (a1.i);
A a2= new A();
System.out.println (a2.i);
}
}
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?
{
A a1=new A();
A a2=new A(10);
A a3=new A(10,15);
A a4=new A();
System.out.println (A.counter);
}
}
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);
}
15.
class A
{
static int i=20;
static
{
A a1=new A();
a1.i=30;
System.out.println(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);
}
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);
}
}
{
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
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
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);
}
}
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
Initialization block
Static Initialization block
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();
}
}
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();
}
}
A a1=new A();
A a2=new A(10);
}
}
{
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);
}
{
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);
}
}
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);
}
}
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);
}
}
class Manager
{
static
{
System.out.println(“1”);
}
public static void main (String args[])
{
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();
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”);
}
}
{
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();
}
}
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(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();
}
}
{
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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
A.check();
System.out.println("--------");
a3.test();
}
static
{
System.out.println("begin");
}
}
class Manager1
{
public static void main(String args[])
{
A.check();
}
}
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();
}
}
}
}
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
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)
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
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
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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.
class Person
{
int test()
{
System.out.println(“test()”);
}
int test(int a)
{
System.out.println(“test(int)”);
}
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 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”
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.
}
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);
}
}
{
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");
}
}
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");
}
}
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");
}
}
{
System.out.println("4");
System.out.println("6");
}
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);
}
}
class Manager
{
static void test(int i)
{
System.out.println("int");
}
public static void main(String args[])
{
Manager.test((int)10.0);
}
}
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);
}
}
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");
}
}
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");
}
}
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");
}
}
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);
}
}
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);
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies
class A
{
public void m1()
{
m2();
}
public void m2()
{
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
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
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
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
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
Statements st2;
Statements st3;
_
_
_
Statements stn;
return 100;
}
return 2000;
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies
return 500;
}
}
class Manager
{
public static void main(String args[])
{
DebitAccount da = new DebitAccount ();
CreditAccount ca = new CreditAccount ();
Overriding:
It is a mechanism in which super class methods can be
redefined in sub classes according to their usage.
{
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.
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
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
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
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.
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.
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
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();
}
}
}
}
}
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();
}
}
}
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());
}
}
}
System.out.println(all[i].getBalance());
}
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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();
}
}
}
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);
}
}
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
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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
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
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
class A{ }
class B extends A{ }
class C extends B{ }
class Manager
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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);
}
}
class A
{
int i;
}
class B extends A
080-41310124 http://www.javaeasytoall.com
Lara Technologies
{
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);
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);
}
}
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;
}
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;
}
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
{
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);
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);
}
}
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);
}
}
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
{
{
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
{
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);
}
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);
}
{
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
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");
}
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");
}
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
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
}
}
Co-variants
class A{ }
class B extends A{ }
class C extends B{ }
class X
{
A test()
{
Statements1;
_
_
_
}
class Z extends Y
{
C test()
{
_Statements1;
_
_
_Statements n;
return null;
}
}
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{}
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.
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
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
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.
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
}
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;
}
}
Packages
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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{ }
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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
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();
}
}
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
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
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
src A.java
A.java class A
{
}
class B
{
}
X.java
X.java
class X
{
}
class B
{
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies
src
pack1
A.java
classes
pack1
A.class
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
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
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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.
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
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
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
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
abstract class A
{
void test1()
{
_
_
_
}
abstract void test2();
}
class B extends A
{
void test2()
{
_
_
_
}
}
abstract class A
{
void test1()
{
_
_
_
080-41310124 http://www.javaeasytoall.com
Lara Technologies
}
abstract void test2();
}
abstract class B extends A
{
void test3()
{
_
_
_
}
}
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();
}
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()
{
_
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:
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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();
}
45) interface A
{
int i;
void show();
080-41310124 http://www.javaeasytoall.com
Lara Technologies
void test();
}
interface B
{
void show1();
void test1();
}
46) interface A
{
void show();
}
class C implements A
{
void show()
{
System.out.println(“show() implemented”);
}
}
O/P: Compile time error.
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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();
}
48) interface A
{
void show();
}
class B {
void show()
080-41310124 http://www.javaeasytoall.com
Lara Technologies
{
System.out.println(“show() implemented”);
}
}
}
O/P: show() implemented
interface Test
{
int i = 10;
void test();
}
Or
interface Test
{
public int i =10;
abstract void test();
}
Interface Test
{
public int i = 10;
abstract void test();
080-41310124 http://www.javaeasytoall.com
Lara Technologies
}
class A implements Test
{
void test()
{
_
_
_
_
}
}
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’.
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
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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.
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.
Final variable
class A
{
final int i = 10;
void test()
{
080-41310124 http://www.javaeasytoall.com
Lara Technologies
i++;
}
}
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
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;
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;
}
}
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
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();
}
}
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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.
For Execution
dev2\classes>java com.lara.Manager
NoClassDefinitionFoundError: Person
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.
Final class
Final method
Final variable
Importance of classpath variable
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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
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.
jar cf filename.jar
Calculator.java
Calculator.class
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
calci-dev\classes>jar cf ../lib/calci.jar *
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.
src
com
lara
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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.
Classpath
Jar
Javadoc
Javap
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
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
}
}
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);
}
}
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
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
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
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
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
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
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
121.
A.java
E:\lab
private class Hello
dev {
src
}
A.java
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?
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
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
}
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
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;
080-41310124 http://www.javaeasytoall.com
Lara Technologies
Note:
1. CTE = Compile Time Error
2. RTE = Run Time Error
3. CS = Compilation Success
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
}
}
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
}
}
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
a. 0 b. 1 c. NPE d. 2
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
}
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
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
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
}
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
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
}
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
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
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
}
}
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
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
}
}
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
080-41310124 http://www.javaeasytoall.com
Lara Technologies
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
}
interface C extends A, B{
int i = 29;
}
142. interface A{
int i = 9;
}
interface B extends A{
int i = 19;
}
interface C extends A, B{
int i = 29;
}
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
int i = 100;
}
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
}
}
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
Hello.java
If I compile like
E:\lab\dev\src> javac Hello.java
080-41310124 http://www.javaeasytoall.com
Lara Technologies
080-41310124 http://www.javaeasytoall.com