You are on page 1of 6

NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT

CORE JAVA
ASSIGNMENT 1
Q.1 Explain any five features of Java language.
Ans: The main features of Java language:

 Simple: Java is one of the simplest programming languages to learn and master in order
to grasp the concept of Object-oriented learning.

 Object-Oriented Programming Language: This is one of the primary reasons why Java
is so popular amongst developers. Java strictly conforms to the rules of Object Oriented
Programming or OOP.  Object-oriented development includes concepts of Objects and
Classes and many more. This enables developers to have a wide variety of options for
designing their software.

 Multithreaded Programming: Java supports multithreaded programming i.e. it supports


multiple operations running at the same time. We can think of a thread as an individual
operation or parts of the program using the processor. It increases the performance by
decreasing the development time needed for a particular software.

 High Performance: Though java is not as fast as compiled languages like C, C++ but
java still has some of the good features like just-in-time compiler, multithreading,
garbage collection etc. that increases the performance of java near to compiled languages.

 Platform Independent: Java is a platform independent language. In java you can write
and compile your program on one machine (eg. windows) and then you can run the
compiled code(.class file) on any other machine (eg. linux or macos). That is what the
platform independent means in java, program written and compiled on one machine can
be run on any other machine.
NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT

Q.2 Write a note on Garbage collection in Java.


Ans: Garbage Collection:

 Garbage Collection in Java is managed by a program called Garbage Collector. Garbage


Collector can be defined as a program that is used to manage memory automatically by
handling the object de-allocation.
 We know that in Java language, the new objects are created and allocated memory using
the new operator. The memory allocated to an object using a new operator remains
allocated until the references are using this object. As soon as the references cease to
exist, the memory that the object occupies is reclaimed.
 Java then handles the de-allocation or destruction of objects automatically and we need
not explicitly destroy the object. This technique is the Garbage Collection technique in
Java where the programmers need not handle the de-allocation of objects explicitly.
 Note that if the programs do not de-allocate the memory when the objects do not need it
then eventually there will be no memory left to allocate and the programs will crash. This
situation is called a memory leak. The garbage collector always runs in the background
on a daemon thread. Garbage Collector is considered as the best example of the daemon
thread. Garbage Collector runs intending to release the heap memory. It does this by
destroying the objects that are “unreachable”.

Q.3 Write a program to show the use of abstract class.

Ans: Code:

abstract class Party {


static void doParty()
{
System.out.println("Lets have some fun!!");
}
}

public class Main extends Party {


public static void main(String[] args)
{
Party.doParty();
}
}

Output: Lets have some fun!!

Q.4 Why Java is called Platform Independent language?


NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT

Ans: Platform independent means the execution of the program is not restricted by the type of
operating system installed and hence we can execute the java program at any type of
environement available.

How:

 Java is a platform independent language because of the magic of java which is bytecode.

 In java, when we execute the source code,it generates the .class file which contains the
bytecodes. Bytecodes are interpreted by JVM which is available with every type of OS
we install.

 On the other side the source code written in C or C++ gets converted into an object code
which is machine and OS dependent and hence are termed as Platform dependent.

Q.5 Explain Java Program structure.

Ans: A typical Structure of a Java program would contain the following elements:
 Package declaration: Classes in java could be placed in different directories/packages
based on the module they are used in or the functionality it provides. For all classes that
belong to a single parent source directory, path from source directory is considered as
package declaration.

 Import statements: There would be classes written in other folders/packages of your


working java project and also there are many classes written by individuals, companies,
institutions, enthusiasts etc., which could be useful in our program. To use them in
a class, we would need to import the class that we intend to use. Many classes could be
imported in a single program and hence multiple import statements could be written.

 Class definition: A name should be given to class in java file. This name is used while
creating an object of the class, in other classes/programs.

 Class variables: Variables are the means of storing the values of parameters that are
required during execution of the program. Variables declared with modifiers have
different scopes, which define the life of a variable. We shall see in detail about the
modifiers like global, local, static and private at Variables in Java.
NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT

 Methods/Routines/Behaviors: This is a room for a set of instructions which form a


purposeful functionality or a routine that would be required to run multiple times during
the execution of the program. To not repeat the the same set of instructions when the
same functionality is required, the instructions are enclosed in a method.

Q.6 What are command line arguments? Explain with example.


Ans:
 The java command-line argument is an argument i.e. passed at the time of running the
java program
 The arguments passed from the console can be received in the java program and it can be
used as an input.
 So, it provides a convenient way to check the behavior of the program for the different
values. You can pass N (1, 2, 3 and so on) numbers of arguments from the command
prompt.
 Example:

Code :
class A{  
public static void main(String args[]){  
  
for(int i=0;i<args.length;i++)  
System.out.println(args[i]);   
}  
}  
compile by > java A.java  
run by > java A Vansh Nagda 8 1 abc

Output: Vansh
Nagda
8
1
abc
Q.7 Explain various data types available in java.
Ans: Data types specify the different sizes and values that can be stored in the variable. There
are two types of data types in Java:
NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT

1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.

Q.8 Explain if else statement with example in Java.

Ans: The Java if-else statement also tests the condition. It executes the if block if condition is
true otherwise else block is executed. If the conditional expression returns true, then a particular
statement(s) will execute otherwise other statement(s) will execute.
Example:

Code:
public class IfElseExample {  
public static void main(String[] args) {  
    //defining a variable  
    int number=13;  
    //Check if the number is divisible by 2 or not  
    if(number%2==0){  
        System.out.println("even number");  
    }else{  
        System.out.println("odd number");  
    }  
}  
}  

Output: odd number

Q.9 Compare while, Do while and for loop in Java.

Ans: The main difference between for loop, while loop, and do while loop is:

 While loop checks for the condition first. so it may not even enter into the loop, if the
condition is false.

 do while loop, execute the statements in the loop first before checks for the condition. At
least one iteration takes places, even if the condition is false.

 for loop is similar to while loop except that initialization statement, usually the counter
variable initialization a statement that will be executed after each and every iteration in the
loop, usually counter variable increment or decrement
NAME: VANSH DILIP NAGDA ROLL NO: 381 CLASS: SYIT

Q.10 What is constructor in Java? Write a program to demonstrate its use.


Ans: Constructor:

 Constructor is a block of code that initializes the newly created object.


 A constructor resembles an instance method in java but it’s not a method as it doesn’t
have a return type.
 In short constructor and method are different(More on this at the end of this guide).
People often refer constructor as special type of method in Java.

Example:

 Here we have created an object obj of class Hello and then we displayed the instance
variable name of the object.
 As you can see that the output is HelloWorld which is what we have passed to the
name during initialization in constructor
 . This shows that when we created the object obj the constructor got invoked. In this
example we have used this keyword, which refers to the current object, object obj in this
example.

Code:
public class Hello {
String name;
//Constructor
Hello(){
this.name = "HelloWorld";
}
public static void main(String[] args) {
Hello obj = new Hello();
System.out.println(obj.name);
}
}

Output: HelloWorld

You might also like