0% found this document useful (0 votes)
18 views16 pages

Cia 1

The document provides an overview of key Java concepts including the Java Virtual Machine (JVM), import statements, class and method saving rules, Java buzzwords, and the differences between String and StringBuffer. It also explains interfaces and constructors with examples, detailing their syntax and usage in Java programming. Additionally, it covers the significance of keywords like super, extends, and implements in the context of Java inheritance and interface implementation.

Uploaded by

nk3182006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views16 pages

Cia 1

The document provides an overview of key Java concepts including the Java Virtual Machine (JVM), import statements, class and method saving rules, Java buzzwords, and the differences between String and StringBuffer. It also explains interfaces and constructors with examples, detailing their syntax and usage in Java programming. Additionally, it covers the significance of keywords like super, extends, and implements in the context of Java inheritance and interface implementation.

Uploaded by

nk3182006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

C.I.

A-1
2 marks

1.Define JVM

Jvm is a virtual machine that lets the user to run java


byte code.

It acts as an interface between the java language


and the underlying hardware.

It is a part of java runtime environment(jre)

2.Define import statement and its syntax

The import keyword is used to import a


package,class or interface into a program or another
package.

Syntax:
Import package.class_name

The asterisk * symbol is used for importing all the


classes in a package.
For example, import java.io.*;

3.State the rules for saving class and method in


Java

Rules for creating class:

1.The class declaration is begun using the class


keyword.

2.The class keyword is followed by the name of the


class.

3.The class methods,fields and constructors are


placed within the braces.

Syntax:

class class_name{
statement-n;
}
Rules for creating methods:

1.Method declarations are begun with the return type


of the method.

2.The return type of the method is followed by the


name of the method.

3.The parameters of the method are placed within


the parentheses of the method.

Syntax:
returnType methodName(p1,p2,....){
Body of the method;
}

4.What are java buzzwords?

The features that are unique to the java language


are called java buzzwords.

Some of the java buzzwords are:


●​Platform independence:Java code can be
executed on multiple platforms, for example,
Windows, Linux, Sun Solaris, Mac/OS, etc. Java
code is compiled by the compiler and converted
into bytecode. This bytecode is a
platform-independent code because it can be
run on multiple platforms, i.e., Write Once and
Run Anywhere (WORA)
●​Portability:Java is portable because it facilitates
you to carry the Java bytecode to any platform.
It doesn't require any implementation.
●​Multi-threaded:A thread is like a separate
program, executing concurrently. We can write
Java programs that deal with many tasks at
once by defining multiple threads. The main
advantage of multi-threading is that it doesn't
occupy memory for each thread. It shares a
common memory area. Threads are important
for multimedia, Web applications, etc.

5.Differentiate between string and string buffer.

String String buffer


The String class is used The string buffer class is
to create immutable used to create mutable
objects that store a objects that store a
sequence of characters. sequence.
Strings are slower as String buffers are faster
they completely as they modify the
overwrite any changes changes.
made.
They require more They require less
memory. memory.
They do not provide any They provide methods
methods. like append(),insert(),etc

6.Define:

1.Super keyword:The super keyword in Java is a


reference variable that is used to refer to parent
class when we’re working with objects.

2.Extends keyword:In Java, the extends keyword is


used to declare a class that inherits from another
class, known as the superclass.
3.Implements keyword:The Java implements
keyword is used with classes to inherit the properties
of an interface. Interfaces can never be extended by
a class.

5 marks:

1.Explain Interface with an example.

An Interface in Java programming language is


defined as an abstract type used to specify the
behavior of a class. An interface in Java is a
blueprint of a behavior. A Java interface contains
static constants and abstract methods.

Interfaces are used to implement abstraction and


multiple inheritance in Java.

Syntax:
interface {
// declare constant fields
// declare methods that are abstract by default.
}
Interfaces can only be used to define abstract
methods.

The variables in an interface are always public ,


static and final.

import java.io.*;

// Interface Declared
interface testInterface {

// public, static and final


final int a = 10;

// public and abstract


void display();
}

// Class implementing interface


class TestClass implements testInterface {

// Implementing the capabilities of


// Interface
public void display(){
System.out.println("Geek");
}
}

class Main
{
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(t.a);
}

The Java implements keyword is used with classes


to inherit the properties of an interface. Interfaces
can never be extended by a class.

A class can extend another class, and similarly, an


interface can extend another interface. However,
only a class can implement an interface, and the
reverse (an interface implementing a class) is not
allowed.
For example,
import java.io.*;

interface Vehicle {

// Abstract methods defined


void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}

// Class implementing vehicle interface


class Bicycle implements Vehicle{

int speed;
int gear;

// Change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// Increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}

// Decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}

public void printStates() {


System.out.println("speed: " + speed
+ " gear: " + gear);
}
}

// Class implementing vehicle interface


class Bike implements Vehicle {

int speed;
int gear;
// Change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}

// Increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}

// Decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}

public void printStates() {


System.out.println("speed: " + speed
+ " gear: " + gear);
}

}
class Main
{
public static void main (String[] args)
{

// Instance of Bicycle(Object)
Bicycle bicycle = new Bicycle();

bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);

System.out.print("Bicycle present state : ");


bicycle.printStates();

// Instance of Bike (Object)


Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);

System.out.print("Bike present state : ");


bike.printStates();
}
}

2.Explain constructors with an example.

A constructor in Java is a special method that is


used to initialize objects.

The constructor is called when an object of a class is


created.

It can be used to set initial values for object


attributes.

Rules for creating constructors:

1.Constructor name must be the same as its class


name.

2.A Constructor must have no explicit return type.

3.A Java constructor cannot be abstract, static and


final.
Types of constructors:

1.Default Constructor

A constructor is called "Default Constructor" when it


does not have any parameter.

2. Parameterized Constructor:

A constructor which has a specific number of


parameters is called a parameterized constructor.

Constructor Overloading

Constructor overloading in Java is a technique of


having more than one constructor with different
signatures.

They are differentiated by the compiler by the


number of parameters in the list and their types.

class Student {
int id;
String name;
int age;

// Creating two-argument constructor


Student(int i, String n) {
id = i;
name = n;
}

// Creating three-argument constructor


Student(int i, String n, int a) {
id = i;
name = n;
age = a;
}

void display() {
System.out.println(id + " " + name + " " + age);
}

public static void main(String args[]) {


Student s1 = new Student(111, "Karan");
Student s2 = new Student(222, "Aryan", 25);
s1.display();
s2.display();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy