SCJP Practice Questions Guide
SCJP Practice Questions Guide
Lara Technologies
SCJP practice
Questions
Volume-1
Become a Laraite
to become a
SCJPite
080-41310124 [Link]
Lara Technologies
By
[Link]
080-41310124 [Link]
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 [Link]
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?
[Link] 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 [Link]?
45. For what [Link] files are used?
46. What does [Link] file contain?
47. What is src?
48. What is wrar?
080-41310124 [Link]
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 [Link]
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
[Link] 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 [Link]
Lara Technologies
080-41310124 [Link]
Lara Technologies
SCJP Oriented
Question 1
Given the following Directory structure
bigProject
|- source
| |-[Link]
|
|- classes
|-
080-41310124 [Link]
Lara Technologies
080-41310124 [Link]
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 [Link]
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 [Link]
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
[Link] = 29; //step4
[Link] = 59.08; //step5
[Link] = 6.0; //step6
[Link] = 18; //step7
[Link] = 48.05; //step8
[Link] = 5.0; //step9
[Link] = 29; //step10
[Link] = 59.08; //step11
[Link] = 6.0; //step12
Double rate = [Link](); //step13
[Link](“P1 runrate: ”+rate); //step14
rate = [Link](); //step15
[Link](“P2 runrate: ”+rate); //step16
rate = [Link](); //step17
[Link](“P3 runrate: ”+rate); //step18
}
080-41310124 [Link]
Lara Technologies
Execution Steps:
080-41310124 [Link]
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 [Link]
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()
{
[Link](“Hello World”);
}//End of show()
} //End of class A
080-41310124 [Link]
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 [Link]
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()
{
[Link](“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 [Link]=24;
4 [Link]=23;
5 [Link]=60;
6 [Link]=65;
7 [Link]=5.9;
8 [Link]=5.8;
9 [Link]();
10 [Link]();
}
}
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()
{
[Link](“You are “ +age*365+ “days old”);
}
}
class PersonManager
{
080-41310124 [Link]
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 [Link]
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?
[Link] the objects are kept?
[Link] are the two types of memory?
[Link] the internals files of java command are kept?
[Link] is the use of new keyword?
[Link] is primitive data type?
[Link] is derived data type?
[Link] is reference variable?
[Link] is live object?
080-41310124 [Link]
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. [Link] = cardlD;
30. [Link] = ownerName;
31. [Link] = 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 [Link]
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.
{
[Link](“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 [Link]
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 [Link]
Lara Technologies
080-41310124 [Link]
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[])
{
[Link](“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 [Link]
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 [Link]
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
[Link](“main method”);
test();
}
public static void test() //called method
{
[Link](“test method”)
}
}
Output of Example 6:
C:\Java\jdk1.5.0_05\bin>javac [Link]
C:\Java\jdk1.5.0_05\bin>java Test
test method
main method
test method
Control Flow
if-else
080-41310124 [Link]
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 [Link]
Lara Technologies
080-41310124 [Link]
Lara Technologies
SCJP Oriented
080-41310124 [Link]
Lara Technologies
Question 2
Given:
10. public class Bar {
[Link] 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) [Link](z);
B. for(int z : x) [Link](z);
C. while( [Link]()) [Link]( [Link]());
D. for( int i=0; i< [Link]; i++ ) [Link](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.
[Link]((x==4) && !b2)
18. [Link](”l “);
19. [Link](”2 “);
20. if ((b2 = true) && b1)
21. [Link](”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 [Link]
Lara Technologies
Question 5
Given:
[Link] x=0;
[Link] y 10;
12. do {
l3. y--;
14. ++x;
15. } while (x < 5);
16. [Link](x + “,“ + y);
What is the result?
A. 5,6
B. 5,5
C. 6,5
D. 6,6
Answer: B
Question 6
Given:
[Link]=12;
26. while (x < 10) {
27. x--;
28. }
29. [Link](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 [Link]
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. [Link](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. [Link](”Inconceivable!”);
24. } else if(c instanceof Cup) {
25. [Link](”Dizzying intellect!”);
26. } else {
27. [Link](0);
28. }
29. }
And the execution of the statements:
Cup cup = new PoisonCup();
takeCup(cup);
What is the output?
080-41310124 [Link]
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. [Link](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. }
[Link](o);
11.}
12.}
080-41310124 [Link]
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.}
[Link](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 [Link]
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 [Link]
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 [Link]([Link]); // will print 30
4 [Link]([Link]); // will print 20
}
}
080-41310124 [Link]
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 [Link]
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 [Link]
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();
[Link] = 29;
[Link] = 59.08;
[Link] = 5.8
double rate = [Link]();
[Link](rate);
Person p2 = new Person(25,60.0,5.8);
rate = [Link]();
[Link](rate);
}//end of main method
}//end of class
080-41310124 [Link]
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 = [Link]();
[Link](i);
[Link](20);
i = [Link]();
[Link](i);
[Link](30);
i = [Link]();
[Link](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;
[Link]([Link]());
[Link]([Link]());
B b3 = new B(20, 30);
B b4 = b3;
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
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. [Link]([Link]);
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 [Link]
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 [Link]
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. [Link] = name;
6. [Link] = 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. [Link] = commission;
B. superb();
commission = commission;
C. [Link] = commission;
superb();
D. super(name, baseSalary);
080-41310124 [Link]
Lara Technologies
[Link] = commission;
E. super();
[Link] = commission;
F. [Link] = 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 [Link](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. [Link](1024);
26. changePayload(p);
27. [Link](”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. [Link](420);
B. [Link](420);
C. p = new Payload(420);
D. [Link](420);
E. p = [Link](420);
F. p = new Payload();
[Link](420);
Answer: A
Question 7
Click the Exhibit button.
10. class Foo {
11. private int x;
[Link](intx) {this.x=x; }
080-41310124 [Link]
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 [Link]
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 [Link]
Lara Technologies
class Manager
{
public static void main(String args[])
{
C c = new C ();
}
}
080-41310124 [Link]
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() {
[Link](“House Num:” +houseNum);
[Link](“Street Num:” + streetNum);
[Link](“Pin Code:” + pinCode);
[Link](“Mobile Num:” + mobileNum);
}
}
class AddressManager
{
080-41310124 [Link]
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();
[Link](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 [Link]
Lara Technologies
}
class PersonManager extends Person
{
public static void main(String args[])
{
PersonManager p1 = new PersonManager();
[Link](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();
[Link](“A”);
}
}
Class B extends A
{
B()
{
Super();
[Link](“B”);
080-41310124 [Link]
Lara Technologies
}
}
Program 7:-
Class A extends Object
{
A()
{
Super();
[Link](“A”);
}
}
Class B extends A
{
B()
{
super(10);
[Link](“B”);
}
}
{
super(10);// to put this code
[Link](“B”);
}
}
Option – 2:-
Class A extends Object
{
A()// remove the parameter
{
Super();
[Link](“A”);
}
}
class B extends A
{
B()
{
Super();
[Link](“B”);
}
}
Program 8:-
class A extends Object
{
A(int i, int j)
{
Super();
[Link](“A”);
}
}
class B extends A
{
B()
{
Super();
}
}
080-41310124 [Link]
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 [Link]
Lara Technologies
[Link](“A”);
super();
}
}
Program 12:-
class A
{
A()
{
[Link](“A”);
this(10);
}
A(int i)
{
[Link](“A (int)”);
}
}
Constructor Chain:
Program 13:-
class A
{
A()
{
//super();
[Link](“A()”);
}
}
class B extends A
{
int i;
B()
{
//super();
[Link](“B()”);
080-41310124 [Link]
Lara Technologies
}
}
class Manager
{
public static void main(String args[])
{
B b1 = new B();
[Link](“End of object creation”);
}
}
Output:
A()
B()
End of object creation
Program 14:-
class A
{
A(int a)
{
[Link](“A(int)”);
}
}
class B extends A
{
int i;
B()
{
super(10);
[Link](“B()”);
}
080-41310124 [Link]
Lara Technologies
B(int a)
{
this();
[Link](“B(int)”);
}
}
class Manager
{
public static void main(String args[])
{
B b1 = new B(10);
[Link](“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 [Link]
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 [Link]
Lara Technologies
8. What is stereotype?
9. Super class are called specialized class(T/F).
[Link] class are called generalized class(T/F).
[Link] class are called generalized class(T/F).
[Link] class are called specialized class(T/F).
[Link] java whether it is possible develop one sub class from many
super class?
[Link] will be inherited to sub class.(T/F)
[Link] multiple inheritance is supported by java or not?
[Link] inherited to the sub class(T/F).
[Link] do you mean by object class?
[Link] object class is super class or it’s a sub class?
class B extends A
{
B()
{
[Link](A);
}
}
classs C extends B
{
C()
{
[Link](C);
}
}
[Link](A);
}
}
class B extends A
{
}
B()
{
[Link](B);
}
}
}
}
SCJP Oriented
Question 1
Given:
1. public class Plant {
080-41310124 [Link]
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 [Link]; }
26. }
27.
28. class Euro extends Money {
29. public String getCountry(String timeZone) {
30. return [Link]();
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 [Link]
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 [Link]() { /*do something*/ }
080-41310124 [Link]
Lara Technologies
}
D. public class Employee implements Info extends Data {
public void [Link]() { /*d something */ }
public void load() { /*do something */ }
}
E. public class Employee implements Info extends Data {
public void load() { /*do something */ }
public void [Link](){ /*do something*/ }
}
F. public class Employee extends Info implements Data{
public void [Link]() { /*do something*/ }
public void [Link]() { /*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 [Link]
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(); [Link]();
4. Goban b = new Goban(); [Link]();
5. Stone c = new Stone(); [Link]();
6. }
7. }
8.
9. class Sente implements Go {
10. public void go() { [Link](”go in Sente.”); }
11. }
12.
13. class Goban extends Sente {
14. public void go() { [Link](”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 [Link]
Lara Technologies
Question 8
Given:
10. class One {
11. public One() { [Link](1); }
12. }
13. class Two extends One {
14. public Two() { [Link](2); }
15. }
16. class Three extends Two {
17. public Three() { [Link](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. [Link](”first “);
18. }
19.
20. public Bootchy(String snootch) {
21. this(420, “snootchy”);
22. [Link](”second “);
23. }
24.
25. public Bootchy(int bootch, String snootch) {
26. [Link] = bootch;
27. [Link] = snootch;
28. [Link](”third “);
29. }
30.
080-41310124 [Link]
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. [Link] = numberOfinstances;
5. }
6. }
7. public class ExtendedA extends ClassA {
8. private ExtendedA(int numberOfinstances) {
9. super(numberOflnstances);
080-41310124 [Link]
Lara Technologies
10. }
11. public static void main(String[] args) {
12. ExtendedA ext = new ExtendedA(420);
13. [Link]([Link]);
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. [Link] = vin;
6. [Link] = 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. [Link] = 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 [Link]
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 [Link]
Lara Technologies
Question 16
3. Given:
5. class Atom{
6. Atom(){[Link](”atom”);}
7. }
8. class Rock extends Atom {
9. Rock(String type){[Link](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 [Link]
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(){[Link](“c”);}}
7. class Meter extends Component {void go()
{[Link](“m”);}}
8.
9. class DeluxeThingy extends Thingy{
10. public static void main(String args[]){
11. DeluxeThingy dt = new DeluxeThingy();
12. [Link]();
13. Thingy t = new DeluxeThingy();
14. [Link]();
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 [Link]
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 [Link]
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;
[Link](b2.i);
}
}
Static members will not be allocated in object. Only non-static
members will be present in object.
080-41310124 [Link]
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();
[Link](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();
[Link](d5.i);
}
}
080-41310124 [Link]
Lara Technologies
Program 5:-
class A
{
static int i;
}
class Manager
{
public static void main(String args[])
{
A.i = 19;
A a2 = new A();
[Link](a2.i);
}
}
Program 6:-
class A
{
static void test()
{
[Link](“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();
[Link]();
[Link]();
[Link]();
}
}
080-41310124 [Link]
Lara Technologies
Program 7:-
class B
{
int i;
static int j;
B(int i)
{
this.i = i;
}
void test()
{
[Link](i);
[Link](j);
}
void test1()
{
[Link](j);
}
Program 8:-
class A
{
static int counter;
A()
{
counter++;
}
080-41310124 [Link]
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();
[Link]([Link]);
}
}
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 [Link]
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()
{
[Link](“Inside static method of A”);
}
}
class Manager
{
public static void main(String[] args)
080-41310124 [Link]
Lara Technologies
{
A o1 = new A();
A o2 = new A(10);
A o3 = new A(10,20);
[Link](“No of objects created:”+[Link]);
[Link]();
}
}
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------- [Link](i);
2------- [Link](A.j);
}
}
Non static members cannot be accessed from static context.
Compiler will generate error during compilation if we do [Link] 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 [Link]
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;
[Link] (c4.i);
}
}
D d4=new D();
D d4=new D();
[Link] (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();
[Link] (d5.i);
}
}
[Link] (a1.i);
A a2= new A();
[Link] (a2.i);
}
}
[Link](j);
}
void test1()
{
[Link] (j);
}
public static void main (String args[])
{
B a1=new B(15);
B a2=new B(20);
[Link]();
b1.test1();
[Link] ();
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();
[Link] ([Link]);
}
}
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 [Link]
Lara Technologies
[Link]("a1.i="+a1.i);
[Link]("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;
[Link]("a1.i="+a1.i);
[Link]("a2.i="+a2.i);
[Link]("a3.i="+a3.i);
[Link]("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;
[Link]("b2.i="+b2.i);
}
}
080-41310124 [Link]
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;
[Link]("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;
[Link]("a2.i="+a2.i);
[Link]("a3.i="+a3.i);
}
}
7.
class A
{
int i;
}
class Manager
080-41310124 [Link]
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;
[Link]("a1.i="+a1.i);
[Link]("a2.i="+a2.i);
[Link]("a3.i="+a3.i);
[Link]("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;
[Link]("a1.i="+a1.i);
[Link]("a2.i="+a2.i);
[Link]("a3.i="+a3.i);
[Link]("a4.i="+a4.i);
}
}
9.
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
080-41310124 [Link]
Lara Technologies
{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=23;
[Link]("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;
[Link]("b1.i="+b1.i);
[Link]("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 [Link]
Lara Technologies
[Link]("b1.i="+b1.i);
[Link]("b2.i="+b2.i);
[Link]("b3.i="+b3.i);
[Link]("b2.i="+b2.i);
}
}
14.
class A
{
static int i=10;
static
{
A a1=new A();
a1.i=10;
[Link](a1.i);
}
15.
class A
{
static int i=20;
static
{
A a1=new A();
a1.i=30;
[Link](a1.i);
}
16.
080-41310124 [Link]
Lara Technologies
class A
{
int i=20;
static
{
A a1=new A();
a1.i=30;
[Link](a1.i);
}
17.
class A
{
int i=20;
static
{
A a1=new A();
a1.i=0;
[Link](a1.i);
}
public static void main(String args[])
{
A a1=new A();
[Link]("a1.i="+a1.i);
}
}
{
A a1=new A();
A a2=new A();
A a3=new A();
A a4=new A();
a1.i=23;
[Link]("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;
[Link]("a1.i="+a1.i);
[Link]("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 [Link]
Lara Technologies
a2.i=34;
[Link]("a1.i="+a1.i);
[Link]("a2.i="+a2.i);
[Link]("a3.i="+a3.i);
[Link]("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;
[Link]("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;
[Link]("a2.i="+a2.i);
}
}
080-41310124 [Link]
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;
[Link]("a2.i="+a2.i);
[Link]("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;
[Link]("a1.i="+a1.i);
[Link]("a2.i="+a2.i);
[Link]("a3.i="+a3.i);
[Link]("a4.i="+a4.i);
}
}
8.
class A
{
int i;
080-41310124 [Link]
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;
[Link]("a1.i="+a1.i);
[Link]("a2.i="+a2.i);
[Link]("a3.i="+a3.i);
[Link]("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;
[Link]("b1.i="+b1.i);
}
}
10.
class B
{
static int i;
}
class Manager
{
public static void main(String args[])
080-41310124 [Link]
Lara Technologies
{
B b1=new B();
B b2=new B();
B b3=new B();
B b4=new B();
b1.i=23;
[Link]("b1.i="+b1.i);
[Link]("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;
[Link]("b1.i="+b1.i);
[Link]("b2.i="+b2.i);
[Link]("b3.i="+b3.i);
[Link]("b2.i="+b2.i);
}
}
12.
class A
{
static int i=10;
static
{
A a1=new A();
a1.i=10;
[Link](a1.i);
}
080-41310124 [Link]
Lara Technologies
13.
class A
{
static int i=20;
static
{
A a1=new A();
a1.i=30;
[Link](a1.i);
}
public static void main(String args[])
{
A a1=new A();
[Link]("a1.i="+a1.i);
}
}
14.
class A
{
int i=20;
static
{
A a1=new A();
a1.i=30;
[Link](a1.i);
}
public static void main(String args[])
{
A a1=new A();
[Link]("a1.i="+a1.i);
}
}
15.
class A
{
080-41310124 [Link]
Lara Technologies
int i=20;
static
{
A a1=new A();
a1.i=0;
[Link](a1.i);
}
public static void main(String args[])
{
A a1=new A();
[Link]("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. [Link]() is a valid invocation of beta().
B. [Link]() 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 [Link]
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 [Link]
Lara Technologies
Day 9
[Link] A
{
int i;
{
[Link](1);
}
{
[Link](2);
}
void test()
{
[Link](3);
}
A()
{
[Link](4);
}
{
[Link](5);
080-41310124 [Link]
Lara Technologies
}
}
class Manager
{
public static void main(String args[])
{
A a1 =new A();
A a2 =new A();
}
}
[Link] A
{
{
[Link](1);
}
A()
{
[Link](2);
}
{
[Link](3);
}
}
A(int i)
{
[Link](4);
}
{
[Link](5);
A(int i, int j)
{
this(1);
[Link](6);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
[Link](“A”);
A a1 = new A(10, 15);
080-41310124 [Link]
Lara Technologies
[Link](“B”);
A a1 = new A(10);
[Link](“C”);
}
}
[Link] A
{
{
[Link](1);
}
A()
{
super();
[Link](2);
}
{
[Link](3);
}
A(int i, int j)
{
this();
[Link](4);
}
A(int i)
{
super();
[Link](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);
}
}
[Link] A
{
080-41310124 [Link]
Lara Technologies
{
[Link](1);
}
A()
{
[Link](2);
}
{
[Link](3);
}
A(int i)
{
[Link](4);
}
A(int i, int j)
{
this(1);
[Link](5);
}
{
[Link](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
{
{
[Link](7);
}
B()
{
[Link](8);
}
B(int i)
{
[Link](9);
}
{
[Link](10);
}
B(int i, int j)
{
super();
080-41310124 [Link]
Lara Technologies
[Link](5);
}
B(double d)
{
super(10, 15);
[Link](12);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
[Link](“A1”);
B b1 = new B(10);
[Link](“B1”);
B b2 = new B();
[Link](“B2”);
A a2 = new A(10);
[Link](“A2”);
A a3 = new A(10, 15);
[Link](“A3”);
B b3 = new B(1, 2);
[Link](“B1”);
B b4 = new B(10.0);
}
}
class Manager
{
static
{
[Link](1);
}
public static void main(String args[])
{
[Link](2);
}
080-41310124 [Link]
Lara Technologies
class Manager
{
static
{
[Link](1);
}
public static void main(String args[])
{
[Link](2);
[Link]();
[Link](3);
}
static
{
[Link](4);
}
public void test()
{
[Link](5);
}
}
class A
{
static
{
[Link](1);
}
A()
{
[Link](2);
}
{
[Link](3);
}
{
[Link](4);
}
void test()
{
[Link](5);
}
080-41310124 [Link]
Lara Technologies
Initialization block
Static Initialization block
class A
{
int i;
{
[Link](“1”);
}
int j;
{
[Link](“2”);
}
void test()
080-41310124 [Link]
Lara Technologies
{
[Link](“3”);
}
A()
{
[Link](“4”);
}
{
[Link](“5”);
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A a2=new A();
}
}
class A
{
int i;
{
[Link](“1”);
}
int j;
{
[Link](“2”);
}
void test()
{
[Link](“3”);
}
A()
{
[Link](“4”);
}
080-41310124 [Link]
Lara Technologies
{
[Link](“5”);
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
}
}
A a1=new A();
A a2=new A(10);
}
}
{
[Link](“1”);
}
int j;
{
[Link](“2”);
}
void test()
{
[Link](“3”);
}
A()
{
[Link](“4”);
}
{
[Link](“5”);
}
A(int i)
{
[Link](“6”);
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
A a2=new A(12,15);
}
{
[Link](“4”);
}
{
[Link](“5”);
}
A(int i ,int j)
{
this(i);
[Link](“6”);
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
[Link](“__________”);
A a2=new A(10,15);
[Link](“__________”);
A a2=new A(10);
}
}
[Link](“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);
[Link](“5”);
}
{
[Link](“6”);
}
}
class B extends A
{
080-41310124 [Link]
Lara Technologies
[Link](“7”);
}
B()
{
[Link](“8”);
}
B(int i)
{
[Link](“9”);
}
{
[Link](“10”);
}
B(int i, int j)
{
[Link](“11”);
}
B(double d)
{
super();
[Link](“12”);
}
}
class Manager
{
public static void main (String args[])
{
A a1=new A();
[Link](“________”);
B b1=new B();
[Link](“________”);
B b2=new B(10);
[Link](“________”);
A a2=new A(10);
[Link](“________”);
A a3=new A(10,14);
[Link](“________”);
B b3=new B(11,20);
B b4=new B(10.5);
}
}
class Manager
{
static
{
[Link](“1”);
}
public static void main (String args[])
{
[Link](“2”);
}
}
}
{
[Link](“3”);
}
{
[Link](“4”);
}
void test()
{
[Link](“5”);
}
static void check()
{
[Link](“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
{
[Link](“begin”);
}
}
{
[Link](“3”);
}
{
[Link](“4”);
}
void test()
{
[Link](“5”);
}
static void check()
{
[Link](“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
{
[Link](“begin”);
}
}
class manager1
{
public static void main (String args[])
{
[Link]();
}
}
class Manager2
{
public static void main (String args[])
{
[Link](args);
080-41310124 [Link]
Lara Technologies
[Link](args);
}
}
[Link] the output for this program:
class A
{
int i;
{
[Link](1);
}
int j;
{
[Link](2);
}
void test()
{
[Link](3);
}
A()
{
[Link](4);
}
{
[Link](5);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A();
}
}
[Link](2);
}
void test()
{
[Link](3);
}
A()
{
{
[Link](4);
}
{
[Link](5);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
}
}
}
A(int i)
{
[Link](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;
{
[Link](1);
}
int j;
{
[Link](2);
}
void test()
{
[Link](3);
}
A()
{
[Link](4);
}
{
[Link](5);
}
A(int i)
{
[Link](6);
}
}
class Manager
{
public static void main(String[] args)
080-41310124 [Link]
Lara Technologies
{
A a1=new A();
}
}
{
[Link](1);
}
A()
{
[Link](2);
}
{
[Link](3);
}
A(int i)
{
[Link](4);
}
{
[Link](5);
}
A(int i,int j)
{
this(i);
[Link](6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]("-------");
A a2=new A(10,12);
[Link]("-------");
A a3=new A(10);
}
}
[Link](2);
}
{
[Link](3);
}
A(int i)
{
[Link](4);
}
{
[Link](5);
}
A(int i,int j)
{
this(i);
[Link](6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]("-------");
A a2=new A(10,12);
[Link]("-------");
A a3=new A(10);
}
}
20. Write the output for this program
class A
{
{
[Link](1);
}
A()
{
[Link](2);
}
{
[Link](3);
}
A(int i)
{
080-41310124 [Link]
Lara Technologies
[Link](4);
}
{
[Link](5);
}
A(int i,int j)
{
this(i);
[Link](6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]("-------");
A a2=new A(10,12);
[Link]("-------");
A a3=new A(10);
[Link]("-------");
A a4=new A(10);
}
}
21. Write the output for this program
class A
{
{
[Link](1);
}
A()
{
[Link](2);
}
{
[Link](3);
}
A(int i,int j)
{
this();
[Link](4);
}
A(int i)
{
080-41310124 [Link]
Lara Technologies
super();
[Link](5);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]("-------");
A a2=new A(10,12);
[Link]("-------");
A a3=new A(10);
[Link]("-------");
A a4=new A(10,12);
}
}
[Link](4);
}
A(int i,int j)
{
080-41310124 [Link]
Lara Technologies
this(i);
[Link](5);
}
{
[Link](6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]("-------");
A a2=new A(10,12);
[Link]("-------");
A a3=new A(10);
[Link]("-------");
A a4=new A(10,12);
[Link]("-------");
A a5=new A(10);
}
}
[Link](4);
}
A(int i,int j)
{
080-41310124 [Link]
Lara Technologies
this(i);
[Link](5);
}
{
[Link](6);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]("-------");
A a2=new A(10,12);
[Link]("-------");
A a3=new A(10);
[Link]("-------");
A a4=new A(10,12);
[Link]("-------");
A a5=new A(10);
}
}
[Link]("5");
}
{
[Link]("6");
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]("-------");
A a2=new A(10,12);
[Link]("-------");
A a3=new A(10);
[Link]("-------");
A a4=new A(10,12);
[Link]("-------");
A a5=new A(10);
}
}
[Link]("4");
}
A(int i,int j)
{
080-41310124 [Link]
Lara Technologies
this(i);
[Link]("5");
}
{
[Link]("6");
}
}
class B extends A
{
{
[Link](7);
}
B()
{
[Link](8);
}
B(int i)
{
[Link](9);
}
{
[Link](10);
}
B(int i,int j)
{
super(i);
[Link]("11");
}
B(double d)
{
super(10,17);
[Link]("12");
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]("-------");
B b1=new B(10);
[Link]("-------");
B b2=new B();
080-41310124 [Link]
Lara Technologies
[Link]("-------");
A a2=new A(12);
[Link]("-------");
A a3=new A(12,10);
[Link]("-------");
B b3=new B(1,2);
B b4=new B(10.3);
}
}
A()
{
[Link](2);
080-41310124 [Link]
Lara Technologies
}
{
[Link](3);
}
{
[Link](4);
}
void test()
{
[Link](5);
}
static void check()
{
[Link](6);
}
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
[Link]("--------");
[Link]();
[Link]("--------");
[Link]();
[Link]("--------");
A a2=new A();
[Link]("--------");
[Link]();
[Link]("--------");
[Link]();
[Link]("--------");
A a3=new A();
[Link]("--------");
[Link]();
[Link]("--------");
[Link]();
}
static
{
[Link]("begin");
}
}
080-41310124 [Link]
Lara Technologies
[Link]();
[Link]("--------");
[Link]();
}
static
{
[Link]("begin");
}
}
class Manager1
{
public static void main(String args[])
{
[Link]();
}
}
A()
{
[Link](2);
}
{
[Link](3);
}
{
[Link](4);
}
void test()
{
[Link](5);
}
static void check()
{
[Link](6);
}
080-41310124 [Link]
Lara Technologies
}
class Manager1
{
public static void main(String args[])
{
[Link]();
}
}
}
}
class Manager2
{
public static void main(String args[])
{
[Link](args);
[Link](args);
}
080-41310124 [Link]
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.
{
[Link](“IIB called “ + ++count + “time”);
}
}
080-41310124 [Link]
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);
[Link](“No of objects created: ”+[Link]);
}
}
Output:
IIB called 1time
IIB called 2time
IIB called 3time
No of objects created: 3
class A
{
A()
{
//super();
//calling statements to IIB’s
[Link](“A()”);
}
A(int i)
{
this();
[Link](“A(int)”);
}
{
[Link](“IIB-A”);
}
}
class Manager
{
public static void main(String args[])
080-41310124 [Link]
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
[Link] 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
[Link](“A()”);
}
{
[Link](“IIB-A”);
}
}
}
class B extends A
{
{
080-41310124 [Link]
Lara Technologies
[Link](“IIB-B”);
}
}
class Manager
{
public static void main(String args[])
{
B b1 = new B();
}
}
Output:
IIB-A
A()
IIB-B
class A
{
static
{
[Link](“Inside static block”);
}
public static void main(String args[])
{
[Link](“Hello World!”);
}
}
080-41310124 [Link]
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
{
[Link](“Inside static block of A”);
}
}
class B extends A
{
static
{
[Link](“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 [Link]
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()
{
[Link](“test()”);
}
int test(int a)
{
[Link](“test(int)”);
}
[Link]();
[Link](10);
[Link](10,20);
}
}
Output:
test()
test(int)
test(int,int)
class A
{
static
{
[Link](1);
}
A()
{
[Link](2);
}
{
[Link](3);
}
}
class B extends A
{
static
{
[Link](4);
}
B()
{
[Link](5);
}
{
[Link](6);
}
}
class C extends B
{
static
{
[Link](7);
}
080-41310124 [Link]
Lara Technologies
C()
{
[Link](8);
}
{
[Link](9);
}
}
class Manager
{
static
{
[Link](10);
}
public static void main(String args[])
{
C c1 =new C();
}
}
class Manager1
{
static
{
[Link](11);
}
public static void main(String args[])
{
A a1 =new A();
[Link](args);
}
}
class Manager
{
static
{
[Link](1);
}
public static void main(String args[])
{
[Link](2);
}
}
080-41310124 [Link]
Lara Technologies
class Test
{
static void test()
{
[Link](1);
}
static void test(int i)
{
[Link](2);
}
static void test(byte b)
{
[Link](3);
}
static void test(double d)
{
[Link](4);
}
}
class Manager
{
public static void main(String args[])
{
[Link](10);
[Link](10.0);
Byte b = 10;
[Link](b);
Float f1 = 10.0f;
[Link](b);
[Link](f1);
080-41310124 [Link]
Lara Technologies
Long l = 10l;
[Link](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)
{
[Link](“ int “);
}
public static void main(String args[])
{
[Link](10.0);
}
}
class Manager
{
public static void main(String args[])
{
080-41310124 [Link]
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)
{
[Link](“ float “);
}
}
class A
{
void test()
{
[Link](1);
}
void test(int i)
{
[Link](2);
}
void test(int i, double j)
{
[Link](3);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
[Link]();
byte b = 10;
[Link](b);
[Link](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 [Link]
Lara Technologies
class A
{
void test()
{
[Link](“hello”);
}
}
class B extends A
{
void test()
{
[Link](“hello”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
A a2 = new B();
B b1 = new A();
[Link]();
[Link]();
[Link]();
}
}
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
{
[Link](7);
}
C()
{
[Link](8);
}
{
[Link](9);
}
}
class Manager
{
static
{
[Link](10);
}
public static void main(String[] args)
{
C c1=new C();
}
}
class Manager1
{
static
{
[Link](11);
}
public static void main(String args[])
{
A a1=new A();
[Link](args);
}
}
12. Write the output for following program:
class Manager
{
static
{
[Link]("1");
080-41310124 [Link]
Lara Technologies
}
public static void main(String[] args)
{
[Link]("2");
}
}
class Manager1 extends Manager
{
static
{
[Link]("3");
}
public static void main(String[] args)
{
[Link](4);
}
}
{
[Link]("1");
}
{
[Link]("5");
}
public static void main(String[] args)
{
[Link]("2");
}
}
class Manager1 extends Manager
{
static
{
[Link]("3");
}
{
[Link]("6");
}
public static void main(String[] args)
{
[Link](4);
}
{
[Link]("7");
}
}
[Link]("2");
}
}
class Manager1 extends Manager
{
static
{
[Link]("3");
}
{
[Link]("6");
}
public static void main(String[] args)
{
[Link](4);
}
{
[Link]("7");
}
}
class Manager
{
static
{
[Link]("1");
[Link]("8");
}
public static void main(String[] args)
{
[Link]("2");
[Link]("5");
}
}
{
[Link]("4");
[Link]("6");
}
080-41310124 [Link]
Lara Technologies
{
[Link](2);
}
static void test(short b)
{
[Link](3);
}
static void test(double d)
{
[Link](4);
}
}
class Manager
{
public static void main(String[] args)
{
[Link](10);
[Link](10.9);
byte b=10;
float f1=10.00f;
[Link](b);
[Link](f1);
long l1=123l;
[Link](l1);
}
}
class Manager
{
static void test(int i)
{
[Link]("int");
}
public static void main(String args[])
{
[Link]((int)10.0);
}
}
class Manager
080-41310124 [Link]
Lara Technologies
{
static void test(int i)
{
[Link]("int");
}
public static void main(String args[])
{
[Link](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)
{
[Link]("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 [Link]
Lara Technologies
{
[Link]("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)
{
[Link]("float");
}
}
class A
{
void test()
{
[Link](1);
}
void test(int i)
{
[Link](2);
}
void test(int k,double j)
{
[Link](3);
}
}
class Manager
080-41310124 [Link]
Lara Technologies
{
public static void main(String[] args)
{
A a1=new A();
[Link]();
byte b=10;
[Link](b);
[Link](10,10.00);
}
}
class A
{
void test()
{
[Link](1);
}
void test(int i)
{
[Link](2);
}
void test(int k,double j)
{
[Link](3);
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link]();
byte b=10;
[Link]((int)b);
[Link](10,10.00);
}
}
080-41310124 [Link]
Lara Technologies
class A
{
public void m1()
{
m2();
}
public void m2()
{
m1();
}
SCJP Oriented
Question 19
Given:
1. class TestA {
2. public void start() { [Link](”TestA”); }
3. }
4. public class TestB extends TestA {
5. public void start() { [Link](”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 [Link]
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. [Link](x);
6. }
7. }
Given:
34. Test t = new Test();
35. [Link](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 [Link]
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 [Link]
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) {[Link](”Long “);}
14. public static void go(Short n) {[Link](”Short “);}
15. public static void go(int n) {[Link](”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 [Link]
Lara Technologies
Question 94
Given:
12. public class Wow {
13. public static void go(short n) {[Link](”short”); }
14. public static void go(Short n) {[Link](”SHORT”);}
15. public static void go(Long n) {[Link](” LONG”); }
16. public static void main(String [] args) {
17. Short y= 6;
[Link] 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. [Link]();
6. value *=multiplier;
7. }
8. public static void main(String[] args) {
9. MultiCalc calculator = new MultiCalc();
10. [Link](2);
11. [Link](”Value is: “+ [Link]);
12. }
13. }
What is the result?
A. Value is: 8
B. Compilation fails.
080-41310124 [Link]
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) {
[Link] x=6;
18. Pass2 p = new Pass2();
19. [Link](x);
20. [Link](” main x = “+ x);
21. }
22.
23. void doStuff(int x) {
24. [Link](” 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. [Link](desc);
9. }
10. public static void main(String[] args) {
11. Item it = new Item();
12. [Link](”Gobstopper”);
13. Item it2 = new Item();
080-41310124 [Link]
Lara Technologies
14. [Link](”Fizzylifting”);
15. modifyDesc(it, “Scrumdiddlyumptious”);
16. [Link]([Link]());
17. [Link]([Link]());
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) { [Link] = id; }
14. public void updateId(int newId) { id = newId; }
15.
16. public static void main(String[] args) {
17. ItemTest fa = new ItemTest(42);
18. [Link](69);
19. [Link]([Link]);
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 [Link]
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 < [Link]; 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 [Link]
Lara Technologies
Statements st2;
Statements st3;
_
_
_
Statements stn;
return 100;
}
return 2000;
}
}
080-41310124 [Link]
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()
{
[Link](“A”);
}
}
class B extends A
{
void test()
{
[Link](“B”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
B b1 = new B();
[Link]();
[Link]();
}
}
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 [Link]
Lara Technologies
[Link](“A”);
}
}
class B extends A
{
void test()
{
[Link](“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();
[Link]();
[Link]();
[Link]();
//[Link]();
}
}
Output:
A
B
B
Program 5:-
class A
{
void test()
{
080-41310124 [Link]
Lara Technologies
[Link](“A”);
}
}
class B extends A
{
void test()
{
[Link](“B”);
}
}
class Manager
{
public static void main(String args[])
{
B b2 = new A();
[Link]();
}
}
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()
{
[Link](“B”);
}
}
class Manager
{
public static void main(String args[])
{
A a1 = new B();
[Link]();
}
}
Output:
Compile time Error. Can’t find test() in class A.
080-41310124 [Link]
Lara Technologies
Program 7:-
class A
{
void test()
{
[Link](“A”);
}
}
class B extends A
{
void test()
{
[Link](“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<[Link]; i++)
{
all[i].test();
}
}
}
Output:
A
B
080-41310124 [Link]
Lara Technologies
Program 9:-
class A
{
void test()
{
[Link](“A”);
}
}
class B extends A
{
int test()
{
[Link](“B”);
return 100;
}
}
class Manager
{
public static void main(String args[])
{
A b1 = new B();
[Link]();
}
}
Output:
compile time error.
Program 10:-
class A
{
public void test()
{
[Link](“A”);
}
}
080-41310124 [Link]
Lara Technologies
class B extends A
{
void test()
{
[Link](“B”);
}
}
class Manager
{
public static void main(String args[])
{
A b1 = new B();
[Link]();
}
}
Output:
Compile time error.
Program 11:-
class A
{
void test()
{
[Link](“A”);
}
}
class B extends A
{
public void test()
{
[Link](“B”);
}
}
080-41310124 [Link]
Lara Technologies
class Manager
{
public static void main(String args[])
{
A b1 = new B();
[Link]();
}
}
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<[Link] ; i++ )
{
all[i].test();
}
}
}
}
}
void test()
{
[Link]("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<[Link] ; 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 [Link]
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<[Link] ; i++ )
{
[Link](all[i].getBalance());
}
}
}
[Link](all[i].getBalance());
}
}
}
080-41310124 [Link]
Lara Technologies
080-41310124 [Link]
Lara Technologies
[Link]("A");
}
}
class B extends A
{
static void test()
{
[Link]("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<[Link] ; i++ )
{
all[i].test();
}
}
}
class Manager11
{
public static void main(String args[])
080-41310124 [Link]
Lara Technologies
{
A a1=new A();
[Link](a1.i);
[Link](a1.j);
[Link](a1.k);
A a2=new B();
[Link](a2.i);
[Link](a2.j);
[Link](a2.k);
A a3=new C();
[Link](a3.i);
[Link](a3.j);
[Link](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();
[Link](a1.i);
A a2=new B();
[Link](a2.i);
A a3=new C();
[Link](a3.i);
}
}
080-41310124 [Link]
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();
[Link](a1.i);
[Link](b1.i);
[Link](b1.j);
C c1=new C();
[Link](c1.i);
[Link](c1.j);
[Link](c1.k);
}
}
class A
{
int i;
}
class B extends A
{
int j;
}
080-41310124 [Link]
Lara Technologies
class C extends B
{
int k;
}
class Manager13
{
public static void main(String args[])
{
B b1=new A();
[Link](a1.i);
[Link](b1.j);
[Link](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();
[Link](b1.i);
[Link](b1.j);
[Link](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();
[Link](b1.i);
[Link](b1.j);
}
}
class Manager17
{
public static void main(String args[])
{
B b1= new C();
A a1=b1;
080-41310124 [Link]
Lara Technologies
C c1=(C)a1;
[Link](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;
[Link](c1.k);
}
}
class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
080-41310124 [Link]
Lara Technologies
}
class Manager19
{
public static void main(String args[])
{
B a= new B();
A a1=a;
B b2= (B)a1;
[Link](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. [Link]([Link]());
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. [Link]([Link]() + “, ‘ + [Link]()
080-41310124 [Link]
Lara Technologies
25. + “, “+ [Link]());
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 [Link]
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) { [Link](); }
B. public void add(B b) { [Link](); }
C. public void add(A a) { [Link](); }
D. public void add(A a, B b) { [Link](); }
E. public void add(C c1, C c2) { [Link](); }
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 [Link]
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. [Link]();
32. [Link](”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 = [Link](a, b);
23. return c;
24. }
25. }
080-41310124 [Link]
Lara Technologies
and:
30. SubCalc sc = new SubCalc();
31. [Link]([Link](3,4));
32. [Link]([Link](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(){[Link](“Afoo”);}
13. }
14. public class Beta extends Alpha{
15. public void foo(){[Link](”Bfoo”);}
16. public static void main(Sting args[]){
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. [Link]();
[Link]();
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 [Link]
Lara Technologies
class A
{
static void test()
{
[Link](“A”);
}
}
class B extends A
{
static void test()
{
[Link](“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 < [Link]; 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 [Link]
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();
[Link](a1.i);
A a2 = new B();
[Link](a2.i);
[Link](a2.j);
A a3 = new C();
[Link](a3.i);
[Link](a3.j);
[Link](a3.k);
}
}
class A
{
int i;
}
class B extends A
080-41310124 [Link]
Lara Technologies
{
int j;
}
class C extends B
{
int k;
}
class Manager
{
public static void main(String args[])
{
A a1 = new A();
[Link](a1.i);
[Link](a1.j);
[Link](a1.k);
A a2 = new B();
[Link](a2.i);
[Link](a2.j);
[Link](a2.k);
A a3 = new C();
[Link](a3.i);
[Link](a3.j);
[Link](a3.k);
}
}
class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
080-41310124 [Link]
Lara Technologies
int k;
}
class Manager
{
public static void main(String args[])
{
B b1 = (B) new A();
[Link](b1.i);
[Link](b1.j);
[Link](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 [Link]
Lara Technologies
{
B b1 = new C();
A a1 = b1;
C c1 = (C ) a1;
[Link](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 [Link]
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;
[Link](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;
[Link](a1.i);
[Link](c1.i);
[Link](c1.j);
[Link](c1.k);
B b1 = (B) a1;
A a2 = b1;
[Link](b1.i);
[Link](b1.j);
[Link](b1.k);
[Link](a2.i);
[Link](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 [Link]
Lara Technologies
B b2 = (C) a1;
[Link](a1.i);
[Link](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 [Link]
Lara Technologies
class C extends B
{
int k;
}
class Manager
{
static void test(a a1)
{
[Link](a1.i);
[Link](a1.j);
[Link](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)
{
[Link](b1.i);
[Link](b1.j);
[Link](b1.k);
}
public static void main(String args[])
{
A a1 = new B();
test(a1);
test((B)a1);
}
}
class Manager
{
static void test1(A a1)
{
[Link](a1.i);
}
static void test2(B b1)
{
[Link](b1.i);
[Link](b1.j);
}
static void test3(C c1)
{
[Link](c1.i);
[Link](c1.j);
080-41310124 [Link]
Lara Technologies
[Link](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;
[Link](b1.j);
[Link](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 [Link]
Lara Technologies
//new prog
Class Test
{
static void test1(A a1)
{
[Link](“A”);
}
static void test1(B b1)
{
[Link](“B”);
}
static void test1(C c1)
{
[Link](“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)
{
[Link](“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 [Link]
Lara Technologies
class Test
{
void test(double d)
{
[Link](“double”);
}
void test(Object obj)
{
[Link](“Object”);
}
}
class Manager
{Test t = new Test();
[Link](10);
[Link](10.0);
[Link](new A());
[Link](new B());
[Link](new C());
[Link](new Object());
[Link]((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 [Link]
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;
[Link](a1.i);
[Link](c1.i);
[Link](c1.j);
[Link](c1.k);
B b1=(B) a1;
[Link](b1.i);
[Link](b1.j);
[Link](b1.k);
[Link](a3.i);
080-41310124 [Link]
Lara Technologies
[Link](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;
[Link](b1.i);
[Link](b1.j);
//[Link](b1.k);
[Link](a3.i);
//[Link](a3.j);
}
}
5.
class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
080-41310124 [Link]
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;
[Link](a1.i);
[Link](c1.k);
[Link](b2.j);
[Link](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;
[Link](a1.i);
[Link](a2.i);
}
}
080-41310124 [Link]
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 [Link]
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)
{
[Link](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)
{
[Link](a1.i);
}
{
[Link](c1.i);
[Link](c1.j);
[Link](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;
[Link]("b1.j");
[Link]("c1.k");
}
080-41310124 [Link]
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)
{
[Link]("double");
}
void test(Object obj)
{
[Link]("object ");
}
}
class Manager13
{
public static void main(String agrs[])
{
Test8 t=new Test8();
080-41310124 [Link]
Lara Technologies
[Link](10);
[Link](10.0);
[Link](new A());
[Link](new B());
[Link](new C());
[Link](new Object());
[Link]((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)
{
[Link]("double");
}
void test(Object obj)
{
[Link]("object ");
}
}
class Manager14
{
public static void main(String agrs[])
{
Test8 t=new Test8();
[Link](10);
[Link](10l);
[Link](10d);
080-41310124 [Link]
Lara Technologies
[Link](10.0);
[Link](new A());
[Link](new B());
[Link](new C());
[Link](new Object());
[Link]((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)
{
[Link]("A");
}
static void test2(B b1)
{
[Link]("B");
}
static void test3(C c1)
{
[Link]("C");
}
public static void main(String agrs[])
{
A a1=new C();
test1(a1);
test1((B) a1);
test1((C)a1);
080-41310124 [Link]
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)
{
[Link]("A");
}
static void test1(B b1)
{
[Link]("B");
}
static void test1(C c1)
{
[Link]("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 [Link]
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)
{
[Link]("A");
}
static void test1(B b1)
{
[Link]("B");
}
19.
class A
{
int i=0;
}
class B extends A
{
080-41310124 [Link]
Lara Technologies
int j=0;
}
class C extends B
{
int k=0;
}
class Test4
{
static void test1(A a1)
{
[Link]("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)
{
[Link]("A");
080-41310124 [Link]
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)
{
[Link]("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 [Link]
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)
{
[Link]("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 [Link]
Lara Technologies
[Link]?
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 [Link]
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();
[Link](a1.i);
A a2 = new B();
[Link](a2.i);
A a3 = new C();
[Link](a3.i);
080-41310124 [Link]
Lara Technologies
A a4 = new D();
[Link](a4.i);
A a5 = new E();
[Link](a5.i);
A a6 = new F();
[Link](a6.i);
B b1 = new B();
[Link](b1.i);
B b2 = new C();
[Link](b2.i);
B b3 = new D();
[Link](b3.i);
B b4 = new E();
Rule 13 Do not include anything in a name to indicate that the name
is for a class.
[Link](b4.i);
B b5 = new F();
[Link](b5.i);
C c1 = new C();
[Link](c1.i);
C c2 = new D();
[Link](c2.i);
C c3 = new E();
[Link](c3.i);
C c4 = new F();
[Link](c4.i);
D d1 = new D();
[Link](d1.i);
D d2 = new E();
[Link](d2.i);
D d3 = new F();
[Link](d3.i);
E e1 = new E();
[Link](e1.i);
E e2 = new F();
[Link](e2.i);
F f1 = new F();
[Link](f1.i);
080-41310124 [Link]
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 [Link]
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 [Link]
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 [Link]
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 [Link]
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 [Link]
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 “[Link]”. 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 “[Link]” 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 [Link]
Lara Technologies
{
public static void main(String[] args)
{
A a1=new A();
[Link](a1.i);
A a2=new B();
[Link](a2.i);
A a3=new C();
[Link](a3.i);
A a4=new D();
[Link](a4.i);
A a5=new E();
[Link](a5.i);
A a6=new F();
[Link](a6.i);
B b1=new B();
[Link](b1.i);
A b2=new C();
[Link](b2.i);
B b3=new D();
[Link](b3.i);
B b4=new E();
[Link](b4.i);
C c1=new C();
[Link](c1.i);
C c2=new C();
[Link](c2.i);
D d1=new D();
[Link](d1.i);
E e1=new E();
[Link](e1.i);
F f1=new F();
[Link](f1.i);
}
}
2.
class A
{
int i=9;
}
class B extends A
080-41310124 [Link]
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();
[Link](a1.i);
A a2=new B();
[Link](a2.i);
A a3=new C();
[Link](a3.i);
A a4=new D();
[Link](a4.i);
A a5=new E();
[Link](a5.i);
A a6=new F();
[Link](a6.i);
B b1=new B();
[Link](b1.i);
A b2=new C();
[Link](b2.i);
B b3=new D();
[Link](b3.i);
B b4=new E();
[Link](b4.i);
B b5=new F();
080-41310124 [Link]
Lara Technologies
[Link](b5.i);
C c1=new C();
[Link](c1.i);
C c2=new C();
[Link](c2.i);
C c3=new E();
[Link](c3.i);
C c4=new F();
[Link](c4.i);
D d1=new D();
[Link](d1.i);
D d2=new E();
[Link](d2.i);
D d3=new F();
[Link](d3.i);
E e1=new E();
[Link](e1.i);
E e2=new F();
[Link](e2.i);
F f1=new F();
[Link](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 [Link]
Lara Technologies
}
class F extends A
{
int i=39;
}
class Manager
{
public static void main(String[] args)
{
A a1=new A();
[Link](a1.i);
A a2=new B();
[Link](a2.i);
A a3=new C();
[Link](a3.i);
A a4=new D();
[Link](a4.i);
A a5=new E();
[Link](a5.i);
A a6=new F();
[Link](a6.i);
B b1=new B();
[Link](b1.i);
A b2=new C();
[Link](b2.i);
B b3=new D();
[Link](b3.i);
B b4=new E();
[Link](b4.i);
C c1=new C();
[Link](c1.i);
C c2=new C();
[Link](c2.i);
D d1=new D();
[Link](d1.i);
E e1=new E();
[Link](e1.i);
F f1=new F();
[Link](f1.i);
}
}
080-41310124 [Link]
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();
[Link](a1.i);
A a2=new B();
[Link](a2.i);
A a3=new C();
[Link](a3.i);
A a4=new D();
[Link](a4.i);
A a5=new E();
[Link](a5.i);
A a6=new F();
[Link](a6.i);
B b1=new B();
[Link](b1.i);
080-41310124 [Link]
Lara Technologies
A b2=new C();
[Link](b2.i);
B b3=new D();
[Link](b3.i);
B b4=new E();
[Link](b4.i);
B b5=new F();
[Link](b5.i);
C c1=new C();
[Link](c1.i);
C c2=new C();
[Link](c2.i);
D d1=new D();
[Link](d1.i);
E e1=new E();
[Link](e1.i);
F f1=new F();
[Link](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 [Link]
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 [Link]
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 [Link]
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 [Link]
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 [Link]
Lara Technologies
class C extends B
{
}
class X
{
int test()
{
return 0;
}
}
class B extends A
{
double test()
{
return 0.0;
}
}
Packages
080-41310124 [Link]
Lara Technologies
Program1:-
src [Link]
pack1 package pack1;
[Link] class A{ }
pack2 class B{ }
[Link]
classese
[Link]
pack1
[Link]
[Link] package pack2;
class B{ }
[Link]
pack2 [Link] class D{ }
080-41310124 [Link]
Lara Technologies
[Link]
src
package com.pack1;
com
class A{ }
pack1
class B{ }
[Link]
[Link]
classese
[Link]
pack2
package com;
[Link]
com
class A{ }
pack1 [Link] class B{ }
[Link]
[Link] class C{ }
[Link]
[Link]
[Link]
[Link] package com.pack2;
pack2
[Link]
[Link] class A{ }
class B{ }
class C{ }
080-41310124 [Link]
Lara Technologies
[Link]
src
package [Link];
com class A { }
lara class B { }
io class C { }
[Link]
classes [Link]
db [Link]
[Link]
com package [Link];
lara class B
{
io [Link]
int i;
[Link]
}
[Link]
[Link]
package [Link];
[Link] class C
[Link] {
db Psvm(String args[])
{
B b1 = new B();
080-41310124 S.o.p( b1.i );
[Link]
}
}
Lara Technologies
src [Link]
package [Link].pack1;
com
[Link] public class A
lara {
[Link] int i;
[Link] }
pack1
[Link]
package [Link];
import com.C;
class B
classes {
[Link]
C c1 = new C();
080-41310124 com } [Link]
[Link]
[Link]
lara pack1
Lara Technologies
[Link]
package com;
import [Link].A;
import [Link].B;
class C
{
Psvm(String args[])
{
A a1 = new A();
B b1 = new B();
}
}
[Link]
[Link]
[Link]
[Link]
src
[Link]
com [Link]
080-41310124 [Link] [Link]
lara
[Link]
[Link]
Lara Technologies
classes
com
lara
080-41310124 [Link]
Lara Technologies
src
com
lara
[Link]
[Link]
[Link]
classes
com
[Link]
lara [Link]
[Link]
src
com
[Link]
lara [Link]
classes
[Link]
com
[Link]
lara
080-41310124 [Link]
Lara Technologies
src
com
lara
server
[Link]
client [Link]
classes
com
lara
[Link]
server
client [Link]
package [Link];
import [Link];
[Link]
package [Link]; [Link]
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”); [Link](); [Link]
} }
} }
Lara Technologies
src [Link]
[Link] class A
{
}
class B
{
}
[Link]
[Link]
class X
{
}
class B
{
}
080-41310124 [Link]
Lara Technologies
src
pack1
[Link]
classes
pack1
[Link]
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 [Link]
Lara Technologies
E:\
LAB
13th-batch
package-dev
src
pack1
[Link]
[Link]
classes
pack1
[Link]
[Link]
36) [Link]
package com.pack1;
class A
{
void show()
{
[Link](“Inside A”);
}
}
[Link]
package com.pack1;
class B
{
public static void main(String[] args)
{
080-41310124 [Link]
Lara Technologies
A a1 = new A();
[Link]();
}
}
O/P : Inside A
37) [Link]
package com.pack1;
class A
{
private void show()
{
[Link](“Inside A”);
}
}
[Link]
package com.pack1;
class B
{
public static void main(String[] args)
{
A a1 = new A();
[Link]();
}
}
O/P: Compiler Error
080-41310124 [Link]
Lara Technologies
E:\
LAB
13th-batch
package-dev
src
com
pack1
[Link]
[Link]
classes
com
pack1
[Link]
[Link]
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.
[Link] and [Link] shares a common package stream i.e
com.pack1.
Access Specifiers :
Access specifiers are the keywords which specifies the
accessibility of class and class [Link] java we have four access
levels and three access specifiers.
E:\
LAB
13th-batch
package-dev
package
stream
src
com
pack1
[Link]
pack2
[Link]
classes
com
pack1
[Link]
080-41310124 [Link]
Lara Technologies
pack2
[Link]
38) [Link]
package com.pack1;
public class A
{
public void show()
{
[Link](“Inside A”);
}
}
[Link]
package com.pack2;
import com.pack1.A;
class B
{
public static void main(String[] args)
{
A a1 = new A();
[Link]();
}
}
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 [Link] , compiler will produce [Link] first and
then [Link] 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 [Link]
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. [Link](bytes);
C. [Link](bytes);
080-41310124 [Link]
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. [Link](bytes);
C. [Link](bytes);
D. [Link](bytes);
E. import [Link]. *; process(bytes);
F. SomeApp cannot use the process method in BitUtils.
Answer: F
Question 4
Given a file [Link]:
1. package [Link];
2.
3. public class GrizzlyBear extends Bear {
4. void hunt() {
5. Salmon s = findSalmon();
6. [Link]();
7. }
8. }
and another file, [Link]:
1. package [Link];
2.
3. public class Salmon extends Fish {
4. void consume() { /* do stuff */ }
5. }
080-41310124 [Link]
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 [Link]
B. add public to the start of line 4 in [Link]
C. add import [Link].*; at line 2 in [Link]
D. add import [Link].*; at line 2 in [Link]
E. add import [Link].*; at line 2 in [Link]
F. add import [Link].*;at line 2 in
[Link]
Answer: AD
Question 5
Given a correctly compiled class whose source code is:
1. package [Link];
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. [Link]
C. java com/sun/sjcp/Commander
D. java -cp [Link] Commander
E. java -cp com/sun/sjcp Commander
Answer: B
Question 6
Click the Exhibit button.
080-41310124 [Link]
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 [Link]. 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/[Link].
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/[Link].
C. The JAR file is located at /foo/[Link] and a classpath
environment variable is set that includes /foo/[Link]/[Link].
D. The JAR file is located at /foo/[Link] and a classpath
080-41310124 [Link]
Lara Technologies
Question 8
Given:
1. package [Link];
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 [Link] if run from the /apps
directory
C. java -classpath /apps [Link] 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 [Link] 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 [Link]
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 [Link] file?
A. /test/[Link]
080-41310124 [Link]
Lara Technologies
B. /home/bob/[Link]
C. /test/games/[Link]
D. /usr/lib/games/[Link]
E. /home/bob/games/[Link]
F. inside jarfile /opt/java/lib/[Link] (with a correct manifest)
G. inside jarfile /home/bob/downloads/[Link] (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 [Link];}
27.}
28. public class Euro extends Money{
29. public String getC(int x){return [Link]();}
30. public static void main(String args[]){
31. [Link](new Yen().getC()+””+new Euro().getC());
32. }
33. }
What is the result?
[Link].
[Link] Canada.
[Link] null.
[Link] Canada.
[Link] fails due to an error on line 26.
[Link] 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 [Link]
Lara Technologies
abstract class A
{
void test1()
{
_
_
_
}
abstract void test2();
}
class B extends A
{
void test2()
{
_
_
_
}
}
abstract class A
{
void test1()
{
_
_
_
080-41310124 [Link]
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 [Link]
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 [Link]
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 [Link]
Lara Technologies
void test();
}
interface B
{
void show1();
void test1();
}
46) interface A
{
void show();
}
class C implements A
{
void show()
{
[Link](“show() implemented”);
}
}
O/P: Compile time error.
080-41310124 [Link]
Lara Technologies
47) interface A
{
void show();
}
class B{
void show()
{
[Link](“show()
implemented”);
}
}
class C implements A
extends B //Syntax error
{
public static void
main(String[] args)
{
C ob = new C();
[Link]();
}
48) interface A
{
void show();
}
class B {
void show()
080-41310124 [Link]
Lara Technologies
{
[Link](“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 [Link]
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 [[Link]]. 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 [Link]
Lara Technologies
abstract class C { }
While compiling the above file there will be 3 ‘.class’ files generated.
There is a default constructor in both ‘[Link]’ and as well as
‘[Link]’.
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 [Link]
Lara Technologies
080-41310124 [Link]
Lara Technologies
1.
class A
{
abstract void test()
{
[Link]("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 [Link]
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 [Link]
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();
[Link](10,10);
[Link]();
B. Circle c = new Shape();
[Link](10,10);
[Link]();
C. Shape s = new Circle();
[Link](10,10);
[Link]();
080-41310124 [Link]
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 [Link]
Lara Technologies
080-41310124 [Link]
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 [Link]
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 [Link]
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 [Link].
}
C)
class A
{
final int i=9;
A()
{
i = 9;
}
}
D)
class A
{
final int i;
A()
{
i = 9;
}
}
E)
class A
080-41310124 [Link]
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 [Link]
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 [Link]
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 [Link]
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 [Link]
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 [Link]
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 [Link]
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 [Link]
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() [Link]
{ com
}
dev2 lara
[Link]
src
com
lara
classes
[Link]
com
080-41310124 [Link]
lara
[Link]
Lara Technologies
[Link] [Link]
package [Link]; package [Link];
import [Link];
public class Person
{ public class Manager
void test() {
{ public void static main(String args[])
S.o.p(“ Hello“); {
} Person p1 = new Person();
} [Link]();
}
}
080-41310124 [Link]
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 [Link]
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 [Link]
Lara Technologies
dev2\classes>set classpath=../../dev1/classes
dev2\classes>java [Link]
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 [Link]
Or
dev2\classes>set classpath=.;../../dev1/classes;
dev2\classes>java [Link]
080-41310124 [Link]
Lara Technologies
dev2\classes>set classpath=%classpath%;../../dev1/classes;.
dev2\classes>java [Link]
Or
dev2\classes>set classpath=.;../../dev1/classes;%classpath%;
dev2\classes>java [Link]
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 [Link]
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 [Link]
[Link]
[Link]
080-41310124 [Link]
[Link]
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/[Link] *
080-41310124 [Link]
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/[Link];.
accounts-dev\src>javac –d ../classes com/lara/accounts/*.java
accounts-dev\src>java [Link]
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 [Link] 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 [Link]
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 [Link]
Lara Technologies
SCJP Oriented
Question 1
A class [Link] is correctly defined in the jar file
[Link].
A user wants to execute the main method of Poker on a UNIX system
using the command:
java [Link]
What allows the user to do this?
A. put [Link] in directory /stuff/java, and set the CLASSPATH to
include /stuff/java
B. put [Link] in directory /stuff/java, and set the CLASSPATH to
include /stuff/java/*.jar
C. Put [Link] in directory /stuff/java, and set the CLASSPATH to
include /stuff/java/[Link]
D. put [Link] in directory /stuff/java/games/cards, and set the
CLASSPATH to include /stuff/java
E. put [Link] in directory /stuff/java/games/cards, and set the
CLASSPATH to include /stuffijava/*.jar
F. put [Link] in directory /stuff/java/games/cards, and set the
CLASSPATH to include /stuff/java/[Link]
Answer: C
080-41310124 [Link]
Lara Technologies
Day 19
[Link] 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();
[Link](a1 instanceof A);
[Link](a1 instanceof B);
[Link](a1 instanceof C);
[Link](a2 instanceof A);
[Link](a2 instanceof B);
[Link](a2 instanceof C);
[Link](a3 instanceof A);
[Link](a3 instanceof B);
[Link](a3 instanceof C);
[Link](b1 instanceof A);
[Link](b1 instanceof B);
[Link](b1 instanceof C);
[Link](b2 instanceof A);
[Link](b2 instanceof B);
[Link](b2 instance of C);
080-41310124 [Link]
Lara Technologies
}
}
3.
Class A
{
}
Class B
{
}
Class Manager
{
Public static void main(string args[]
{
A a1= new A();
If(a1 instance of A)
{
[Link](“type of A”);
}
Else
{
[Link](“ not a type of A”);
}
If(a1 instance of B)
{
[Link](“type of B”);
}
Else
{
[Link](“ not a type of B”);
}
}
}
4.
Class A
080-41310124 [Link]
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();
[Link](a1);
[Link](a2);
[Link](b1);
}
}
6.
Class Manager
{
Public static void main(String agrs[])
{
[Link](args[0]);
[Link](args[1]);
}
}
080-41310124 [Link]
Lara Technologies
7.
Class Manager
{
Public static void main(String agrs[])
{
[Link](“hello to all” + args[0]);
}
}
Classes>java Manager world
Classes>java Manager
8.
Class Manager
{
Public static void main(String agrs[])
{
[Link](“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 [Link]
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. [Link]([Link](4, 5));
What is the result?
A. Line 26 prints “a” to [Link].
B. Line 26 prints ‘b” to [Link].
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. [Link]
B. [Link]
C. [Link]()
D. [Link]()
E. [Link]()
Answer: B
Question 4
Given:
15. public class Yippee {
16. public static void main(String [] args) {
17. for(int x = 1; x < [Link]; x++) {
18. [Link](args[x] +“ “);
19. }
080-41310124 [Link]
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<[Link]; x++) {
16. [Link](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 [Link]
Lara Technologies
Question 7
Given:
[Link] Mud{
12. //insert code here
13. [Link](“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 [Link]
Lara Technologies
5. }
6. public void go(String…y,int x){
7. [Link](y[[Link]-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 [Link]
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 [Link]?
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 [Link]
Lara Technologies
If I compile like
E:\lab\dev\src> javac [Link]
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
[Link]
E:\lab
package com;
dev
class Hello
src {
com
}
[Link]
If I compile like
1. E:\lab\dev\src>javac [Link]
2. E:\lab\dev\src>javac com/[Link]
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 [Link]
Lara Technologies
package pack1;
E:\lab class A
{
dev
}
src
pack1
[Link]
[Link]
pack2
package pack2;
[Link] 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 [Link] & [Link] together in the
above program?
118. I have directory structure as follows
[Link]
package pack2;
pack2 import [Link];
[Link] class Hello
{
080-41310124 [Link]
}
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 [Link]
kept?
package pack1;
E:\lab class A
{
dev
}
src
pack1
[Link]
[Link]
120.
E:\lab [Link]
dev package src.pack1;
class A
src {
pack1
}
[Link] 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 [Link]
Lara Technologies
121.
[Link]
E:\lab
private class Hello
dev {
src
}
[Link]
080-41310124 [Link]
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;
[Link]([Link]);
[Link]([Link]);
}
}
How may objects and references are creating in the above
program and what could be the output?
080-41310124 [Link]
Lara Technologies
}
abstract void m1();
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
183. Abstract class A{
void m(){
[Link]("A");
}
static abstract void m1(){
[Link]("abstract");
}
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
184. abstract interface A{
void m1();
public abstract void m2();
public void m3();
abstract void m4();
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
185. interface B{
void m();
}
class A implements B{}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
186. interface B{
void m();
}
class A implements B{
void m()
{
[Link]("hello ");
public static void main(String args[]){
new A().m();
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] hello
187. Abstract class A{
public void methedA(){
080-41310124 [Link]
Lara Technologies
[Link]("helloA");
}
public static void main(String args[]){
A a1=new A();
[Link]();
}
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] helloA.
188. interface B{
void m1();
}
class A implements B{}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
189. interface B{
private abstract void m1();
}
class A extends B{
public void m1(){
[Link]("hi");
}
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully &
prints hi
190. interface B{
abstract void m1();
}
class A implements B{
protected void m1(){
[Link]("hello");
}
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
&prints hello.
191. interface B{
abstract void methodB();
}
class A implements B{
public abstract void methodB(){
[Link]("hi");
080-41310124 [Link]
Lara Technologies
}
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] hello.
192. interface B{
public static abstract void interfaceMethod();
}
abstract class A implements B{
public void interfaceMethod (){
[Link]("hi");
}
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] hello.
193. interface B{
public final abstract void interfaceMethod();
}
abstract class A implements B{
public void interfaceMethod (){
[Link]("hello");
}
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] hello.
194. interface B{
public static abstract void interfaceMethod();
}
abstract class A implements B{
public void interfaceMethod ()
{
[Link]("hello");
}
public static void main(String args[]){
B b1=new A();
[Link]();
}
}
what is the result ?
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] hello.
195. interface B{
080-41310124 [Link]
Lara Technologies
new A().interfaceMethod1();
}
}
what is the result ?
[Link] b. RTE c. Compile success d. prints hello and helloto all.
198. abstract class A{
public void main(){
[Link]("A");
}
}
class B implements A{
public static void main(String args[]){
new B().main();
}
}
[Link] b. RTE c. Compile success d. prints A;
199. abstract class A {
void main();
}
Class C extends A{
public void main()
{
[Link]("good");
}
public static void main(String args[]){
new C().main();
}
}
[Link] b. RTE c. Compile success d. prints good.
200. abstract class A{
abstract void m1();
public void main(){
[Link]("test");
}
}
abstract class B extends A{
abstract void m2();
}
class C extends B{
public void m2(){
[Link]("class C")
}
}
[Link] b. RTE c. Compile success d. prints class C;
080-41310124 [Link]
Lara Technologies
201. interface I{
abstract int i=10;
public abstract void test();
}
abstract class A implements I{
public void test(){
[Link]("test");
}
public static void main(String ... args) {
[Link](new A().i);
}
}
[Link] b. RTE c. Compile success d. prints 10;
202. interface Test{
int i;
public void test();
}
abstract class A implements Test {
public void test(){
[Link]("test");
}
public static void main(String ... args) {
Test t=new A();
t.i=20;
[Link](new A().i);
}
}
[Link] b. RTE c. Compile success [Link]&Run succes & prints 20
[Link]&Run succes & prints 0;
203. abstract class Test{
abstract Test();
abstract void Test(int i);
}
class ExtendsTest extends Test{
public Test(){
[Link]("this is test");
}
public void Test(int i){
[Link]("this id test int test");
}
static public void main(String args[]) {
int i=20;
new ExtendsTest().Test(i++);
}
080-41310124 [Link]
Lara Technologies
}
[Link] b. RTE c. Compile success [Link]&Run succes & prints 20
[Link]&Run succes & prints 0;
f. prints 21;
204. interface I{
void testA();
}
class A implements I{
void testA(){
[Link]("One");
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] One
205. interface Test{
public void test(){
[Link]("Test");
}
}
class C implements Test{}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] Test
206. interface I{
void testA(){
[Link]("One");
}
}
class A implements I{
public static void main(String args[]){
void testA(){
[Link]("Two");
}
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] One. [Link] Two.
207. Interfac A{
void test();
[Link]("A");
}
Interface B extends A{
void test();
[Link]("B");
}
080-41310124 [Link]
Lara Technologies
class C implements B{
public static void main(String args[]){
void test(){
[Link]("C");
}
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] B. [Link] 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{
[Link]("Lara");
}
}
class B extends A{
public static void main(String args[]){
public void testA{
[Link]("Java");
}
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] f. Lara
215. class A{
abstract void testA(){
[Link]("hello");
}
}
class B {
protected void testA(){
[Link]("Java");
}
}
080-41310124 [Link]
Lara Technologies
216. interface A{
void testA();
}
class B{
void testB(){
[Link]("B");
}
}
interface C{
void testC();
}
class Manager extends B implements A,C{
public static void testA(){
[Link]("A");
}
public static void testB(){
[Link]("B");
}
public static void testC(){
[Link]("C");
}
public static void main(String args[]){
[Link]("main");
[Link]();
[Link]();
[Link]();
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] A. [Link] B. [Link] C
217. public class A extends B{
void testC(){
[Link]("Hello To All");
}
public static void main(String args[]){
new A().testc();
}
}
abstract class B{
void testC();
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] Hello To All.
218. class One implements Two{
080-41310124 [Link]
Lara Technologies
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] test test.
226. interface A{}
class B{
public final static void test(){[Link]("test");}
}
public class Abc extends B implements A {
public static void main(String[] args) {
new Abc().test();
[Link]();
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] test test.
227. interface A{ void test();}
class B{
void test(){ [Link]("test"); }
}
public class Abc extends B implements A {
public static void main(String[] args) {
new Abc().test();
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] test.
228. interface A{ void test();}
class B{
public void test(){ [Link]("test"); }
}
public class Abc extends B implements A
{
public static void main(String[] args) {
new Abc().test();
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] test.
080-41310124 [Link]
Lara Technologies
233. interface X {
public static final int i;
final float f;
static public String S;
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] none
234. abstract class A{
int i=20;
A(int i){
[Link](i);
}
}
class B extends A
{
public static void main(String args[]){
new B();
}
}
[Link] b. RTE c. Compile success [Link] and run successsully
[Link] 20;
235. abstract class A{
int i=20;
A(int i) {
[Link](i);
}
}
class B extends A{
B() {
super(40);
}
public static void main(String args[]){
new B();
}
}
[Link] b. RTE c. Compile success [Link] 40 [Link] 20;
236. abstract class A{
int i=20;
A(){
this(70);
}
A(int i){
[Link](i);
}
080-41310124 [Link]
Lara Technologies
}
class B extends A{
B(){
this(50);
}
B(int k) {
super(60);
}
public static void main(String args[]){
A a = new B();
}
}
[Link] b. RTE c. prints 50 [Link] 60 [Link] 20 [Link] 70;
237. abstract class A{
int i=20;
A(int i) {
[Link](i);
}
abstract static {};
}
class B extebds A{
B(){
super(40);
}
public static void main(String args[]) {
new B();
}
}
[Link] b. RTE c. Compile success [Link] 40 [Link] 20;
238. abstract class A{
int i=20;
A(int i) {
[Link](i);
}
static {
[Link]("static ");
}
}
class B extends A{
B(){
super(40);
}
public static void main(String args[]){
new B();
080-41310124 [Link]
Lara Technologies
}
}
[Link] b. RTE c. Compile success [Link] static and 40 [Link]
static and 20;
F. prints 40 and static [Link] 20 and static
239. abstract classA {
int i=20;
A(int i){
[Link](i);
}
static {
[Link]("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);
}
}
[Link] b. RTE c. Compile success [Link] static and 40 [Link]
static and 20;
F. prints 40 and static [Link] 20 and static [Link] 50 20 40 static
;
240. abstract class A{
int i=20;
A(int i){
[Link](i);
}
static {
[Link]("static ");
}
{
[Link]("instance");
}
}
class B extends A
{
B(){
080-41310124 [Link]
Lara Technologies
super(40);
}
public static void main(String args[]){
A a=new B();
}
}
[Link] b. RTE c. Compile success [Link] static , intance and 40
[Link] static,instance and 20;
f. prints 40 ,instance and static [Link] instance static and 20;
080-41310124 [Link]
Lara Technologies
Note:
1. CTE = Compile Time Error
2. RTE = Run Time Error
3. CS = Compilation Success
080-41310124 [Link]
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;
[Link](++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) {
[Link](new Manager().flag);
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
[Link] class Manager {
boolean flag;
public static void main(String[] args) {
if(flag=true){
[Link](flag);
}
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
[Link] class Manager {
boolean static flag;
public static void main(String[] args) {
if(flag=true){
[Link](flag);
}
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
[Link] class Manager {
static boolean flag;
public static void main(String[] args) {
080-41310124 [Link]
Lara Technologies
if(flag=true){
[Link](flag == false);
}
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
[Link] class Manager {
static boolean flag = !true;
public static void main(String[] args) {
if(flag=true){
[Link](flag = false);
}
}
}
a. CTE b. RTE c. CS and prints true
d. CS and prints false
[Link] class Manager {
String name = null;
static{
name = "abc";
}
public static void main(String[] args) {
[Link]([Link]());
}
}
a. CTE b. CS and Gives nothing. c. CS and
Gives NPE
d. CS and prints 3
[Link] class Manager {
static String name = null;
static{
name = "abc";
}
public static void main(String[] args) {
[Link]([Link]);
}
}
a. CTE b. CS and Gives nothing. c. CS and
Gives NPE
d. CS and prints 3
[Link] class Manager {
static final String name;
public static void main(String[] args) {
080-41310124 [Link]
Lara Technologies
[Link]([Link]());
}
}
a. CTE b. CS and prints nothing. c. CS and
Gives NPE
d. CS and prints 3
[Link] class Manager {
static final String name;
static{
name = "abc";
}
public static void main(String[] args) {
[Link]([Link]());
}
}
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
[Link] class Manager {
static public void main(String main[]) {
[Link]([Link]);
}
}
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
[Link] class Manager {
static void public main(String main[]) {
[Link]([Link]);
}
}
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
[Link] class Manager {
public void main(String main[]) {
[Link]([Link]);
}
}
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
[Link] class Manager {
static void main(String main[]) {
[Link]([Link]);
}
}
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
[Link] class Manager {
protected static void main(String main[]) {
[Link]([Link]);
}
}
080-41310124 [Link]
Lara Technologies
}
}
class Test{
public static void main(String []args) {
[Link](“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 [Link]
c. CS and prints Test, if we run [Link]
d. CS and prints only Manager, if we run either [Link]
or [Link]
e. None of the above
[Link] class Manager {
public static void main(String []args) {
[Link](args);
}
}
class Test{
public static void main(String []args) {
[Link](“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 [Link]
c. CS and prints Test, if we run [Link]
d. CS and prints only Manager, if we run either [Link]
or [Link]
e. CS and prints only Test, if we run either [Link] or
[Link]
f. None of the above
[Link] class Manager {
public static void main(String []args) {
[Link]("Manager");
}
}
class Test extends Manager{
public static void main(String []args) {
[Link]("Test");
}
}
a. CS and prints Manager, if we run [Link]
080-41310124 [Link]
Lara Technologies
a. 0 b. 1 c. NPE d. 2
080-41310124 [Link]
Lara Technologies
Manager mgr;
Manager(){
[Link](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
[Link] class Manager{
Manager mgr = new Manager();
Manager(){
[Link](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
[Link] class Manager{
int i;
Manager mgr;
public static void main(String args[]){
[Link](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
[Link] class Manager{
int i;
public static void main(String args[]){
Manager mgr;
[Link](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
[Link] class Manager{
080-41310124 [Link]
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
[Link] class Manager{
final int i = 9;
Manager(){
i = 90;
}
static Manager mgr = new Manager();
public static void main(String args[]){
[Link](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
[Link] class Manager{
final int i;
static Manager mgr = new Manager();
public static void main(String args[]){
[Link](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
[Link] class Manager{
static int i;
static{
i = 9;
}
{
i ++;
}
Manager(){
i++;
}
static Manager mgr = new Manager();
public static void main(String args[]){
new Manager();
[Link](mgr.i);
}
}
080-41310124 [Link]
Lara Technologies
[Link] Test{
int i = 10;
{
i++;
}
Test(){
--i;
}
}
public class Manager extends Test{
Manager(){
i--;
}
{
++i;
}
public static void main(String args[]){
[Link](new Manager().i);
080-41310124 [Link]
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
[Link] 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[]){
[Link](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
[Link] Test{
static {
[Link](1);
}
{
[Link](2);
}
080-41310124 [Link]
Lara Technologies
Test(){
[Link](3);
}
}
public class Manager extends Test{
Manager(){
[Link](4);
}
static{
[Link](5);
}
{
[Link](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
[Link] Test{
Test(){
[Link]("1");
}
}
public class Manager extends Test{
Manager(){
[Link]("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
[Link] class Manager{
static void test(){
if(true){
return;
}
else{
080-41310124 [Link]
Lara Technologies
return;
}
[Link]("the end");
}
public static void main(String args[]){
test();
}
}
a. CTE b. RTE c. CS and prints the end
d. none of the above
[Link] class Manager{
static void test(){
if(true){
return;
}
else{
return;
[Link]("the end");
}
}
public static void main(String args[]){
test();
}
}
a. CTE b. RTE c. CS and prints the end
d. none of the above
[Link] class Manager{
static void test(int i){
[Link]("int");
}
static void test(byte i){
[Link]("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
[Link]("int");
}
static void test(short i){
[Link]("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
[Link] A{
static void test(){
[Link]("A");
}
}
public class Manager extends A{
static void test(){
[Link]("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
[Link]();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
[Link] A{
void test(){
[Link]("A");
}
}
public class Manager extends A{
static void test(){
[Link]("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
[Link]();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
[Link] A{
static void test(){
[Link]("A");
}
}
public class Manager extends A{
void test(){
[Link]("Manager");
}
080-41310124 [Link]
Lara Technologies
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
[Link] A{
private final void test(){
[Link]("A");
}
}
public class Manager extends A{
public void test(){
[Link]("Manager");
}
public static void main(String args[]){
Manager mgr = new Manager();
[Link]();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
[Link] class A{
private abstract void test();
}
public class Manager extends A{
public void test(){
[Link]("Manager");
}
public static void main(String args[]){
Manager mgr = new Manager();
[Link]();
}
}
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 [Link]
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){
[Link]("B");
}
void test(A a1){
[Link]("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
[Link] A{}
class B extends A{}
class Test{
void test(B b1){
[Link]("B");
}
void test(A a1){
[Link]("A");
}
}
public class Manager{
080-41310124 [Link]
Lara Technologies
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints B
[Link] A{
int i = 9;
}
class B extends A{
int i = 19;
}
public class Manager{
public static void main(String args[]){
A a1 = new B();
[Link](a1.i);
}
}
a. CTE b. RTE c. CS and prints 19
d. CS and prints nothing e. CS and prints 19
[Link] A{
int i = 9;
}
class B extends A{
int i = 19;
}
public class Manager{
public static void main(String args[]){
A a1 = new B();
[Link]((B)a1.i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 19
[Link] A{
int i = 9;
}
class B extends A{
int i = 19;
}
public class Manager{
public static void main(String args[]){
A a1 = new B();
[Link](((B)a1).i);
}
080-41310124 [Link]
Lara Technologies
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 19
[Link] A{
int i = 9;
}
class B extends A{
int i = 19;
}
public class Manager{
public static void main(String args[]){
A a1 = new A();
[Link]((B)a1.i);
}
}
a. CTE b. RTE c. CS and prints 9
d. CS and prints nothing e. CS and prints 19
[Link] 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();
[Link](((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
[Link] A{
int i = 9;
}
class B extends A{
int i = 19;
}
class C extends B{
int i = 29;
}
080-41310124 [Link]
Lara Technologies
f. CS and prints 19
122. class A{
static abstract void test(){
[Link]("A");
}
}
public class Manager{
public static void main(String args[]){
[Link]("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[]){
[Link](“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[]){
[Link](“Manager”);
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
080-41310124 [Link]
Lara Technologies
133. interface A{
void test();
}
public class Manager extends A{
void test(){
[Link]("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
[Link]();
}
}
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(){
[Link]("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
[Link]();
}
}
a. CTE b. RTE c. CS and prints A
d. CS and prints nothing e. CS and prints Manager
135. interface A{
080-41310124 [Link]
Lara Technologies
void test();
}
public class Manager implements A{
public void test(){
[Link]("Manager");
}
public static void main(String args[]){
Manager a1 = new Manager();
[Link]();
}
}
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(){
[Link]("Manager");
}
public static void main(String args[]){
A a1 = new Manager();
[Link]();
}
}
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(){
[Link]("Manager");
}
public static void main(String args[]){
A a1 = new A();
[Link]();
}
}
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 [Link]
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();
[Link](((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
[Link](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 [Link]
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++;
[Link](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++);
[Link](i);
}
static void test(int i){
[Link](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[]){
[Link](new Manager().mgr);
}
}
a. CTE b. RTE c. CS and prints nothing
d. CS and prints null
168. I have directory structure as follows
[Link]
If I compile like
E:\lab\dev\src> javac [Link]
080-41310124 [Link]
Lara Technologies
080-41310124 [Link]