0% found this document useful (0 votes)
200 views

Java Quiz

The document discusses key concepts in object-oriented programming in Java including: 1. Classes define templates for objects with states and behaviors. Objects are instances of classes that have specific values for those states. 2. Methods are collections of statements that perform operations. The return keyword exits a method optionally returning a value. 3. Inheritance, implemented via the extends keyword, allows subclasses to inherit fields and methods from parent classes while method overriding provides specific implementations of inherited methods.

Uploaded by

clarence
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views

Java Quiz

The document discusses key concepts in object-oriented programming in Java including: 1. Classes define templates for objects with states and behaviors. Objects are instances of classes that have specific values for those states. 2. Methods are collections of statements that perform operations. The return keyword exits a method optionally returning a value. 3. Inheritance, implemented via the extends keyword, allows subclasses to inherit fields and methods from parent classes while method overriding provides specific implementations of inherited methods.

Uploaded by

clarence
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

QUESTION 1

A class can be defined as a template/blueprint that describes the state/behavior that the object of
its type supports

QUESTION TWO

An object is an instance of a class. Objects have states and behaviors, for example a Car has color,
name, manufacturer,

QUESTION 3

Public class Car {

String carName;

Int color;

String numberPlate;

Void topSpeed(){

Void mileage(){

QUESTION 4

A java method is a collection of statements that are grouped together to perform an operation.
When you call system.out.printl() method, for example, the system actually executes several
statements in order to display a message on the console.

example

/** the snippet returns the minimum between two numbers */

Public static int minFunction (int n1, int n2) {

Int min ;

If (n1 > n2)

Min = n2;

Else

Min = n1 ;

Return min;

QUESTION 5

Implements – used for implementing an interfaces. To implement an interface we must use the
keyword implements. In java you can implement more than one interface.
Extends – used for extending a class. In java we inherit fields and methods of a class by extending it
using the keyword extends. A class can only extend a maximum of one class only

QUESTION 6

Instance variables in java are non-static variables which are defined in a class outside any method,
constructor or a block

QUESTION 7

A constant variable is a variable whose value cannot change once it has been assigned.

QUESTION 8

Class variables also known as static variables are declared with the static keyword but outside a
method, constructor, or block.

QUESTION 9

In java return is a keyword used to exit from the method with or without a value. Every method is
declared with a return type and it is mandatory for java methods.

QUESTION 10

Int total (int aNumber) {

Int a_value = aNumber + 10;

return a_value;

QUESTION 11

Java this keyword is used to refer the current instance of a method on which it is used

Private string javaFAQ;

Class Outer;

Class inner{

Void foo() {

Outer O = outer.this;

QUESTION 12

Method overloading in java is when a class has multiple methods by same name but different
parameters

QUESTION 13

Class Adder {
Static int add ( int a , int b) { return a + b;}

Static int add ( int a, int b, int c) {return a+b+c;}

Class TestOverloading {

Public static void main (string[]args) {

System.out.println (Adder.add(11,11));

System.out.println (Adder.add(11,11,11));

}}

QUESTION 14

If a subclass {child class} has the same method as declared in the parent class, it is known as method
overriding.

In other words, if a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.

QUESTION 15

// java program to demonstrate why we need method overriding

// here we are calling the method of the parent class with child class object

// creating parent class

Class vehicle {

Void run ( ) {

system.out.println (“vehicle is running);}

//creating child class

Class bike extends vehicle

Public static void main (string []) {

// creating an instance of a child class

Bike obj = new bike();

// calling the method with the child class instance

Obj.run () ;

}}
QUESTION 16

Constructor is a block of code that initializes the newly created object. A constructor resembles an
instance method in java but its not a method as it does not have a return type

Constructor has same name as the class and it looks like this in java code

Public class myClass {

//this is the constructor

My class(){

You might also like