×
The question isn’t who is going to let me; it’s who is going to stop me.
--Your friends at LectureNotes
Close

Solution to Previous Year Exam Questions for Advanced Java Programming - AJP of REC by Adarsh Dash

  • Advanced Java Programming - AJP
  • 2019
  • PYQ Solution
  • Biju pattnaik University - REC
  • Computer Science Engineering
  • B.Tech
  • 2495 Views
  • 16 Offline Downloads
  • Uploaded 5 months ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

Q1. Answer the following Questions: a. What is the Signature of a method? How it is written in Java? Sol: Function signature contains the function name, return type of the function and the list of arguments. In Java, it is written as: <returntype> functionName(arg. List) Ex: int facto(int n) b. Assume double [][] x= new double [4][5] what are the x.length and x[2].length? Sol: X.length = 4 and x[2].length = 5 c. Suppose s1 and s2 are two strings what is the result of following code? s1.equals(s2)==s2.equals(s1) sol: True d. What are the 2 ways to overriding methods? Sol: Ways to override a method:  By using super key word  By Dynamic Method dispatching e. Give two examples for Heavy weight components? sol: components in Swing (JWindow, JDialog, JApplet, and JFrame) are heavyweight f. What should be the code to draw a circle with r 20 centered at (50,50) using graphic g object sol: To draw a circle: g.drawOval(50,50,20) g. Difference between abstract class and abstract method? Sol: Abstract Class: Abstract class forms a skeleton. It contains some abstract methods. We cannot have objects of abstract classes. Abstract Method: Abstract Methods are non-body methods. They need to be overridden in the derived classes. h. What exception does the code throw? public class Test { Public static void main(String a[]) { System.out.println(1/0); } } Sol : ArithmaticException

Text from page-2

i. What happen if an applet does not contain an init()? sol: If an applet does not contain init() method then it will not be loaded in the browser. j. Statement required for a server to listen to client request? Sol: ServerSocket se = new ServerSocket(2222) Socket clientSocket= se.accept(); All the clients will be communicating with the server using a port number. Q2. Differentiate between method overriding and overloading? 1. Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime. 2. Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class. 3. The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required. Overriding is all about giving a specific implementation to the inherited method of parent class. 4. Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods. 5. Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime. 6. private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one private/final methods of same name but a child class cannot override the private/final methods of their base class. 7. Return type of overloaded methods should be same however in case of method overriding the return type of overriding method can be different from overridden method. 8. Argument list should be different while doing method overloading. Argument list should be same in method Overriding. Overloading example //A class for adding upto 5 numbers class Sum { int add(int n1, int n2) { return n1+n2; } int add(int n1, int n2, int n3)

Text from page-3

{ return n1+n2+n3; } int add(int n1, int n2, int n3, int n4) { return n1+n2+n3+n4; } int add(int n1, int n2, int n3, int n4, int n5) { return n1+n2+n3+n4+n5; } public static void main(String args[]) { Sum obj = new Sum(); System.out.println("Sum of two numbers: "+obj.add(20, 21)); System.out.println("Sum of three numbers: "+obj.add(20, 21, 22)); System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23)); System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24)); } } Output: Sum of two numbers: 41 Sum of three numbers: 63 Sum of four numbers: 86 Sum of five numbers: 110 Here we have 4 versions of same method add. We are overloading the method add()here. Overriding example package beginnersbook.com; class CarClass { public int speedLimit() { } return 100;

Text from page-4

} class Ford extends CarClass { public int speedLimit() { return 150; } public static void main(String args[]) { CarClass obj = new Ford(); int num= obj.speedLimit(); System.out.println("Speed Limit is: "+num); } } Q3. Define Inheritance and types of inheritance supported by java? Creating a new class from existing class is called as inheritance. Where we will be inheriting properties and methods from a super class to a sub class. Inheritance can be classified to 5 types. 1. Single Inheritance 2. Hierarchical Inheritance 3. Multi Level Inheritance 4. Hybrid Inheritance 5. Multiple Inheritance 1. Single Inheritance when a single derived class is created from a single base class then the inheritance is called as single inheritance. Single Inheritance Example Program

Lecture Notes