Pre-Placement Training - Java
Assignment 5 - Object Oriented Programming
● Easy Level questions (Attempt any 2) -
○ Inheritance 1 [ YT Link ]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class Animal{
void walk()
{
[Link]("I am walking");
}
}
class Bird extends Animal
{
void fly()
{
[Link]("I am flying");
}
void sing() {
[Link]("I am singing");
}
}
public class Solution{
public static void main(String args[]){
Bird bird = new Bird();
[Link]();
[Link]();
[Link]();
}
}
○ Inheritance 2
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
//Write your code here
class Arithmetic {
public int add(int a, int b){
return a+b;
}
}
class Adder extends Arithmetic{
public class Solution{
public static void main(String []args){
// Create a new Adder object
Adder a = new Adder();
// Print the name of the superclass on a new line
[Link]("My superclass is: " +
[Link]().getSuperclass().getName());
// Print the result of 3 calls to Adder's
`add(int,int)` method as 3 space-separated integers:
[Link]([Link](10,32) + " " + [Link](10,3)
+ " " + [Link](10,10) + "\n");
}
}
● Medium Level Questions (Attempt any 2) -
○ Method Overriding 2
import [Link].*;
import [Link].*;
class BiCycle{
String define_me(){
return "a vehicle with pedals.";
}
}
class MotorCycle extends BiCycle{
String define_me(){
return "a cycle with an engine.";
}
MotorCycle(){
[Link]("Hello I am a motorcycle, I am "+
define_me());
BiCycle bi=new BiCycle();
String temp=bi.define_me(); //Fix this line
[Link]("My ancestor is a cycle who is "+
temp );
}
}
class Solution{
public static void main(String []args){
MotorCycle M=new MotorCycle();
}
}
○ Iterator [ YT Link ]
import [Link].*;
public class Main{
static Iterator func(ArrayList mylist){
Iterator it=[Link]();
while([Link]()){
Object element = [Link]();
if(element instanceof Integer )//Hints: use
instanceof operator
continue;
break;
}
return it;
}
@SuppressWarnings({ "unchecked" })
public static void main(String []args){
ArrayList mylist = new ArrayList();
Scanner sc = new Scanner([Link]);
int n = [Link]();
int m = [Link]();
for(int i = 0;i<n;i++){
[Link]([Link]());
}
[Link]("###");
for(int i=0;i<m;i++){
[Link]([Link]());
}
Iterator it=func(mylist);
while([Link]()){
Object element = [Link]();
[Link]((String)element);
}
}
}