Professional Documents
Culture Documents
Assignment: TA (Compulsory)
PART-A
1. How platform independence is achieved in JAVA ?
Java Features:
● Java Virtual Machine (JVM)
- an imaginary machine that is implemented by emulating software on a real
machine
- provides the hardware platform specifications to which you compile all Java
technology code
Bytecode
- a special machine language that can be understood by the Java Virtual
Machine (JVM)
- independent of any particular computer hardware, so any computer with a
Java interpreter can execute the compiled Java program, no matter what
type of computer the program was compiled on.
The javac command is used to invoke Java's compiler and compile a Java source file.
A typical invocation of the javac command would look like below:
javac [options] [source files]
1. Encapsulation
2. Polymorphism
3. Inheritance
In a Java program, all characters are grouped into symbols called tokens. Larger language features
are built from the first five categories of tokens (the sixth kind of token is recognized, but is then
discarded by the Java compiler from further processing). We must learn how to identify all six kind of
tokens that can appear in Java programs. In EBNF we write one simple rule that captures this
structure:
token <= identifier | keyword | separator | operator | literal | comment
The different types of Tokens are:
If you run an java file(as an .class or .jar file) there's always 1 method being called: The main(String[]
args) method.
The method is only called once.
Example of an main method:
public static void main(String args[]) throws IOException {
LoggingBootstrap.bootstrap();
gui = new GUI();
}
In this case it only bootstraps the logger and uses the constuctor method of GUI(the graphical user
interface of the program)
It is because, the keywords are defined in the java language for a specific purpose. If we start
declaring variables with the same name, the compiler would not know if you are trying to use a
variable are trying to tell it to perform an action and hence keywords cannot be used as a variable
name.
Because, the main method is the starting point of the java program and if we need an object of that
class even before the main can be invoked, it is not possible. Hence it is declared static so that the
JVM Can acess the main method without having to instantiate that particular class
A constructor creates an Object of the class that it is in by initializing all the instance variables and
creating a place in memory to hold the Object. It is always used with the keyword new and then the
Class name. For instance, new String(); constructs a new String object.
PART - B
1. a) What is inheritance and how one can achieve code reusability? Explain
with an example.
Inheritance in Java refers to the feature wherein the code/functionality of one class can be used in
another class that extends this class. Example:
Here the class Child extends the class Parent and hence the methods of Parent are available for Child
to use. This way we are re-using the code in the parent class in the child class instead of re-writing
the whole thing again.
b) What is a class? How does it accomplish data hiding? Explain with example.
A class is a collection of data and methods to work with that, and other, data. An object is a specific
instance of a class. For example, "ArrayList" is a class that contains methods such as "add" and
"remove" for changing the items in the list. The instances (objects) of ArrayList can use these
methods to perform operations. It's easier to show the structure of things with a custom class.
//Constructors:
public MyClass()
{
}
Do-While : In do while the statements are executed for the first time and then the
conditions are tested, if the condition turns out to be true then the
statements are executed again.
There is no correct simple general formula for sum to n terms of the series
1+1/2+1/3+1/4+ ............. + 1/n
Deviation from the actual value fluctuates but remains relatively low.
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of
-128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in
large arrays, where the memory savings actually matters. They can also be used in place
of int where their limits help to clarify your code; the fact that a variable's range is limited can serve
as a form of documentation.
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value
of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you
can use a short to save memory in large arrays, in situations where the memory savings actually
matters.
int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of
-2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type
is generally the default choice unless there is a reason (like the above) to choose something else. This
data type will most likely be large enough for the numbers your program will use, but if you need a
wider range of values, use long instead.
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of
-9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use
this data type when you need a range of values wider than those provided by int.
float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values
is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language
Specification. As with the recommendations for byte and short, use a float (instead of double) if
you need to save memory in large arrays of floating point numbers. This data type should never be
used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal
class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java
platform.
double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of
values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language
Specification. For decimal values, this data type is generally the default choice. As mentioned above,
this data type should never be used for precise values, such as currency.
boolean: The boolean data type has only two possible values: true and false. Use this data
type for simple flags that track true/false conditions. This data type represents one bit of information,
but its "size" isn't something that's precisely defined.
char: The char data type is a single 16-bit Unicode character. It has a minimum value
of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
Package statement
import statements
Class Declaration {
Constructor declaration
Method declarations
A Java Virtual Machine (JVM) is a set of computer software programs that use a virtual machine model
for the execution of Java computer programs and scripts.
The JVM accepts data in a form commonly referred to as Java bytecode. This language conceptually
represents the instruction set of a stack-oriented, capability architecture.
javac - it is compiler which converst java source code to byte code ie.class file. This byte
code is standard for all platforms, machines or operating systems.
java - this is interpreter. this interprets the .class file based on a particular platform and
excutes them.e jvm -> java virtual machine comes into play. jvm for windows will be
different from jvm for solarais or linux . but all the jvm take the same byte code and
executes them in that platform.
b) How are data and method organized in an object oriented program ? Illustrate
the same for car object.
There are 2 ways: "private or protected" and "public". A programmer has the choice of making the
object's data either protected or public. Protected means the data associated with that object can be
only accessed by the object's own methods. It can't be globally accessed by simply calling the
variable. Public means it can be accessed from any function.
E.g:
Class Person
{
Protected:
Char name;
Char hair-colour;
change haircolour
change name
The above class can have an object of person with his name and hair-colour. As you can see it is
protected and no one else can change them except your own functions of change hair-colour and
change name.
That's how data and methods are organized. It is a security feature, called data encapsulation.
Java is an pure object oriented programming language, it uses the concepts of Classes, Objects,
Inheritance, Polymorphism. And the execution of a program is non-linear.
It is so called because you can't write a program with out using classes & objects.
Java was created for the purpose of making a language that could be implemented on many different
types of computers (cell phone, mac, PC, linux, etc.) C on the other hand can only be run on a
computer of the same type as the one that compiled the program.
Java is also object-oriented, whereas C is not. Java allows a user to create classes that contain data
and methods. C is not capable of doing that.
structured programming
procedure oriented programming
Java's bitwise operators operate on individual bits of integer (int and long) values. If an operand is
shorter than an int, it is promoted to int before doing the operations.
It helps to know how integers are represented in binary. For example the decimal number 3 is
represented as 11 in binary and the decimal number 5 is represented as 101 in binary. Negative
integers are store in two's complement form. For example, -4 is 1111 1111 1111 1111 1111 1111
1111 1100.
The bitwise operators
& - and
| - or
^ - Xor
~ - not
<< - left shift
>> - right shift
>>> - right shift
Examples:
1. For Loop
2. While Loop
3. Do - While Loop
Both For loop and While loop would iterate through a certain lines of code within the loop's limit as
long as the loop condition is satisfied.
A do while loop would execute the loop once even before checking the condition. So in a do while loop,
even if the loop condition is not satisfied the loop would execute once.
Example Declarations:
while (i < n) {
...
i++;
}
do {
...
i++;
} while (i < n) ;
polymorphism means the ability to take more than one form. This is something similar to a word
having several different meanings depending on the context.
Overloading - More than one method in the same class with the same name but different signature
Overriding - Method with the same name in child class masking/hiding the method in the parent class
A parameterized constructor in java is just a constructor which take some kind of parameter (variable)
when is invoked. For example.
class MyClass {
}
b) Explain the methods
(i) Trim
(ii) Substring
(iii) Length.