You are on page 1of 67

Mailam Engineering College, Mailam.

Department of Information Technology


(CS2311/U-IV)

UNIT IV
Syllabus
Introduction to JAVA, byte code, virtual machines – objects – classes – Java doc
– Packages – Arrays – Strings.
PART - A
1. What is meant by Java?
Object Oriented Multithreaded High Level Programming Language
developed by sun Microsystems in 1991.
2. What is meant by Platform?
Platform is the hardware or system software environment in which your
program runs. Most platforms are described as a combination of hardware and
operating system.
3. Java is platform independent language. Justify.
Platform is the hardware or system software environment in which your
program runs. Moreover java language run by any operating system, thats why
java is called platform independent languages.
4. What is meant by Java Application?
An Application is a program that runs on your computer, under the
operating system of that system
5. What is meant by Java Applet?
An Applet is an application designed to be transmitted over the internet
and executed by a Java-Compatible web browser.
6. Define JDK.
JDK(Java Development Kit). JDK is a software package from Sun
Microsystems. Latest version of JDK is 1.5. This software package contains
tools.
7. What are the JDK Tools
 Javac- Java Compiler
 Java –Java interpreter
 Jdb – Java Debugger
 Javap- Java Disassembler
 Javadoc- Java Documentation

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 1
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 Javah - Java Header file Generator


 The Appletviewer
8. What are features of java
 Simple
 Object Oriented
 Distributed
 Interpreted
 Robust
 Secure
 Architecture-Neutral
 Portable
 High Performance
 Multithreaded
 Dynamic Language
9. What are features does not supported by java?
 Goto statement
 Multiple inheritance
 Operator overloading
 Structures and Unions allowed
 Pointers
10. What are features supported by java
o Automatic memory management
o Multithreaded programs
11. Define Java Character Set.
o Alphabets
o Digits
o Special characters
o Java uses Unicode character set.
12. What is meant by Java Class Defintion?
A java program contains 2 parts. They are
 A class definition that encloses the entire program

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 2
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 A main() method that contains the body.


Every java program should contain atleast one class.
13. What is meant by bytecode in java?
Java compiler after compiling the program creates a new file referred as
the class file, which contains a special code referred as the bytecode. It is
similar to machine language, but unlike machine language, java byte code is
exactly the same on every platform.
14. Define JVM.
It is an abstract computing machine, having an instruction and memory,
which is used to implement the java program language. The JVM is responsible
for cross-platform portability of java.
15. Define API.
Java API are libraries of compiled code that you can use in your
programs. They let you add readymade and customizable functionality to save
your programming time.
16. Define Garbage Collection in Java?
Garbage Collection also referred as automatic memory management.
Periodically frees the memory used by objects that are no longer needed. The
garbage collector in java scans dynamic memory areas for objects and marks
those that are referenced. After all possible paths to objects are investigated the
unreferenced objects are freed.
17. How multiple inheritance is achieved in java?
Java does not support multiple inheritance. It is achieved by the use
interface.
18. State the use of super keyword in java (Nov/ Dec 2011)
The super keyword is used to access a member of an immediate base
class, from a derived class.
19. Mention the various access levels supported in java
o Public
o Protected
o Private

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 3
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

o Package
20. Define method overloading.
Java enables 2 or more methods with same name but with different
signatures. The signature includes the number of type, and sequence of the
arguments passed to a method. The capability to overload a method is referred
to as overloading methods.
22. What is a class?
 Collection of objects.
 A class is a blueprint, or prototype, that defines the variables and the
methods common to all objects of a certain kind.
23. What is an object?
 An object is a software bundle of variables and related methods.
 An instance of a class depicting the state and behavior at that particular
time in real world.
24. What is a method?
Encapsulation of a functionality which can be called to perform specific
tasks.
25. What is encapsulation? Explain with an example.
Encapsulation is the term given to the process of hiding the
implementation details of the object. Once an object is encapsulated, its
implementation details are not immediately accessible any more. Instead they
are packaged and are only indirectly accessible via the interface of the object.
26. What is inheritance? Explain with an example.
Inheritance in object oriented programming means that a class of objects
can inherit properties and methods from another class of objects.
27. What are the different types of modifiers?
There are access modifiers and there are other identifiers. Access
modifiers are public, protected and private. Other is final and static.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 4
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

28. What are the access modifiers in Java?


There are 3 access modifiers. Public, protected and private, and the
default one if no identifier is specified is called friendly, but programmer cannot
specify the friendly identifier explicitly.
29. What is a wrapper class? (Nov/ Dec 2011)
They are classes that wrap a primitive data type so it can be used as a
object
30. What is a static variable and static method? What's the difference
between two?
The modifier static can be used with a variable and method. When
declared as static variable, there is only one variable no matter how instances
are created, this variable is initialized when the class is loaded. Static method
do not need a class to be instantiated to be called, also a non-static method
cannot be called from static method.
31. What is interface?
Interface is a contact that can be implemented by a class , it has method
that need implementation.
32. What is method overloading?
Overloading is declaring multiple methods with the same name, but with
different argument list.
33. What is the difference between an array and a vector?
Numbers of elements in an array are fixed at the construction time,
whereas the number of elements in vector can grow dynamically.
34. What is a constructor?
In Java, the class designer can guarantee initialization of every object by
providing a special method called a constructor. If a class has a constructor,
Java automatically calls that constructor when an object is created, before
users can even get their hands on it. So initialization is guaranteed.
35. What is casting?
Conversion of one type of data to another when appropriate. Casting
makes explicitly converting of data.
36. What is the difference between final, finally and finalize?

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 5
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

The modifier final is used on class variable and methods to specify


certain behaviors explained above. And finally is used as one of the loop in the
try catch blocks, It is used to hold code that needs to be executed whether or
not the exception occurs in the try catch block. Java provides a method called
finalize( ) that can be defined in the class. When the garbage collector is ready
to release the storage ed for your object, it will first call finalize( ), and only on
the next garbage-collection pass will it reclaim the objects memory. So finalize(
), gives you the ability to perform some important cleanup at the time of
garbage collection.
37. What's the difference between an interface and an abstract class?
(Nov/Dec 2012)
An abstract class may contain code in method bodies, which is not
allowed in an interface. With abstract classes, you have to inherit your class
from it and Java does not allow multiple inheritance. On the other hand, you
can implement multiple interfaces in your class.
38. How could Java classes direct program messages to the system
console, but error messages say to a file?
The class System has a variable out that represents the standard output,
and the variable err that represents the standard error device. By default, they
both point at the system console. This how the standard output could be re-
directed:
Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st);
System.setOut(st);
39. Write a simple program in java.
Import java.io.*;
Public class sample
{
Public staic void main(String args[])
{
System.out.println(“Java Programming”);
}
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 6
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

40. What is an Array?


Array: Array is the most important thing in any programming language.
By definition, array is the static memory allocation. It allocates the memory for
the same data type in sequence. It contains multiple values of same types. It
also stores the values in memory at the fixed size. Multiple types of arrays are
used in any programming language such as: one - dimensional, two -
dimensional or can say multi - dimensional.
41. How can you declare an array?
int num[ ]; or int num = new int[2];
Sometimes user declares an array and its size simultaneously. You may
or may not be define the size in the declaration time. Such as:
int num[ ] = {50,20,45,82,25,63};
42. Write a java program for sorting of numbers.
import java.util.*;
public class array{
public static void main(String[] args){
int num[] = {50,20,45,82,25,63};
int l = num.length;
int i,j,t;
System.out.print("Given number : ");
for (i = 0;i < l;i++ ){
System.out.print(" " + num[i]);
}
System.out.println("\n");
System.out.print("Accending order number : ");
Arrays.sort(num);
for(i = 0;i < l;i++){
System.out.print(" " + num[i]);
}
}
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 7
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Output of the program:


C:\chandan>javac array.java
C:\chandan>java array
Given number : 50 20 45 82 25 63
Ascending order number : 20 25 45 50 63 82
43. Write a java program for find out average of an array.
public class ArrayAverage{
public static void main(String[] args) {
double nums[]={1.0,2.3,3.4,4.5,40.5};
double result=0.0;
int i=0;
for(i=0; i < nums.length; i++){
result=result + nums[i];
}
System.out.println("Average is =" + result/nums.length);
}
}
44. What are 2 dimensional arrays in java? Give an Example.
Two-dimensional arrays are defined as "an array of arrays". Since an
array type is a first-class Java type, we can have an array of ints, an array of
Strings, or an array of Objects. For example, an array of ints will have the type
int[]. Similarly we can have int[][], which represents an "array of arrays of ints".
Such an array is said to be a two-dimensional array.
The command
int[ ][ ] A = new int[3][4];
45. Write a java program for comparing 2 strings.
import java.lang.*;
import java.io.*;
public class CompString{
public static void main(String[] args) throws IOException{
System.out.println("String equals or not example!");
BufferedReader bf = new BufferedReader(new InputStreamReader(Syste

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 8
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

m.in));
System.out.println("Please enter first string:");
String str1 = bf.readLine();
System.out.println("Please enter second string:");
String str2 = bf.readLine();
if (str1.equals(str2)){
System.out.println("The given string is equals");
}
else{
System.out.println("The given string is not equals");
}
}}
46. How can u replace a single character in a String?
To replace a character with the given character in sting first convert the
string into char array. Use getChars(int scrStart, int scrEnd, char[] destChar,
int destStart) method for converting a string into an array of characters. Then
use while loop for comparing the "oldChar" ( character name for replace) to be
change with the new character of the array. If any match find then replace the
"oldChar" with newChar (character name to replace) and set flag =1. To convert
charArray into string, pass "charArray" into String class. getChars(int
scrStart, int scrEnd, char[] destChar, int destStart): This method returns an
array of characters from a string. We are passing four parameter into this
method. First parameter scrStart is the starting point while second parameter
scrEnd is the end point of the source string to convert the string into a char
array. The destChar is the destined array which stores all the characters. The
destStart is starting index to store the characters.
47. Write a java program for find out string length.
import java.lang.*;
import java.io.*;
public class StringLength{
public static void main(String[] args) throws IOException{
System.out.println("String lenght example!");

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 9
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

BufferedReader bf = new BufferedReader(new InputStreamReader(Syste


m.in));
System.out.println("Please enter string:");
String str = bf.readLine();
int len = str.length();
System.out.println("String lenght : " + len);
}
}
48. What is a command line argument?
We are taking a sequence of character from command line at run time.
Store these strings into an array string. Show these strings on console as
output. If user forget enter string at run time on command line argument then
the message "No values has been entered at the command line" will displayed
on screen. To check the user enter string or not at run time we just compare
length of argument values .If value is greater than one then display the values
on screen else so message "No values has been entered at the command line".
public class ArgumentPassingExample{
public static void main(String[] args){
int num=args.length;
String s[]=new String[num];
if(num>0){
System.out.println("The values enter at argument command line are:");
for (int i = 0; i <num ; i++)
{
System.out.println("Argument " + (i + 1) +
" = " + args[i]);
}
}
else{
System.out.println("No values has been entered at the command line.");
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 10
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

}
C:\convert\rajesh\completed>javac ArgumentPassingExample.java
C:\convert\rajesh\completed>java ArgumentPassingExample rajesh
kumar rahul
The values enter at argument command line are:
Argument 1 = rajesh
Argument 2 = kumar
Argument 3 = rahul
49. What is String Buffer in Java?
The StringBuffer class is used to represent characters that can be
modified. This is simply used for concatenation or manipulation of the strings.
StringBuffer is mainly used for the dynamic string concatenation which
enhances the performance. A string buffer implements a mutable sequence of
characters. A string buffer is like a String, but can be modified. At any point in
time it contains some particular sequence of characters, but the length and
content of the sequence can be changed through certain method calls.
50. Write any 4 methods available in String Buffer Class.
substring()
This is the substring() function which is used to get the sub string from
the buffered string from the initial position to end position (these are fixed by
you in the program).
deleteCharAt()
This is the deleteCharAt() function which is used to delete the specific
character from the buffered string by mentioning that's position in the string.
length()
This is the length() function is used to finding the length of the buffered
string.
delete()
This is the delete() function is used to delete multiple character at once
from n position to m position (n and m are will be fixed by you.) in the buffered
string.
51. What is Package?

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 11
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

A Java package is a mechanism for organizing a group of related files in


the same directory and having each class file in a package directive with that
directory name at the top of the file. Programmers also use package convention
to organize classes that belong to the same files or providing similar
functionality.
52. What are all the features of Java Package?
Features of a Java package
 The protected members of the classes can be accessed by each other in
the same package.
 It resolves the naming collision for the types it contains.
 A package may have the following types.
 Interfaces
 Classes
 Enumerated types
 Annotations
53. What are all the accesses protections used in Java Packages?
 No modifier (default): In case of no modifier, the classes and members
specified in the same package are accessible to all the classes inside the
same package.
 public: The classes, methods and member variables under this specifier can
be accessed from anywhere.
 protected: The classes, methods and member variables under this modifier
are accessible by all subclasses, and accessible by code in same package.
 private: The methods and member variables are accessible only inside the
class.
54. What is Naming convention of packages?
A hierarchical naming pattern is used for java packages, with levels
separated by dots in the hierarchy. The packages that come lower in the
naming hierarchy are called "sub package" of the corresponding package higher
in the hierarchy. Java uses the package naming conventions in order to avoid
the possibility of source file having the same name. The naming convention

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 12
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

defines how to create a unique package name, so that packages that are widely
used with unique namespaces. This allows packages to be easily managed. In
general, we starts a package name begins with the order from top to bottom
level. Package names should be in lowercase characters whenever possible.
55. What is Java – Doc Commends?
Commenting in Java can take two forms, typical comments that can be
found in numerous other programming languages like C/C++; namely single
line comments and multiline comments. Then there are Java doc comments
that are unique to the Java language. In this section we will discuss why
commenting is needed and look at how to provide the standard single line and
multi-line comments in Java.
56. What is the need for comments in java?
Some of the reasons for using comments are:
 Often comments are added at the start of each source file to give a
description of what source the file contains and copyright information.
 The fields and methods of a class are often given brief comments that
describe their purpose and what they do.
 Complex code is often commented heavily to make it clearer and easier to
understand.
57. What are all the types of comments available in java?
Single Line Comments
Single line comments are used to add a very brief comment within some
code, often a long or complex method. They begin with a double forward slash
(//) and end with the end of line or carriage return. As an example consider:
private static String name = "Guys"; //The name to print

Multi Line Comments


If a comment is going to span across more than one line then a multi-line
comment should be used. These are often useful for providing more in-depth
information. They start with a forward slash followed by an asterisk (/*) and
end with an asterisk followed by a forward slash (*/). Consider:

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 13
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

/* Getter method provides public access in read only fashion.


This function returns the port number. */
int getPort() { ... }
58. What is abstract class? (Nov/Dec 2013)
Abstract class is a class that needs to be extended and its methods
implemented, a class has to be declared abstract if it has one or more abstract
methods.
59. What is the purpose of the getBytes( ) method?
This method has following two forms:

 getBytes(String charsetName): Encodes this String into a sequence of


bytes using the named charset, storing the result into a new byte array.

 getBytes(): Encodes this String into a sequence of bytes using the


platform's default charset, storing the result into a new byte array.

60. List all data types in java.

Data Type Default Value (for fields)

byte 0

short 0

int 0

long 0L

float 0.0f

double 0.0d

char '\u0000'

String (or any object) null

boolean false

61. Why is java language called as „robust‟?


Java is Robust because it is highly supported language. It is portable across
many Operating systems. Java also has feature of Automatic memory
management and garbage collection. Strong type checking mechanism of Java

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 14
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

also helps in making Java Robust. Bugs, especially system crashing bugs, are
very rare in Java.

PART - B
1. Discuss about JAVA HISTORY.
JAVA HISTORY
 Java is a general-purpose, object-oriented programming language
developed by Sun Microsystems of USA in 1991.
 Originally called Oak by James Gosling (one of the inventor of the
language).
 Java was invented for the development of software for consumer
electronic devices like TVs, tosters, etc.
 The main aim had to make java simple, portable and reliable.
 Java Authors: James , Arthur Van , and others

Following table shows the year and beginning of Java.


Year Progress
Sun decided to developed software that could be used for
1990
electronic devices. And the project called as Green
Project head by James Gosling.
1991 Announcement of a new language named “Oak”
The team verified the application of their new language
1992 to manage a list of home appliances using a hand held
device.

The World Wide Web appeared on the Internet and


1993 transformed the text-based interface to a graphical rich
environment.

The team developed a new Web browsed called “Hot Java” to


1994
locate and run Applets.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 15
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Oak was renamed to Java, as it did not survive “legal”


1995 registration. Many companies such as Netscape and
Microsoft announced their support for Java.

Java language is now famous for Internet


1996 programming as well as a general purpose OO
language.

1997 Sun releases Java Development Kit(JDK 1.1)

1998 Sun releases Software Development Kit (SDK 1.2)

Sun releases Java 2 platform Standard Edition (J2SE) and


1999
Enterprise Edition(J2EE).
2000 J2SE with SDK 1.3 was released.
2002 J2SE with SDK 1.4 was released.
2004 J2SE with JDK 5.0 was released.

2. Explain in detail about Java features (Nov/Dec 2013)


JAVA FEATURES:
 The Java is an object oriented programming language developed by Sun
Microsystems of USA in 1991.
 Java is first programming language which is not attached with any
particular hardware or operating system.
 Program developed in Java can be executed anywhere and on any system.
 Features of Java are as follows:
1. Compiled and Interpreted
2. Platform Independent and portable
3. Object- oriented
4. Robust and secure
5. Distributed
6. Familiar, simple and small

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 16
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

7. Multithreaded and Interactive


8. High performance
9. Dynamic and Extensible
1. Compiled and Interpreted

 Basically a computer language is either compiled or interpreted. Java


comes together both these approach thus making Java a two-stage
system.
 Java compiler translates Java code to Bytecode instructions and Java
Interpreter generate machine code that can be directly executed by
machine that is running the Java program.
2. Platform Independent and portable
 Java supports the feature portability.
 Java programs can be easily moved from one computer system to
another and anywhere. Changes and upgrades in operating systems,
processors and system resources will not force any alteration in Java
programs.
 This is reason why Java has become a trendy language for programming
on Internet which interconnects different kind of systems worldwide.
 Java certifies portability in two ways. First way is, Java compiler
generates the bytecode and that can be executed on any machine.
Second way is, size of primitive data types are machine independent.
3. Object- oriented
 Java is truly object-oriented language.
 In Java, almost everything is an Object. All program code and data exist
in objects and classes.
 Java comes with an extensive set of classes; organize in packages that
can be used in program by Inheritance. The object model in Java is
trouble-free and easy to enlarge.
4. Robust and secure
 Java is a most strong language which provides many securities to make
certain reliable code.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 17
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 It is design as garbage –collected language, which helps the


programmers virtually from all memory management problems.
 Java also includes the concept of exception handling, which detain
serious errors and reduces all kind of threat of crashing the system.
 Security is an important feature of Java and this is the strong reason
that programmer use this language for programming on Internet.
 The absence of pointers in Java ensures that programs cannot get right
of entry to memory location without proper approval.
5. Distributed
 Java is called as Distributed language for construct applications on
networks which can contribute both data and programs.
 Java applications can open and access remote objects on Internet easily.
 That means multiple programmers at multiple remote locations to work
together on single task.
6. Simple and small
 Java is very small and simple language.
 Java does not use pointer and header files, goto statements, etc. It
eliminates operator overloading and multiple inheritance.
7. Multithreaded and Interactive
 Multithreaded means managing multiple tasks simultaneously.
 Java maintains multithreaded programs. That means we need not wait
for the application to complete one task before starting next task.
 This feature is helpful for graphic applications.
8. High performance
 Java performance is very extraordinary for an interpreted language,
majorly due to the use of intermediate bytecode.
 Java architecture is also designed to reduce overheads during runtime.
The incorporation of multithreading improves the execution speed of
program.
9. Dynamic and Extensible
 Java is also dynamic language.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 18
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 Java is capable of dynamically linking in new class, libraries, methods


and objects.
 Java can also establish the type of class through the query building it
possible to either dynamically link or abort the program, depending on
the reply.
3. Distinguish between or Compare contrast between Java and C++
JAVA C++
C++ is basically C with Object-
Java is true Object oriented language.
oriented extension.

Java does not support operator


overloading. C++ supports operator overloading.

It supports labels with loops and


It supports goto statement.
statement blocks

Java does not have template classes as


C++ has template classes.
in C++.

Source code can be written to be


Java compiled into byte code for the
platform independent and written to
Java Virtual Machine. The source code
take advantage of platform. C++
is independent on operating system.
typically compiled into machine code.

Java does not support multiple


C++ supports multiple inheritance of
inheritance of classes but it supports
classes.
interface.

Runs in a protected virtual machine. Exposes low-level system facilities.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 19
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Java does not support global variable.


C++ support global variable.
Every variable should declare in class.

Java does not use pointer. C++ uses pointer.

It Strictly enforces an object oriented It Allows both procedural programming


programming paradigm. and object oriented programming.

There are no header files in Java. We have to use header file in C++.

4. With the pictorial representation explain the concept of Java Virtual


Machine? (Nov/Dec 2011)
Java Virtual Machine
 All language compilers translate source code into machine code for a specific
computer. Java compiler also does the same thing.
 But, Java compiler produces an intermediate code known as bytecode
for a machine that does not exist. This machine is called the Java
Virtual Machine and it exists only inside the computer memory.
 It is a simulated computer within the computer and does all major functions
of a real computer.
 The following diagram illustrates the process of compiling a Java program
into bytecode which is also referred to as virtual machine code.

Java Program Java Compiler Virtual Machine

Source Code Byte Code

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 20
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 The virtual machine code is not machine specific. The machine specific code
(known as machine code) is generated by the Java interpreter by acting as
an intermediary between the virtual machine and the real machine.

Byte Code Java Interpreter Machine Code

Virtual Machine Real Machine

Note: [Remember that the interpreter is different for different machines]

 The following diagram illustrates how Java works on a typical computer.


 The Java object framework (Java API) acts as a intermediary between the
user programs and a virtual machine which in turn acts as the intermediary
between the operating system and Java object framework.

Real Machine
Operating System
Java Virtual Machine
Java Object Framework (API)
Compiler Interpreter
User Application Programs

User

5. Write short note on Java Environment. (Nov/Dec 2011)


JAVA ENVIRONMENT
 Java environment includes a number of development tools, classes and
methods. The development tools are part of the system known as Java
Development Kit (JDK) and the classes and methods are part of the Java

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 21
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Standard Library (JSL), also known as the Application Programming


Interface (API).
 Java Development kit (JDK) – The JDK comes with a set of tools that are
used for developing and running Java program. It includes:
1. Appletviewer( It is used for viewing the applet)
2. Javac(It is a Java Compiler)
3. Java(It is a java interpreter)
4. Javap(Java diassembler,which convert byte code into program
description)
5. Javah(It is for java C header files)
6. Javadoc(It is for creating HTML document)
7. Jdb(It is Java debugger)
For compiling and running the program we have to use following
commands:
a) javac (Java compiler)
 In java, we can use any text editor for writing program and then save
that program with “.java” extension.
 Java compiler convert the source code or program in bytecode and
interpreter convert “.java” file in “.class” file.
Syntax:
C:\javac filename.java
If my filename is “abc.java” then the syntax will be
C:\javac abc.java
b) java(Java Interpreter)
 As we learn that, we can use any text editor for writing program and then
save that program with “.java” extension.
 Java compiler convert the source code or program in bytecode and
interpreter convert “.java” file in “.class” file.
Syntax:
C:\java filename
If my filename is abc.java then the syntax will be
C:\java abc

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 22
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

6. Write short note on Simple Java program.


class FirstProgram
{
public static void main(String args[])
{
System.out.println(“This is my first program”);
}
}
 The file must be named “FirstProgram.java” to equivalent the class
name containing the main method.
 Java is case sensitive. This program defines a class called
“FirstProgram”.
 A class is an object oriented term. It is designed to perform a specific
task.
 A Java class is defined by its class name, an open curly brace, a list of
methods and fields, and a close curly brace.
 The name of the class is made of alphabetical characters and digits
without spaces, the first character must be alphabetical.
 The line “public static void main (String [] args )” shows where the
program will start running. The word main means that this is the main
method –The JVM starts running any program by executing this method
first.
 The main method in “FirstProgram.java” consists of a single statement
System.out. println ("This is my first program");
 The statement outputs the character between quotes to the console.
Above explanation is about how to write program and now we have to learn
where to write program and how to compile and run the program.
For this reason, the next explanation is showing the steps.
1. Edit the program by the use of Notepad.
2. Save the program to the hard disk.
3. Compile the program with the javac command.(Java compiler)
4. If there are syntax errors, go back to Notepad and edit the program.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 23
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

5. Run the program with the java command.(Java Interpreter)


6. If it does not run correctly, go back to Notepad and edit the program.
7. When it shows result then stop.

7. Explain in detail about Java Command Line Arguments. (Nov/Dec 2013)


JAVA COMMAND LINE ARGUMENTS
 Command line arguments are parameters that are supplied to the
application program at the time of invoking its execution.
 They must be supplied at the time of its execution following the file name.
 In the main () method, the args is confirmed as an array of string known as
string objects.
 Any argument provided in the command line at the time of program
execution, are accepted to the array args as its elements.
 Using index or subscripted entry can access the individual elements of an
array. The number of element in the array args can be getting with the
length parameter.
Example:
class Add
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a+b;
System.out.println(“Addition is=”+c);
}
}
output:
c:\javac Add.java
c:\java Add 5 2
7

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 24
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

8. Explain in detail about Classes in java.


Definition:
A class is a collection of objects of similar type. Once a class is defined, any
number of objects can be produced which belong to that class.
Class;
 A class is nothing but a blueprint or a template for creating different
objects which defines its properties and behaviors.

 Java class objects exhibit the properties and behaviors defined by its class.
 A class can contain fields and methods to describe the behavior of an object.
 Methods are nothing but members of a class that provide a service for an
object or perform some business logic.
 Java fields and member functions names are case sensitive.
 Current states of a class’s corresponding object are stored in the object’s
instance variables.
 Methods define the operations that can be performed in java programming.

A class has the following general syntax:

class name1
{
//public variable declaration
void methodname()
{
//body of method…
//Anything
}
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 25
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Example for Class:


public class Cube {
int length;
int breadth;
int height;
public int getVolume()
{
return (length * breadth * height);
}
}

Example to use method:


class Demo public static void main(String
{ args[])
private int x,y,z; {
public void input() Demo object=new Demo();
{ object.input();
x=10; object.sum();
y=15; object.print_data();
} }
public void sum() }
{
z=x+y;
}
public void print_data()
{
System.out.println(“Answer is =”
+z);
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 26
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

In program,
Demo object=new Demo();
object.input();
object.sum();
object.print_data();
 In the first line we created an object.
 The three methods are called by using the dot operator. When we call a
method the code inside its block is executed. The dot operator is used to call
methods or access them.

CREATING “MAIN” IN A SEPARATE CLASS


 We can create the main method in a separate class, but during compilation
you need to make sure that you compile the class with the “main” method.
class Demo class SumDemo
{ {
private int x,y,z; public static void main(String args[])
public void input() { {
x=10; Demo object=new Demo();
y=15; object.input();
} object.sum();
public void sum() object.print_data();
{ }
z=x+y; }
} public void print_data()
{
System.out.println(“Answer is =” +z);
}
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 27
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Use of dot operator


 We can access the variables by using dot operator.
class DotDemo class Demo1
{ {
int x,y,z; public static void main(String args[]){
public void sum(){ DotDemo object=new DotDemo();
z=x+y; DotDemo object2=new DotDemo();
} object.x=10;
public void show(){ object.y=15;
System.out.println("The Answer is "+z); object2.x=5;
} object2.y=10;
} object.sum();
object.show();
object2.sum();
object2.show();
}}
output :
C:\cc>javac Demo1.java
C:\cc>java Demo1
The Answer is 25
The Answer is 15

Instance Variable
 All variables are also known as instance variable. This is because of the
fact that each instance or object has its own copy of values for the variables.
Hence other use of the “dot” operator is to initialize the value of variable for
that instance.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 28
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Methods with parameters


class prg
{ class prg1
int n,n2,sum; {
public void take(int x,int y) public static void main(String args[])
{ {
n=x; prg obj=new prg();
n2=y; obj.take(10,15);
} obj.sum();
public void sum() obj.print();
{ }
sum=n+n2; }
}
public void print()
{
System.out.println("The Sum is"+sum);
}
}

Methods with a Return Type


 When method return some value that is the type of that method.
 Some methods are with parameter but that method did not return any value
that means type of method is void.
 And if method return integer value then the type of method is an integer.
class Demo1 class prg
{ { public static void main(String args[]){
int n,n2; int sum;
public void take( int x,int y) Demo1 obj=new Demo1();
{ obj.take(15,25);

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 29
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

n=x; sum=obj.process();
n=y; System.out.println("The sum is"+sum);
} }}
public int process() Output:
{ The sum is 25
return (n+n2); } }
Method Overloading
 Method overloading means method name will be same but each method
should be different parameter list.
class prg1 class Demo
{ {
int x=5,y=5,z=0; public static void main(String args[])
public void sum() {
{ prg1 obj=new prg1();
z=x+y; obj.sum();
System.out.println("Sum is "+z); obj.sum(10,12);
} System.out.println(+obj.sum(15));
public void sum(int a,int b) }
{ }
x=a; Output:
y=b; sum is 10
z=x+y; sum is 22
System.out.println("Sum is "+z); 27
}
public int sum(int a)
{
x=a;
z=x+y;
return z;
}}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 30
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Passing Objects as Parameters


 Objects can even be passed as parameters.
class para123
{ class DemoPara
int n,n2,sum,mul; {
public void take(int x,int y) public static void main(String args[])
{ {
n=x; para123 ob=new para123();
n2=y; ob.take(3,7);
} ob.sum();
public void sum() ob.take2(ob);
{ ob.multi();
sum=n+n2; }
System.out.println("The Sum }
is"+sum); Output:
} C:\cc>javac DemoPara.java
public void take2(para123 obj) C:\cc>java DemoPara
{ The Sum is 10
n=obj.n; Product is 21
n2=obj.n2;
}
public void multi()
{
mul=n*n2;
System.out.println("Product is"+mul);
}
}

Passing Values to methods and Constructor:


These are two different ways of supplying values to methods.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 31
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Classified under these two titles -


1. Pass by Value
2. Pass by Address or Reference
class Demopbv int a,b;
{ a=1;
int n,n2; b=2;
public void get(int x,int y) System.out.println("Initial Values of a &
{ b "+a+" "+b);
x=x*x; //Changing the values of passed Demopbv obj=new Demopbv();
arguments obj.get(a,b);
y=y*y; //Changing the values of passed System.out.println("Final Values "+a+"
arguments "+b);
} }
} }
class Demo345 Output:
{ C:\cc>javac Demo345.java
public static void main(String args[]) C:\cc>java Demo345
{ Initial Values of a & b 1 2
Final Values 1 2

Pass by Reference
 Objects are always passed by reference. When we pass a value by reference,
the reference or the memory address of the variables is passed. Thus any
changes made to the argument causes a change in the values which we
pass.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 32
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

class pass_by_ref class apply7


{ {
int n,n2; public static void main(String args[])
public void get(int a,int b) {
{ int x=5,y=10;
n=a; pass_by_ref obj=new pass_by_ref();
n2=b; obj.get(x,y); //Pass by Value
} System.out.println("Initial Values are--
public void doubleit(pass_by_ref temp) ");
{ System.out.println(+obj.n);
temp.n=temp.n*2; System.out.println(+obj.n2);
temp.n2=temp.n2*2; obj.doubleit(obj); //Pass by Reference
} System.out.println("Final Values are");
} System.out.println(+obj.n);
System.out.println(+obj.n2);
}
}

9. Discuss in detail the Abstract class with example. (Nov/Dec 2013)


Definition:

An abstract class is a class that is declared as abstract. It may or may not


include abstract methods. Abstract classes cannot be instantiated, but they can
be subclass.

 Abstract classes are those which can be used for creation of objects.
However their methods and constructors can be used by the child or
extended class. The need for abstract classes is that you can generalize the
super class from which child classes can share its methods. The subclass of
an abstract class which can create an object is called as "concrete class".

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 33
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 When the keyword abstract appears in a class definition, it means that zero
or more of it’s methods are abstract.

 An abstract method has no body.

 Some of the subclass has to override it and provide the implementation.

 Objects cannot be created out of abstract class.

 Abstract classes basically provide a guideline for the properties and methods
of an object.

 In order to use abstract classes, they have to be subclassed.

 There are situations in which you want to define a superclass that declares
the structure of a given abstraction without providing a complete
implementation of every method.

 That is, sometimes you want to create a superclass that only defines
generalized form that will be shared by all of its subclasses, leaving it to
each subclass to fill in the details.

 One way this situation can occur is when a superclass is unable to create a
meaningful implementation for a method.

SYNTAX:

abstract type name(parameter-list);

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 34
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

abstract class shape class triangle extends shape


{ {
double dim1; triangle(double a, double b)
double dim2; {
shape(double a, double b) super(a,b);
{ }
dim1 = a; double area()
dim2 = b; {
} System.out.println("Area of
abstract double area(); triangle.");
} return dim1*dim2/2;
}
class rectangle extends shape }
{
rectangle(double a, double b) public class Abstract_D2
{ {
super(a,b); public static void main(String args[])
} {
double area() rectangle r = new rectangle(10,20);
{ triangle t = new triangle(10,6);
System.out.println("Area of
rectangle."); System.out.println(r.area());
return dim1*dim2; System.out.println(t.area());
} }}
} Output:
Area of rectangle.
200.0
Area of triangle.
30.0

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 35
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

10. Explain Constructor and constructor overloading with examples?


Definition:
A constructor has the same name as the name of the class to which it
belongs. Constructor’s syntax does not include a return type, since
constructors never return a value.
 Constructors may include parameters of various types. When the
constructor is invoked using the new operator, the types must match those
that are specified in the constructor definition.
 Java provides a default constructor which takes no arguments and performs
no special actions or initializations, when no explicit constructors are
provided.
 The only action taken by the implicit default constructor is to call the
superclass constructor using the super() call.
 Constructor arguments provide you with a way to provide parameters for the
initialization of an object.
Example:
Cube1(int l, int b, int h) {
public class Cube1 { length = l;
breadth = b;
int length; height = h;
int breadth; }
int height; public static void main(String[] args) {
public int getVolume() Cube1 cubeObj1, cubeObj2;
{ cubeObj1 = new Cube1();
return (length * breadth * height); cubeObj2 = new Cube1(10, 20, 30);
} System.out.println("Volume of Cube1 is : "
Cube1() { + cubeObj1.getVolume());
length = 10; System.out.println("Volume of Cube1 is : "
breadth = 10; + cubeObj2.getVolume()); }}
height = 10;
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 36
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Note:
 If a class defines an explicit constructor, it no longer has a default
constructor to set the state of the objects.
 If such a class requires a default constructor, its implementation must be
provided. Any attempt to call the default constructor will be a compile time
error if an explicit default constructor is not provided in such a case.
Overloaded Constructors
 The constructors in a class all have the same name as the class, their
signatures are differentiated by their parameter lists.
 It is possible to use this() construct, to implement local chaining of
constructors in a class.
 Java requires that any this() call must occur as the first statement in a
constructor.
public class Cube2 { Cube2(int l, int b, int h) {
int length; length = l;
int breadth; breadth = b;
int height; height = h;
public int getVolume() { System.out.println("Finished with
return (length * breadth Parameterized Constructor having 3
* height); params"); }
} public static void main(String[] args) {
Cube2() { Cube2 cubeObj1, cubeObj2;
this(10, 10); cubeObj1 = new Cube2();
cubeObj2 = new Cube2(10, 20,
System.out.println("Finished 30);
with Default Constructor"); System.out.println("Volume of Cube1 is :
} " + cubeObj1.getVolume());
Cube2(int l, int b) {
this(l, b, 10); System.out.println("Volume of
Cube2 is : " + cubeObj2.getVolume());
System.out.println("Finished }

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 37
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

with Parameterized Constructor having 2 }


params");
}

11. Explain Single Dimensional Arrays in JAVA. (Nov/Dec 2012) (May/Jun


2013)

ARRAY:

Definition: Array is the static memory allocation. It allocates the memory for
the same data type in sequence. It contains multiple values of same types. It
also stores the values in memory at the fixed size. Multiple types of arrays are
used in any programming language such as: one - dimensional, two -
dimensional or can say multi - dimensional.

Declaration :

int num[ ]; or int num = new int[2];

 Sometimes user declares an array and its size simultaneously. You may or
may not be define the size in the declaration time.

int num[ ] = {50,20,45,82,25,63};

Example 1: (Array sorting)

 In This program illustrates that the array working way. This program takes
the numbers present in the num[] array in unordered list and prints
numbers in ascending order.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 38
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 In this program the sort() function of the java.util.*; package is using to sort
all the numbers present in the num[] array. The Arrays.sort() automatically
sorts the list of number in ascending order by default. This function held the
argument which is the array name num.

import java.util.*;
public class array{ Output:
public static void main(String[] args){ C:\chandan>javac
int num[] = {50,20,45,82,25,63}; array.java
int l = num.length; C:\chandan>java
int i,j,t; array
System.out.print("Given number : "); Given number : 50
for (i = 0;i < l;i++ ){ 20 45 82 25 63
System.out.print(" " + num[i]); Ascending order
} number : 20 25 45
System.out.println("\n"); 50 63 82
System.out.print("Accending order number : ");
Arrays.sort(num);
for(i = 0;i < l;i++){
System.out.print(" " + num[i]);
} } }

Example 2: (Copying array to array)

 Firstly we have to define a class "CopyArray". Now, we take two integer type
arrays like: array1 [] and array2 []. The array1[] contains some integer type
values (2,3,4,5,8,9) and another is blank. After initializing this, now we get
number of rows and columns by using the array1.length. We use the 'for'
loop statement for displaying and copying ( array1[j] = array2[j] ) the array.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 39
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

public class CopyArray { System.out.print("]");


public static void main(String[] args) { System.out.println("\narray1:");
int array1[]= {2,3,4,5,8,9}; System.out.print("[");
int array2[] = new int[6]; for(int j=0; j<array1.length; j++){
System.out.println("array:"); array2[j] = array1[j];
System.out.print("["); System.out.print(" "+ array2[j]);
for (int i=0; i<array1.length; i++){ }
System.out.print(" "+array1[i]); System.out.print("]");
} }
}

Example 3: (String Array)

class StringCharacter { Output:

static String[] C:\>javac StringCharacter.java


roseindia={"chanan","tapan",
C:\>java StringCharacter
"Amar","santosh","deepak"};
chanan
public static void main(String args[]){
tapan
for(int i=0;i<5;i++){
System.out.println(roseindia[i]); } Amar

}} santosh

deepak

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 40
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Example 4: (Sum of Array)

public class ArrayAverage{


public static void main(String[] args) {
double nums[ ]={1.0,2.3,3.4,4.5,40.5};
double result=0.0;
int i=0;
for(i=0; i < nums.length; i++){
result=result + nums[i];
}
System.out.println("Average is =" + result/nums.length);
}
}

12. Explain Double Dimensional Arrays in Java with Example.

Definition: Two-dimensional arrays are defined as "an array of arrays". Since


an array type is a first-class Java type, we can have an array of ints, an array of
Strings, or an array of Objects.

Declaration:

int[ ][ ] A = new int[3][4];

Example 1: Matrix Multiplication

Matrix: A matrix is a collection of data in rows and columns format.

 In this program we are going to implement a matrix. To make a program


over the two dimensional array, first of all we have to declare class named as

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 41
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

MatrixExample that has one static method outputArray() which takes


integer type array and represents it.

 For displaying the matrix we need to its rows and column by using the
array.length method.

 Use the for loop to print all the values stored in the array. At last use the
main() method inside which we are going to declare the values of the
multidimensional array which we are going to use. Call the outputArray()
method inside the main method. The output will be displayed to the user by
println() method.

class MatrixMultiply{ System.out.println();


public static void main(String[] args) { }
int array[][] = {{5,6,7},{4,8,9}}; for(int i = 0; i < x; i++) {
int array1[][] = {{6,4},{5,7},{1,1}}; for(int j = 0; j < y-1; j++) {
int array2[][] = new int[3][3]; for(int k = 0; k < y; k++){
int x= array.length;
System.out.println("Matrix 1 : "); array2[i][j] += array[i][k]*array1[k][j];
for(int i = 0; i < x; i++) { }
for(int j = 0; j <= x; j++) { }
System.out.print(" "+ array[i][j]); }
} System.out.
System.out.println(); println("Multiply of both matrix : ");
} for(int i = 0; i < x; i++) {
int y= array1.length; for(int j = 0; j < y-1; j++) {
System.out.println("Matrix 2 : "); System.out.print(" "+array2[i][j]);
for(int i = 0; i < y; i++) { }
for(int j = 0; j < y-1; j++) { System.out.println();
System.out.print(" "+array1[i][j]); }
} }
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 42
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Example 2: Matrix Addition

class MatrixSum{ OUTPUT:


public static void main(String[] args) {
int array[][]= {{4,5,6},{6,8,9}}; C:\amar work>javac
int array1[][]= {{5,4,6},{5,6,7}}; MatrixSum.java
System.out.println("Number of Row= " + array.length);
System.out.println("Number of Column= " + array[1].length); C:\amar work>java
MatrixSum
int l= array.length;
System.out.println("Matrix 1 : "); Number of Row= 2

for(int i = 0; i < l; i++) {


for(int j = 0; j <= l; j++) { Number of Column= 3

System.out.print(" "+ array[i][j]);


} Matrix 1 :

System.out.println();
456
}
int m= array1.length;
689

System.out.print(" "+array1[i][j]);
Matrix 2 :
}
System.out.println();
546
}
System.out.println("Addition of both matrix : ");
567
for(int i = 0; i < m; i++) {
for(int j = 0; j <= m; j++) {
Addition of both matrix
System.out.print(" "+(array[i][j]+array1[i][j]));
:
}
System.out.println();
9 9 12
}
}} 11 14 16

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 43
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

13. Explain String operations in Java with Example. (Nov/Dec 2012) (May/Jun
2013)
STRINGS:
Definition: Strings, which are widely used in Java programming, are a
sequence of characters. In the Java programming language, strings are objects.
The Java platform provides the String class to create and manipulate strings.
Creating Strings:
String greeting = "Hello world!";
The different string operations are

SN Methods with Description

char charAt(int index)


1
Returns the character at the specified index.

int compareTo(Object o)
2
Compares this String to another Object.

int compareTo(String anotherString)


3
Compares two strings lexicographically.

String concat(String str)


4
Concatenates the specified string to the end of this string.

boolean equals(Object anObject)


5
Compares this string to the specified object.

byte getBytes()
6 Encodes this String into a sequence of bytes using the platform's default
charset, storing the result into a new byte array.

byte[] getBytes(String charsetName


7 Encodes this String into a sequence of bytes using the named charset,
storing the result into a new byte array.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 44
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)


8
Copies characters from this string into the destination character array.

int hashCode()
9
Returns a hash code for this string.

int indexOf(int ch)


10 Returns the index within this string of the first occurrence of the specified
character.

int length()
11
Returns the length of this string.

boolean matches(String regex)


12
Tells whether or not this string matches the given regular expression.

boolean regionMatches(boolean ignoreCase, int toffset, String other,


13 int ooffset, int len)
Tests if two string regions are equal.

String replace(char oldChar, char newChar)


14 Returns a new string resulting from replacing all occurrences of oldChar in
this string with newChar.

String replaceAll(String regex, String replacement


15 Replaces each substring of this string that matches the given regular
expression with the given replacement.

String replaceFirst(String regex, String replacement)


16 Replaces the first substring of this string that matches the given regular
expression with the given replacement.

String[] split(String regex)


17
Splits this string around matches of the given regular expression.

18 boolean startsWith(String prefix)

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 45
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Tests if this string starts with the specified prefix.

String substring(int beginIndex)


19
Returns a new string that is a substring of this string.

char[] toCharArray()
20
Converts this string to a new character array.

String toLowerCase()
21 Converts all of the characters in this String to lower case using the rules of
the default locale.

String toString()
22
This object (which is already a string!) is itself returned.

String toUpperCase()
23 Converts all of the characters in this String to upper case using the rules of
the default locale.

String trim()
24
Returns a copy of the string, with leading and trailing whitespace omitted.

static String valueOf(primitive data type x)


25
Returns the string representation of the passed data type argument.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 46
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

public class AllStringFuctionExample { System.out.println(“String before

public static void main(String[] args) { trimming ==> ” + tempstr);

String str = “All String function System.out.println(“String after trimming

Example in java”; ==> ” + tempstr.trim());

// convert string into Lower case // Find the character at the given index
from the given string
String Lowercase = str.toLowerCase();
System.out.println(“Character at the
System.out.println(“Lower case String ==>
index 6 is ==> ” + str.charAt(6));
” + Lowercase);

// find the substring between two index


// convert string into upper case
range
String Uppercase = str.toUpperCase();
System.out.println(“String between index
System.out.println(“Upper case String ==>
3 to 9 is ==> ”
” + Uppercase);
+ str.substring(3, 9));
// Find length of the given string

System.out.println(“Length of the given


// replace the character with another
string ==>” + str.length());
character
// Trim the given string i.e. remove all first
System.out.println(“String after
and last the spaces from
replacement ==> ”
// the string
+ str.replace(„a‟, „Y‟));
String tempstr = “ String trimming
example “;
// replace the substring with another
substring

System.out.println(“String after
replacement ==> ”

+ str.replace(“java”, “loan”)); }}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 47
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

JAVA STRINGBUFFER CLASS

 StringBuffer is mainly used for the dynamic string concatenation which


enhances the performance. A string buffer implements a mutable
sequence of characters. A string buffer is like a String, but can be
modified. At any point in time it contains some particular sequence of
characters, but the length and content of the sequence can be changed
through certain method calls.
StringBuffer class Methods

SN Methods with Description

public StringBuffer append(String s)


1 Updates the value of the object that invoked the method. The method takes
boolean, char, int, long, Strings etc.

public StringBuffer reverse()


2 The method reverses the value of the StringBuffer object that invoked the
method.

public delete(int start, int end)


3
Deletes the string starting from start index until end index.

public insert(int offset, int i)


4
This method inserts an string s at the position mentioned by offset.

replace(int start, int end, String str)


5 This method replaces the characters in a substring of this StringBuffer with
characters in the specified String.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 48
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Example:
public class JavaStringBufferExample { /*
2. StringBuffer StringBuffer(int
public static void main(String[] args) { length)
/* Constructs empty StringBuffer with
Java StringBuffer is a mutable initial capacity of length
sequence of characters. */
Difference between Java String and StringBuffer sbObj2 = new
StringBuffer is that StringBuffer StringBuffer(10);
can be modified while String can
not. /*
3. StringBuffer StringBuffer(String
Java StringBuffer can buit using str)
one of the following constructors constructs StringBuffer with the
*/ contents same as argument String
*/
/* StringBuffer sbObj3 = new
1. StringBuffer StringBuffer() StringBuffer("Hello World");
Construct empty StringBuffer with System.out.println(sbObj3);
initial capacity of 16
*/ }
}
StringBuffer sbObj1 = new
StringBuffer();

14. Explain static members in detail.


The static members are,
 Static variables
 Static methods

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 49
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Static Variables
Fields that have the static modifier in their declaration are called static
fields or class variables. They are associated with the class, rather than with
any object. Every instance of the class shares a class variable, which is in one
fixed location in memory. Any object can change the value of a class variable,
but class variables can also be manipulated without creating an instance of the
class.
Static Methods
The Java programming language supports static methods as well as
static variables. Static methods, which have the static modifier in their
declarations, should be invoked with the class name, without the need for
creating an instance of the class, as in
ClassName.methodName(args)

Example
class Cube { public class CubeStaticTest {
public static void main(String args[]) {
int length = 10; System.out.println("Number of Cube
int breadth = 10; objects = " + Cube.numOfCubes);
int height = 10; System.out.println("Number of Cube
public static int numOfCubes = 0; objects = "+ Cube.getNoOfCubes());
// static variable }
public static int getNoOfCubes() { }
//static method Output:
return numOfCubes; Number of Cube objects = 0
} Number of Cube objects = 0
public Cube() {
numOfCubes++; //
}
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 50
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

14. Explain Java Packages with Example. (Nov/Dec 2012)


JAVA PACKAGE

Definition 1:

A Java package is a mechanism for organizing a group of related files in


the same directory and having each class file in a package directive with that
directory name at the top of the file.
 Programmers also use package convention to organize classes that belong to
the same files or providing similar functionality.
 Java source files can include a package statement at the top left of the file to
designate the package for the classes in which the source file defines.
 If we are not including any package in our java source file then the source
file automatically goes to the default package.
Definition 2:

Packages are used in Java, in-order to avoid name conflicts and to


control access of class, interface and enumeration etc. A package can be
defined as a group of similar types of classes, interface, enumeration and sub-
package. Using package it becomes easier to locate the related classes.

Package are categorized into two forms


 Built-in Package:-Existing Java package for example java.lang, java.util etc.
 User-defined-package:- Java package created by user to categorized classes
and interface

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 51
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Creating a package
 Creating a package in java is quite easy. Simply include a package command
followed by name of the package as the first statement in java source file.
Example:
package mypack;
public class employee
{
...statement;
}
 The above statements create a package called mypack.
 Java uses file system directory to store package. For example the .class for
any classes you to define to be part of mypack package must be stored in a
directory called mypack

package mypack; class test


class Book {
{ public static void main(String[] args)
String bookname; {
String author; Book bk = new Book("java","Herbert");
Book(String b, String c) bk.show();
{ }
this.bookname = b; }
this.author = c;
}
public void show()
{
System.out.println(bookname+" "+
author);
}
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 52
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

To run this program :


 Create a directory under your current working development directory (i.e.
JDK directory), name it as mypack.
 compile the source file
 Put the class file into the directory you have created.
 Execute the program from development directory.

NOTE: Development directory is the directory where your JDK is install.


Uses of java package
 Package is a way to organize files in java; it is used when a project consists
of multiple modules. It also helps resolve naming conflicts.
 Package's access level also allows you to protect data from being used by the
non-authorized classes.
import keyword
 import keyword is used to import built-in and user-defined packages into
your java source file. So that your class can refer to a class that is in
another package by directly using its name.
 There are 3 different ways to refer to class that is present in different
package
1. using fully qualified name (But this is not a good practice.)
Example:
class MyDate extends java.util.Date
{
//statement;
}
2. import the only class you want to use.
Example:
import java.util.Date;
class MyDate extends Date
{
//statement.
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 53
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

3. import all the classes from the particular package


Example:
import java.util.*;
class MyDate extends Date
{
//statement;
}
 Import statement is kept after the package statement.
Example:
package mypack;
import java.util.*;
 But if you are not creating any package then import statement will be the
first statement of your java source file.
Static import
 Static import is a feature that expands the capabilities of import keyword.
It is used to import static member of a class.
 We all know that static member is referred in association with its class name
outside the class.
 Using static import, it is possible to refer to the static member directly
without its class name.
 There are two general form of static import statement.
 The first form of static import statement, import only a single static
member of a class
Syntax
import static package.class-name.static-member-name;

Example
import static java.lang.Math.sqrt; //importing static method sqrt
of Math class

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 54
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 The second form of static import statement,imports all the static


member of a class
Syntax
import static package.class-type-name.*;

Example
import static java.lang.Math.*; //importing all static member of
Math class

Example without using static import

public class Test


{
public static void main(String[] args)
{
System.out.println(Math.sqrt(144));
}
}
Output

12

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 55
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Example using static import


import static java.lang.Math.*;
public class Test
{
public static void main(String[] args)
{
System.out.println(sqrt(144));
}
}
Output
12

Features of a Java package


 The protected members of the classes can be accessed by each other in
the same package.

 It resolves the naming collision for the types it contains.

 A package may have the following types.

o Interfaces

o Classes

o Enumerated types

o Annotations

15. Discuss in detail the access specifiers available in java.


Access Modifiers
1. private
2. protected
3. default
4. public

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 56
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Public
 Public classes, methods, and fields can be accessed from everywhere.
Example:
public class Square { // public class
public int x, y, size; // public instance variables
}
Private
 Private methods and fields can only be accessed within the same class to
which the methods and fields belong.
 Private methods and fields are not visible within subclasses and are not
inherited by subclasses.
 So, the private access specifier is opposite to the public access specifier.
 It is mostly used for encapsulation: data are hidden within the class and
accessor methods are provided.

Example:
public class Square { // public class
private double x, y // private (encapsulated) instance variables

public setCorner(int x, int y) { // setting values of private fields


this.x = x;
this.y = y;
}

public getCorner() { // setting values of private fields


return Point(x, y);
}
}
Protected
 Protected methods and fields can only be accessed within the same class to
which the methods and fields belong, within its subclasses, and within
classes of the same package, but not from anywhere else.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 57
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Default
 If you do not set access to specific level, then such a class, method, or field
will be accessible from inside the same package to which the class, method,
or field belongs, but not from outside this package.
 This access-level is convenient if you are creating packages.
Access Level Permitted By Each Specifier:

Situation public protected default private

Accessible to class
yes yes yes no
from same package?

Accessible to class
yes no, unless it is a subclass no no
from different package?

16. Explain Java – Doc Comments in detail with Example.


Definition:
 One of the more useful tools in the JDK toolkit is javadoc. This tool is
used to produce documentation pages from comments embedded within
our Java programs. The document pages are automatically produced in
HTML with a distinctive style that has come to be associated with Java
documentation.
 In order to use javadoc, we must first embed special comments within
our program.
 These comments have the form:
/** This is a comment .........................
...and here is line two of the comment*/

 The comment begins with “/**” and ends (as do all comment blocks) with
“*/”. We associate these blocks with specific parts of our .java file by
placing the special comment block directly before that part of the code.

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 58
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

 Blocks can be associated with: The class, itself, public, or otherwise


accessible instance variables, or specific methods of the class.
 The general form for a .java file with javadoc comments is as follows:
/** This is a comment associated with the class */
public class MyClass {
/** This block is associated with an instance variable */
public int xxx;
/**This comment is associated with a method */
public void myMethod () {. . .}

The Need for Comments


Some of the reasons for using comments are:
 Often comments are added at the start of each source file to give a
description of what source the file contains and copyright information.

 The fields and methods of a class are often given brief comments that
describe their purpose and what they do.

 Complex code is often commented heavily to make it clearer and easier to


understand.

Comments in Java
Single Line Comments
 private static String name = "Guys"; //The name to print

Multi Line Comments


 /* Getter method provides public access in read only fashion.
This function returns the port number. */
int getPort() { ... }

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 59
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

17. Create a Complex number class in Java. The class should have a
constructor and methods to add, subtract and multiply two complex nos,
and to return the real and imaginary parts.
public class Complex { public Complex mul(Complex op) {
public Complex () { Complex result = new Complex();
this.re = 0; result.setRe(this.re * op.getRe() - this.im *
this.im = 0; op.getIm());
} result.setIm(this.re * op.getIm() + this.im *
public Complex(double re, double im) { op.getRe());
this.re = re; return result;
this.im = im; }
}
public Complex(Complex input) { public Complex div(Complex op) {
this.re = input.getRe(); Complex result = new Complex(this);
this.im = input.getIm(); result = result.mul(op.getConjugate());
} double opNormSq =
public double getRe() { op.getRe()*op.getRe()+op.getIm()*op.getIm();
return this.re; result.setRe(result.getRe() / opNormSq);
} result.setIm(result.getIm() / opNormSq);
public double getIm() { return result;
return this.im; }
} public Complex getConjugate()
public void setRe(double re) { {
this.re = re; return new Complex(this.re, this.im * (-1));
} }
public void setIm(double im) { public String toString() {
this.im = im; if (this.re == 0) {
} if (this.im == 0) {
public Complex add(Complex op) { return "0";
Complex result = new Complex(); } else {
result.setRe(this.re + op.getRe()); return (this.im + "i");

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 60
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

result.setIm(this.im + op.getIm()); }
return result; } else {
} if (this.im == 0) {
public Complex sub(Complex op) { return String.valueOf(this.re);
Complex result = new Complex(); } else if (this.im < 0) {
result.setRe(this.re - op.getRe()); return(this.re + " " + this.im + "i");
result.setIm(this.im - op.getIm()); } else {
return result; return(this.re + " +" + this.im + "i");
} }
public Complex mul(Complex op) { }
Complex result = new Complex(); }
result.setRe(this.re * op.getRe() - private double re;
this.im * op.getIm()); private double im;
result.setIm(this.re * op.getIm() + public static void main(String argv[]) {
this.im * op.getRe()); Complex a = new Complex(3, 4);
return result; Complex b = new Complex(1, -100);
} System.out.println(a.add(b));
System.out.println(a.sub(b));
System.out.println(a.mul(b));
System.out.println(a.div(b));

}
}

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 61
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

18. Write a menu-based java program that can calculate the area of a
triangle, circle or square, based on the user‟s choice.
package rand; switch(sn)
import java.io.*; {
public class chpackage case 1:
{ {
public String read()throws double r,area;
IOException { c.put("Enter the radius");
BufferedReader r=Double.parseDouble(c.read());
br=new BufferedReader(new area=3.14*r*r;
InputStreamReader(System.in)); c.put("The area of circle"+area);
String str=br.readLine(); break;
return str; }
} Case 2:
{{{
public void paste(String st) double b,h,area;
{ c.put("Enter the breath");
System.out.print(st); b=Double.parseDouble(c.read());
} c.put("Enter the height");
public void put(String pu) h=Double.parseDouble(c.read());
{ area=0.5*b*h;
System.out.println(pu); c.put("The area of circle"+area);
} break;
} case 3: {
The below code is outside of the double r,h,area;
folder. c.put("Enter the radius");
import rand.*; r=Double.parseDouble(c.read());
import java.io.*; c.put("Enter the height");
class pk4 { h=Double.parseDouble(c.read());
public static void main(String area=3.14*r*r*h;
ar[])throws IOException c.put("The area of cylinder"+area);

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 62
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

{ break; }
chpackage c=new chpackage();
c.put("AREA OF VARIOUS case 4: {
GEOMETRICAL FIGURES"); int si,area;
{ c.put("Enter the side");
c.put("MAIN MENU"); si=Integer.parseInt(c.read());
c.put("1.Area of circle"); area=si*4;
c.put("2.Area of triangle"); c.put("The area of square"+area);
c.put("3.Area of cylinder"); break;
c.put("4.Area of square"); }
c.put("5.Exit"); case 5:
c.put("Enter your choice"); {
int sn=Integer.parseInt(c.read()); }
} }}

University Questions
PART – A
1. What is an abstract class? (Nov/Dec 2011)
Refer Q.No:58
2. Under which context would you use „final‟ and „finally‟. (Nov/Dec 2011)
Refer Q.No:36
3. Give two examples for java modifiers. (Nov/Dec 2010)
Refer Q.No:27 & 28
4. What is the purpose of the getBytes( ) method? (Nov/Dec 2010)
Refer Q.No: 59
5. List all data types in java. (Nov/Dec 2008)
Refer Q.No: 60
6. Define classes and methods in Java. (Nov/Dec 2008)
Refer Q.No:22 & 24
7. What is byte code? (May/Jun 2012) (Nov/Dec 2013)

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 63
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

Refer Q.No: 13
8. What are packages? (May/Jun 2012)
Refer Q.No: 51
9. Why is java language called as „robust‟? (Nov/Dec 2013)
Refer Q.No:61
10. How does java make an executable file? (Nov/Dec 2013)
Refer-Q-no: Part b 5
11. Is JVM„s platform independent? Justify. (May/Jun 2013)
Refer Q.No: 15
12. How do we allocate an array dynamically in Java? (May/Jun 2013)
Refer Q.No: 41
13. How is the keyword „super‟ used in Java? (Nov/ Dec 2011)
Refer-Q-no: 18
14. What are wrapper classes in Java? (Nov/ Dec 2011)
Refer-Q-no: 29
15. Give a sample statement for parseInt( ) and give comments for the
statement. (Nov/ Dec 2010)
Refer-Q-no: part b – 7
16. What is the difference between an interface and abstract class?
(Nov/Dec 2012)
Refer-Q-no: 37

PART – B

1. Describe the structure of a typical Java program and give the steps to
execute it. (10) (Nov/Dec 2011)
Refer Q.No: 5
2. Explain the importance of JVM in JRE. (6) (or) Explain the virtual
machine concept with reference to Java.(Nov/Dec 2010) (Nov/Dec 2011)
Refer Q.No: 4
3. Create a Complex number class in Java. The class should have a

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 64
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

constructor and methods to add, subtract and multiply two complex nos,
and to return the real and imaginary parts. (10) (Nov/Dec 2011)
Refer Q.No: 17
4. What are packages? How we they created and used? (Nov/Dec 2011)
Refer Q.No: 14
5. Write a menu-based java program that can calculate the area of a
triangle, circle or square, based on the user‟s choice. (Nov/Dec 2010)
Refer Q.No: 18
6. Explain the Virtual Machine concept with reference to Java.
(Nov/Dec 2010)
Refer Q.No: 3
7. Explain constants, variables and operators in Java. Write a simple java
program to concatenate two strings. (Nov/Dec 2008)
Refer Q.No: 13
8. Explain array declaration, initialization and use in Java. Write a java
program to implement matrix multiplication. (Nov/Dec 2008)
Refer Q.No: 12
9. Write a java program to perform all string operations using String class.
(May/Jun 2012)
Refer Q.No: 13
10. Write a java program to create two single dimensional array, initialize
them and add them store the result in another array. (May/Jun 2012)
Refer Q.No: 11
11. Explain about various string operation in Java. (Nov/Dec 2012)
Refer Q.No: 13
12. Write a program to find the maximum number of the given array.
(Nov/Dec 2012)
Refer Q.No: 11
13. Explain abut packages in Java. (Nov/Dec 2012)
Refer Q.No: 14
14. Write a program to convert an Integer array to String. (Nov/Dec 2012)
Refer Q.No: 11 & 13

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 65
Mailam Engineering College, Mailam.
Department of Information Technology
(CS2311/U-IV)

15. Write a Java program using arrays to do the following (May/Jun 2013)
(i) to copy one array content to another array
(ii) to arrange the numbers in ascending order
(iii) find the maximum of an array
Refer Q.No: 11
16. Write a simple java program to find a given string in a string array.
(May/Jun 2013) (or) How one dimensional arrays are handled in Java?
Explain it with an example. (April/May 2011)
Refer Q.No: 11
17. Write a Java program to split a string into multiple Java String
Objects. (May/Jun 2013)
Refer Q.No: 13
18. i) Explain about Java features. (Nov/Dec 2013)

ii) Discuss about Java command line arguments.


iii) Write a Java program to find the sum of the following
series 1-2+3-4+…+n
Refer Q.No: 2 & 7
19. i) Distinguish between (Nov/Dec 2013)
a. Abstract class and class
b. Interface and class
ii) Discuss about benefits of Abstract class.
Refer Q.No: 9, Part –a (37)
20. What are the various data types available in Java? Explain it with an
example. Explain about operators in Java? (Nov/Dec 2011)
Refer Q.No:
21. How two dimensional arrays are handled in Java? Explain it with an
example. (Nov/Dec 2011)
Refer Q.No: 12

Prepared by: Mrs. S. AMUTHA, AP/IT


Mr. P. MATHIVANAN, AP/IT 66

You might also like