You are on page 1of 32

Introduction to OOP and java language

• Is java completely object oriented?


– True/False
– Ans: False
• Objects are created on stack in java
– True/False
– False
• Objects are created on stack in C++
– True/False
– True and False (If new operator is used True else
False)
• The new operator allocates memory during compile-
time
– True/False
– False
• The finalize method is invoked more than once
by a Java virtual machine for any given object.
– True/False
– False
• JVM is platform independent True/False
– False
• The main method can be overloaded? True/False
– True
• What is Core usage of Java ?
• What is Abstraction ?
• If ‘wtBox’ class inherits from ‘Box’ , write valid
statements.
• Box b=new Box(); c) wtBox w=new wtBox();
• Box b1=new wtBox(); d) wtBox w1=new Box();
• e) Object obj=new Box();

Predict output

class A class B extends A{ int b; }


{ class TestTest
int b=10; {
private A() public static void main(String[] args)
{ {
this.b=7; A a=new B();
} System.out.println(a.f());
int f() }}
{
return b;
}
}

Compilation Error: private constructor


• access specifiers differences.
– private
– default
– protected
– public
Classes and Objects
• asked to write simple java code of employee class and
create objects
Given: What is the result?
15. public class Yippee { A. No output is produced.
16. public static void main(String [] 123
args) { B. No output is produced.
17. for(int x = 1; x < args.length; x++) 234
{ C. No output is produced.
18. System.out.print(args[x] + " "); 1234
19. } D. An exception is thrown at
20. } runtime.
21. } 123
and two separate command line E. An exception is thrown at
invocations: runtime.
java Yippee 234
java Yippee 1 2 3 4
Given: What is the result?
11. public static void main(String[] A. null
args) { B. zero
12. String str = "null"; C. some
13. if (str == null) { D. Compilation fails.
14. System.out.println("null"); E. An exception is thrown at runtime.
15. } else (str.length() == 0) {
16. System.out.println("zero");
17. } else {
18. System.out.println("some");
19. }
20. }

D
Given: A. Line 16
11. public void genNumbers() { B. Line 17
12. ArrayList numbers = new ArrayList(); C. Line 18
13. for (int i=0; i<10; i++) { D. Line 19
14. int value = i * ((int) Math.random()); E. The object is NOT a candidate for
15. Integer intObj = new Integer(value); garbage collection.
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }
Which line of code marks the earliest point
that an object referenced by intObj
becomes a candidate for
garbage collection?

D
• What is object,class,polymorphism,inheritance.
explain with example.

• In real world, an entity that has its state and behaviour


is known as object
– Ex: A Car is an object. It has states(name, color, model) and its
behaviour (changing gear and applying brakes)
• Class
– A class is a template or blue print that is used to create
objects.
• Difference between procedural and object oriented
programming

Procedural programming Object Oriented Programming

program is divided into small parts program is divided into parts


called functions. called objects
,Importance is not given to data but to Importance is given to the data rather
functions as well as sequence of actions than procedures or functions because it
to be done. works as a real world.
Data can move freely from function to objects can move and communicate
function in the system. with each other through member
functions.
To add new data and function is not so OOP provides an easy way to add new
easy. data and function.
Overloading is not possible. overloading is possible
public class TestString1 { What is the output?
2. public static void A. 42
main(String[] args) { B. 420
3. String str = "420"; C. 462
4. str += 42; D. 42042
5. System.out.print(str); E. Compilation fails.
6. } F. An exception is thrown at
7. } runtime.

D
Solution:
public int update(int quantity,int adjust){
quantity=quantity+adjust;
return quantity;
}

public void call Update( ) {


int quant=100;
quant=update(quant,320);
System.out.println("the quantity is " +quant);
}
INHERITANCE
• Which of the following inheritance is not supported by
java
– Simple
– Multi level
– Multiple
– Hierarchical
• How java supports Multiple Inheritance
– Through interfaces
• Explain how inheritance and polymorphism used in
mobiles?
Consider a Person is having multiple mobiles like
basic phone and smart phone. He uses basic phone to
send text and to add contacts and he uses smart
phone to tweet and to add contacts (Few services are
considered and more assumptions are allowed if time
permits to implement). He wanted to display contacts
added from basic phone and list the messages he
tweeted from smart phone. Write a java program to
simulate the above said services for a person.
• Difference between over loading and overriding
• What is static and dynamic polymorphism
• What is dynamic method dispatch?
• Why java doesn’t support multiple inheritance?
– diamond problem of multiple inheritance.
• How to perform multiple inheritance in java?
interface f1{ class Test7 implements f3{
public void m1(){
void m1(); System.out.println("Poly");}
} }
interface f2{ class Test7Demo{
public static void
void m1(); main(String[] args){
} Test7 t7=new Test7();
interface f3 extends
f1 f=t7;
f1, f2{ t7.m1();
} f.m1();
}
}
abstract class test4{ Output
static void printInfo(){
System.out.println("printin compilation error
g Info!!!!!!!!!"); Exception
} printing Info!!!!!!!!!
}
class Test4Demo{
public static void
main(String[] args){
test4.printInfo();}
}

printing Info!!!!!!!!!
Predict output
Predict output
public class Test
{
public static void main(String args[])
{ Test obj= null;
obj.printData();
obj.printName(“Sachin”);
}
private static void printData()
{System.out.println("Hello World!!!! ");
}
private void printName(String name)
{
System.out.print("Hello "+ name); }
}

Hello World!!!!!

NullPointerException
Interfaces
Solution:
interface Reloadable{
public void reload();
}
class Edit{
public void edit(){/* Edit Here*/}
}
interface Displayable
extends Reloadable {
public void display();
}
interface Rideable { public class Camel implements Rideable {
String getGait(); int weight = 2;
} public static void main(String[] args) {
new Camel().go(8);
}
void go(int speed) {
++speed;
weight++;
int walkrate = speed * weight;
System.out.print(walkrate + getGait());
}
String getGait() {
return " mph, lope";
}
}

Compilation Error
Given A. final
11. public interface Status { B. static
12. /* insert code here */ int C. native
MY_VALUE = 10; D. public
13. } E. private
Which three are valid on line F. abstract
12? G. protected

An interface can extend abstract class?

D, B A
Solution:
interface Reloadable{
public void reload();
}
class Edit{
public void edit(){/* Edit Here*/}
}
interface Displayable
extends Reloadable {
public void display();
}
Given the following, C. abstract class Class2
1. interface Base { implements Base { }
2. boolean m1 (); D. abstract class Class2
3. byte m2(short s); implements Base {
4. } public boolean m1() { return
(true); } }
Which code fragments will compile?
(Choose all that apply.) E. class Class2 implements
A. interface Base2 implements Base { } Base {
B. abstract class Class2 extends Base { boolean m1() { return false; }
public boolean m1() { return true; } }
byte m2(short s) { return 42; } }

C&D
Exception Handling
1. What type exceptions do not require try-catch blocks?

2. throw key word can be used to throw object of any class from try block?.

3. The block of code which will be executed, if there is a run time error or not

try {int a=4/0;}


catch(Exception e)
{System.out.println(“catch msg”);}
catch(ArithMeticException e1){ System.out.println “Divide Zero”);}
finally
{ System.out.println(“finally msg”);}

Unchecked, False, finally, compilation error


class Test1{ What is the result?
public static void parse(String str) { A. 0.0
try {
float f = Float.parseFloat(str);
B. Compilation fails.
} catch (NumberFormatException C. A ParseException is thrown
nfe) { by the parse method at
f = 0; runtime.
} finally { D. A NumberFormatException
System.out.println(f);
}
is thrown by the parse method
} at runtime.
public static void main(String[] args)
{
parse("invalid");
}
}

COMPILATION Fails
Given: Under which three circumstances
31. // some code here will the code on line 37 be executed?
(Choose three.)
32. try { A. The instance gets garbage
33. // some code here collected.
34. } catch (SomeException se) B. The code on line 33 throws an
{ exception.
35. // some code here C. The code on line 35 throws an
exception.
36. } finally { D. The code on line 31 throws an
37. // some code here exception.
38. } E. The code on line 33 executes
successfully.

B&E
class test2{ What is the result?
public static void main(String[] args) A. test
{
try {
B. Exception
args = null; C. Compilation fails.
args[0] = "test"; D. NullPointerException
System.out.println(args[0]);
}
catch (Exception ex) {
System.out.println("Exception");
}
catch (NullPointerException npe) {

System.out.println("NullPointerExce
ption");
} }}

C
try { What is the result if a
34. // some code here NullPointerException occurs
35. } catch on line 34?
(NullPointerException e1) { A. c
36. System.out.print("a"); B. a
37. } catch (RuntimeException C. ab
e2) { D. ac
38. System.out.print("b"); E. bc
39. } finally { F. abc
40. System.out.print("c");
41. }

ac

You might also like