Java Paper Solution
1. Consider the following class
Public class A{
Public static int x=7;
Public int y=3;
Private float z =1.0;
A (int a, int b) {x=a; y=b;}
}
What is output from the following code?
A a = new A (5, 1);
A b = new A ();
a.x = 8;
b.y = 7;
System.out.println( “a.y=” + a.y );
System.out.println( “b.y=” + b.y );
System.out.println( “a.x=” + a.x );
System.out.println( “b.x=” + b.x );
System.out.println( “A.x=” + A.x );
a.y = 1
b.y= 7
a.x= 8
b.x= 7
A.x= 7
2. Write a java program to print a circle with the centre (250,250) and radius 60 and translate it 10 units in right direction.
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.Jcomponent;
public class frame1 extends Jcomponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g; // Recover Graphics2DEllipse C1= new Ellipse2D.Double(250, 250, 60, 60);
g2.draw(C1);
c1.translate(10,0);
g2.draw(C1);
}
}
3. Write a complete executable applet program which prints “DA-IICT”//DAIICT.html
<html>
<head> <title> Applet </title> </head>
<body>
<applet code="DAIICT.class" CodeBase="" width=300 height=400></applet>
</body>
</html>
//DAIICT.javaimport java.applet.*;
import java.awt.*;
public class DAIICT extends Applet
{
public void paint(Graphics g) {
g.drawString("DA-IICT",20,40);
}
}
4. What is OOP? Give its important features
A type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. To perform object-oriented programming, one needs an object-oriented programming language (OOPL). Java, C++ and Smalltalk are three of the more popular languages.
The following are the important features of OOP:
· Objects- They are instance of classes. It is a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Attributes and behavior of an object are defined by the class definition.
· Class- A class acts as a blue-print that defines the properties, states, and behaviours that are common to a number of objects. An object is an instance of the class.
· Abstraction -The process of exposing only the relevant and essential data to the users without showing unnecessary information.
Polymorphism – The ability of any entity to take multiple forms.
Encapsulation – The process of Preventing the data from unwanted access by binding of code and data in a single unit called object.
Inheritance – It promotes the reusability of code and eliminates the use of redundant code. It is the property through which a child class obtains all the features defined in its parent class. When a class inherits the common properties of another class, the class inheriting the properties is called a derived class and the class that allows inheritance of its common properties is called a base class
5. What is difference between a constructor and a method?
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
6. What is method overloading? Give an example.
Overloading is a mechanism in which we can use many methods having the same function name but can pass different number of parameters or different types of parameter.
For Example:-
int sum(int a,int b);
double sum(double a,double b); int sum(int 1,int b,int c);
That means, by overloading mechanism, we can handle different number of parameters or different types of parameter by having the same method name.
Following example explain the concept of overloading.
public class overDemo{
public static void main(String args[]){
System.out.println(“Sum of two integer “); Sum(10,20);
System.out.println(“Sum of two double numbers “);
Sum(10.5,20.4);
System.out.println(“Sum of three integer “);
Sum(10,20,30);
}
public static void Sum(int num1,int num2)
{
int ans=num1+num2; System.out.println(ans);
}
public static void Sum(double num1,double num2)
{
double ans=num1+num2; System.out.println(ans);
}
public static void Sum(int num1,int num2,int num3) {
int ans=num1+num2+num3; System.out.println(ans);
}
}
7. What is difference between static and non-static variables?
Static Variables:
· A static variable is associated with the class has only one copy per class but not for each object. An instance of a class does not have static variables.· Static variables can be accessed by static or instance methods
· Memory is allocated when the class is loaded in context area at run time.
Non-Static Variables:
· Non-static variables will have one copy each per object. Each instance of a class will have one copy of non-static variables.
· Instance variables can be accessed only by the instance methods.
· Instance variables are allocated at compile time.
8. What is a final class? Give an example of final variable.
Using the final keyword will turn any class into final. When you declare a class to be final, it can never be sub classed. This is usually done for security or performance reasons. A method is made final if it has an implementation that should not be changed and it is critical to the consistent state of the object.
//Program that illustrates final variable use in java using dayInWeek variable
//FinalVariableDemo.java
class FinalVariableDemo {
public static void main(String[] args) {
//Once created and initialized, its value can not be changed.final int dayInWeek = 7;
//Below statement will not compile. you can not change value of dayInWeek variable.
//dayInWeek = 6;
System.out.println("Number of days in a week = " + dayInWeek);
}
}
Output:
Number of days in a week = 7
9. Write statements in java to convert string str=”MTechICT” to “MTechDes” without replacing the complete string.
String str = "MTechICT";
String sub = str.substring(0,5) ; // now sub contains “MTech”
String des = "Des";
String str = sub + des; // now str contains “MTechDes”
10. Differentiate between OOP and procedural oriented programming.
Object Oriented Programming
Procedural Oriented Programming
1 In object oriented programming approach there is a collection of objects.
The procedural programming language executes series of procedures sequentially.
2
This is a bottom up programming approach. This is a top down programming Approach. 3
The procedural programming language executes series of procedures sequentially.
2
This is a bottom up programming approach. This is a top down programming Approach. 3
The main focus is on objects.
The major focus is on procedures or functions.
4
Data reusability is one of the important Feature of OOP. Data reusability is not possible.
5
Data binding can be done by making it private.
Data binding is not possible.
6
It is complex to implement.
It is simple to implement.
11. Why Java is called as internet language?
The major focus is on procedures or functions.
4
Data reusability is one of the important Feature of OOP. Data reusability is not possible.
5
Data binding can be done by making it private.
Data binding is not possible.
6
It is complex to implement.
It is simple to implement.
11. Why Java is called as internet language?
Java is called internet language because it is platform independent. Once written program can be run anywhere on any platform making java programs portable. Java is distributed, interpreted, robust, secure, architecture neutral, multi-threaded and dynamic.
12. Explain each word in the following statement: public static void main( String args[])
To execute a class, it must contain a valid main method.
Public static void main(String args[])
This line is for function void main().The main is of type public static void.
· Public -Public is a access mode of the main() by which the class visibility can be defined. Typically main must be declared as public.
· Static: To declare it as a class member.
· Void- Returns no value.
· Main()-It is the first method that automatically gets invoked when the program executed. The main method must always be defined as public to make it publicly accessible.· String- The parameter which is passed to main is String args[]. Hence String is a class name and args[] is an array which receives the command line arguments when the program runs.
· Args[]- parameter, is an array of class String. It provides access to command line parameters.
Comments
Post a Comment