Oop
Oop
Ans :- We have put everything in a class because of the JVM ‘s work. JVM Complies the java
byte code in to ‘a .class’ file. When we compile a Program whatever the code we put in class of a
package in java are going to convert into machine code and after that the program will run.
5.WHO AM I?
USE FOLLOWING
(Class, Method, Object, Instance Variable)
a. I am compiled from a .javafile . Class
b. My instance variable values can be different from my buddy’s values. Object
c. I behave like a template. Class
d. I like to do stuff. Method
e. I can have many methods. Class
f. I represent ‘state’. class
g. I have behaviors. object
h. I am located in objects. class
i. I live on the heap. Instance Variable
j. I am used to create object instances. Instance Variable
k. My state can change.
l. I declare methods. class
m. I can change at runtime. Object
int x=1;
While(x<3) {
System.out.print (“Doo”);
System.out.print (“Bee”);
x=x+1;
}
If(x ==3) {
System.out.print (“Doo”);
}
}
}
6. CodeMagnets
A working java program is all scrambled up on the fridge. Can you reconstruct the
code snippets to make a working java program that produces the output listed below?
Some of the curly braces fell on the floor and they were too small to pick up, so feel free
to add as many of those as you need!
Class shuffle{
Public static void main(String[] args){
int x=3;
System.out.println(“% java shufflel”);
while(x>0)
{
if(x>2)
{
System.out.print(“a”);
}
x=x-1;
System.out.print(“-“);
if(x==2)
{
System.out.print(“b c”);
}
x=x-1;
System.out.print(“-“);
if(x==1)
{
System.out.print(“d”);
x=x-1;
}
}}}
Ans :- If we pass anything other than numerical value in Integer.parseInt it will show error.And it
2. What happens if the argument you want to pass is an object instead of aprimitive?
3. Can a method declare multiple return values? Or is there some way to return more
than onevalue?
5. Do I have to do something with the return value of a method? Can I Just Ignore it?
6. What about method parameters? How do the rules about local variables apply to them?
7. Do the following.
8. BE the compiler
Each of the Java files on this page represents a complete source file. Your job is to play
compiler and determine whether each of these files will compile. If they won't compile.
how would you fix them, and if they do compile. what would be their output?
10. A bunch of Java components, in full costume, are playing a party game ,Who am I.
They give you a clue, and you try to guess who they are, based on what they say.
Assume they always tell the truth about themselves. If they happen to say something
that could be true for more than one guy, then write down all for whom that sentence
applies. Fill In the blanks next to the sentence with the names of one or moreattendees.
Tonight's attendees:
Instance variable, argument, return, getter, setter ,encapsulation, public, private,
pass by value, method
11. MixedMessages
A short Java program is listed to your right. Two blocks of the program are missing.
Your challenge is to match the candidate blocks of code (below), with the output that
you'd see if the blocks were Inserted. Not all the lines of output will be used, and some of
the lines of output might be used more than once. Draw lines connecting the candidate
blocks of code with their matching command-line output.
VARIABLES
2. BE theJVM.
The Java file on this page represents a complete source rue. Your job is to play
JVM and determine what would be the output when the program runs?
3. CodeMagnets
A working Java program Is all scrambled up on the fridge . Can you reconstruct
the code snippets to make a working Java program that produces the output listed
below? Some of the curly braces fell on the floor and they were too small to pick up,
so feel free to add as many of those as you need!
4. A short Java program is listed below. One block of the program is missing. Your
challenge is to match the candidate block of code (on the left), with the output that
you'd see If the block were inserted. Not all the lines of output will be used, and some
of the lines of output might be used more than once. Draw lines connecting the
candidate blocks of code with their matching command-lineoutput.
CLASS AND
OBJECT
1. Each of the Java files on this page represents a complete source file. Your job is
to play compiler and determine whether each of these files will compile. If they
won’t compile, how would you fix them, and if they do compile, what would be
their output?
A
class TapeDeck {
boolean canRecord =
false; void playTape() {
System.out.println(“tape playing”);
}
void recordTape() {
System.out.println(“tape
recording”);
}
}
class TapeDeckTestDrive{
public static void main(String []
args) { t.canRecord = true;
t.playTape();
if (t.canRecord == true)
{ t.recordTape();
}
}
}
B.
class DVDPlayer {
boolean canRecord =
false; void recordDVD() {
System.out.println(“DVD recording”);
}
}
class DVDPlayerTestDrive {
public static void main(String [] args) {
DVDPlayer d = new
DVDPlayer(); d.canRecord =
true;
d.playDVD();
if (d.canRecord == true)
{ d.recordDVD();
}
}
}
2. CodeMagnets
A working java program is all scrambled up on the fridge . Can you reconstruct
the code snippets to make a working java program that produces the output
listed below? Some of the curly braces fell on the floor and they were too small to
pick up, so feel free to add as many of those as you need!
3. Pool Puzzle
Your job is to take snippets from the pool and place them into the blank lines in the
code. You may use the same snippet more than once, and you won't need to use all the
snippet. Your goal is to make classes that will compile and run and produce
outputlisted.
int x = 0;
while( ){
e1.hello();
if ( ){
e2.count = e2.count
+1;
}
if ( ){
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
class {
int = 0;
void {
System.out.println(“helloooo... “);
}
}
4. JavaCross
let's give your right brain something to do. It's your standard crossword, but almost all
of the solution words are from chapter 1.Just to keep you awake, we also threw in a few
(non-Java) words from the high-tech world.
Across
4. Command-line invoker
6. Back again?
8. Can't go bothways
9. Acronymfor your laptop'Spower
12. numbervariabletype
13. Acronym for achip
14. Saysomething
18. Quite a crew ofcharacters
19. Announce a new class ormethod
21. What'sa prompt good for?
Down
1. Not an integer (or your boat)
2. Comebackempty-handed
3. Openhouse
5. 'Things' holders
7. Until attitudes improve
10. Source code
consumer 11 . Can't pin
it down
13. Dept .of LAN jockeys
1S. Shocking modifier
16. Just gona have one
17.How to get things
done
20. Byte codeconsumer
CONSTRUCTOR
1. Why do you need to write a constructor if the compiler writes one foryou?
2. How can you tell a constructor from a method? Can you also have a method
that's the same name as theclass?
5. How could a private constructor ever be useful? Nobody could ever call It, so
nobody could ever make a newobject?
6. Match thefollowing.
3. We know how subclass gets to inherit a superclass method, but what if the super class
wants to use the subclass version of themethod?
4. In a subclass, what if I want to use both the superclass version and my overriding
subclass version of a method? In other words, I don't want to completely replace the
superclass version, I Just want to add more stuff toit.
5. Are there any practical limits on the levels of subclassing? How deep can yougo?
6. Hey, I just thought of something... if you don't have access to the source code for a
class, but you want to change the way a method of that class works, could you use
subclassing to do that? To extend the "bad" class and override the method with your
own bettercode?
7. Can you extend any class? Or is it like class members where if the class is private you
can't inheritit.
8. Why would you ever want to make a final class? What advantage would there be in
restricting a class from being subclassed?
9. Can you make a method final, without making the whole classfinal?
10. MixedMessages
A short Java program is listed below. One block of the program is missing! Your
challenge is to match the candidate block of code (on the left),with the output that You'd
see if the block were inserted. Not all the lines of output will be used, and some of the
lines of output might be used more than once . Draw lines connecting the candidate
blocks of code with their matching command-line output.
11. BE theCompiler
"Which of the A-B pairs of methods listed on the right, if inserted into the classes on the
left. would compile and produce the output shown? (The A method inserted into class
Monster, the B method inserted into classVampire.)
12. PoolPuzzle
2. Interface does not give you multiple inheritance because you can’t put any
implementation code in them. If all methods are abstract, what does an interface
really youbuy?
4. What if you make a concrete subclass and you need to override a method, but
you want the behavior in the superclass version of the method? in other words,
what if you don’t need to replace the method with an override, but you just want
to add to it with some additional specific code.
5. On the left you'll find sets of class diagrams. Your job is to turn these into
valid Java declaration. We did number 1 for you.
6. On the left you will find sets of class and interface declarations. Your job is to draw
the associated class diagrams on the right. What did the first one for you. Use a dashed
line for "implements" and a solid line for"extends".
7. PoolPuzzle
}
}
GARBAGE COLLECTOR
1. BE the
GarbageCompiler.
Which of the lines of code on the right, if added to the class on the left at point A,
would cause exactly one additional object to be eligible for the Garbage
Collector? (Assume that point A will call more methods) will execute for a long
time, giving the Garbage Collector time to do its stuff.)
2. In this code example, several new objects are created. Your challenge is to find
the object that is 'most popular; i.e. the one that has the most reference variables
referring to it. Then list how many total references there are for that object, and
what they are! We'll start by pointing out one of the new objects, and its
referencevariable
STATIC AND FINAL KEYWORDS
1. What if you try to call a non-static method from a static method, but the non-static
method doesn't use any instance variables. Will the compiler allowthat?
2. I could swear I've seen code that calls a static method using a reference variable Instead
of the classname.
3. A static method can't access a non-static variable. But can a non-static method access a
static variable?
4. Why would I want to make a class final? Doesn't that defeat the whole purpose ofOO?
5. Isn't it redundant to have to mark the methods final If the class isfinal?
6. BE thecompiler
The Java file on this page represents a complete program. Your job is to play with
compiler and determine whether this file will compile. If it won't compile. how would you
fix it. and if it does compile. what would be its output?
7. Your Job is to decide whether each of the following statements Is true orfalse.
1. To use the Math class, the first step is to make an instance ofit.:-
2. You can mark a constructor with the static keyword.:-
3. Static methods don't have access to instance variable state of the 'this' object.:-
4. It is good practice to call a static method using a reference variable.:-
5. Static variables could be used to count the instances of a class.:-
6. Constructors are called before static variables are initialized.:-
7. MAX_SIZE would be a good name for a static final variable.:-
8. A static initializer block runs before a class's constructorruns.:- _
9. If a class is marked final, all of its methods must be marked final.:-
10. A final method can only be overridden if its class is extended.:-
11. There is no wrapper class for boolean primitives.:-
12. A wrapper is used when you want to treat a primitive like an object.:-
1. Why does there have to be a full name? Is that the only purpose of apackage?
2. How does a full name really help? What's to prevent two people from giving a
class the same packagename?
3. Does import make my class bigger? Does it actually compile the imported class
or package into mycode?
5. The method draw() in NewCircle has been set as private. Class SamePackage is
in the same package as NewCircle. Will the class be able to use themethod?
package circle;
import
circle.NewCircle; Class
SamePackage
{
public static void main(String[] args)
{
NewCircle nc= new
NewCircle(); nc.draw();
}
}
package purchase;
public class
Employee
{
protected double age=35.00;
}
Company.java
import
purchase.Employee
public class Company
{
public static void main(String[] args)
{
Employee e= new Employee();
System.out.println("Age="+e.ag
e);
}
}
THREAD
A. start() C. run()
B. init(). D.go()
B. The program compiles fine,but it does not print anything because t does not
invoke the run()method.
C. The program compiles and runs fine and display test on theconsole.
A. The program will not compile because it does not implement the run()method.
B. The program will not compile because it does not contain abstractmethods.
D. None of theabove.
B. run-test
C. compilationfails
D. runtimeerror
6. Which of these method of Thread class is used to Suspend a thread for a period
oftime?
A. sleep()
B. terminate()
C. suspend()
D. stop()
7. Which function of pre defined class Thread is used to check weather current
thread being checked is stillrunning?
A. isAlive()
B. Join()
C. isRunning()
D. Alive()
A. yield()
B. sleep(longmsce)
C. go()
D. stop()
9. What notify() methoddo?
10. Which keyword when applied on a method indicates that only one thread
shouldexecute the method at atime?
A. final
B. synchronized
C. static