Introduction
to
Programming
Object and Class, Data types, Basic
I/O
TYPES OF PROGRAMMING
There are two types of programming languages:
1. Low level language
a) Machine language
b) Assembly language
2. High level language
a) Procedural-Oriented language
b) Problem-Oriented language
c) Natural language
Machine language
• Sometimes referred to as machine code or object code,
• machine language is a collection of binary digits or bits that the computer reads and
interprets. Machine language is the only language a computer is capable of
understanding.
Example: Below is an example of machine language (binary) for the text "Hello World".
01001000 01100101 01101100 01101100 01101111 00100000
01010111 01101111 01110010 01101100 01100100
How does a computer convert text into binary or 0's and 1's?
● Computers convert text and other data into binary by using an assigned ASCII
value.
● Once the ASCII value is known that value can be converted into binary.
Assembly language
● They are often used to write operating systems
● The Java Bytecode Assembler (or simply Assembler) is a program that converts
code written in "Java Assembly Language" into a valid Java.class file
● most compilers convert source code directly to machine code,
Assembly language
● some examples of instructions supported by x86 processors.
MOV - move data from one location to The following assembly language can be
another used to add the numbers 3 and 4:
ADD - add two values
SUB - subtract a value from another mov eax,3 -loads 3 into the register
value "eax"
PUSH -push data onto a stack mov ebx,4 -loads 4 into the register
POP - pop data from a stack "ebx“
JMP - jump to another location
INT - interrupt a process add eax,ebx,ecx-adds "eax" and "ebx" and
stores the result (7) in "ecx"
Procedural-Oriented language
● A procedural language is a type of computer programming
● language that specifies a series of well-structured steps and procedures within its
programming context to compose a program.
● It contains a systematic order of statements, functions and commands to
complete a computational task or program.
● The procedural language segregates a program within variables, functions,
statements and conditional operators.
Problem-oriented language
● A computer language designed to handle a particular class of problem.
Example:
● COBOL was designed for business (Common Business-Oriented
Language)
● FORTRAN for scientific (Formula Translation)
● GPSS for simulation.
Natural language
● natural language refers to a human language such as English, Russian, German,
or Japanese as distinct from the typically artificial command or programming
language with which one usually talks to a computer
CLASS & OBJECTS
What is Object?
● In real-world an entity that has state and its behavior is known as an object.
● For Example:
✔ A Car is an object. It has states (name, color, model) and its behavior (changing gear, applying
brakes).
What is Class?
● A class is a template or blueprint that is used to create objects.
● Class representation of objects and the sets of operations that can be applied to
such objects.
● A class consists of Data members and methods.
DEFINING A CLASS IN JAVA
Syntax: ` Example:
public class class_name public class Car
{ {
Data Members; public:
Methods; double color;
} double model;
}
CLASS MEMBERS
● Data and functions are members.
Example:
public class Cube
{
int length;
int breadth;
int length; // Error
}
● A member cannot be redeclared within a class.
● No member can be added elsewhere other than in the class definition.
DIFFERENT WAYS TO CREATE AN OBJECT
DIFFERENT WAYS TO CREATE AN OBJECT
● Using new keyword
MyObject object = new MyObject();
● Using Class.forName()
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
● Using clone()
MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();
● Using object deserialization
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
CREATE AN OBJECT
Example:01 Example:02
public class MyClass { public class MyClass {
int x = 5; int x = 5;
public static void main(String[] public static void main(String[] args) {
args) MyClass myObj1 = new MyClass();
{ MyClass myObj2 = new MyClass();
System.out.println(myObj1.x);
MyClass myObj = new MyClass(); System.out.println(myObj2.x);
System.out.println(myObj.x); }
} }
}
CREATE AN OBJECT
Using Multiple Classes:
create an object of a class and access it in another class.
MyClass.java OtherClass.java
public class MyClass { class OtherClass {
int x = 5; public static void main(String[] args) {
} MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
3 WAYS TO INITIALIZE OBJECT
1. By reference variable
2. By method
3. By constructor
3 WAYS TO INITIALIZE OBJECT
Object and Class Example: Initialization through reference
class Student{ class TestStudent2 {
int id; public static void main(String args[])
String name; {
} Student s1=new Student();
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name);
}
}
3 WAYS TO INITIALIZE OBJECT
Object and Class Example: Initialization through method
class Student{ class TestStudent4{
int rollno;
String name; public static void main(String args[])
void insertRecord(int r, String n){ {
rollno=r; Student s1=new Student();
name=n; Student s2=new Student();
} s1.insertRecord(111,"Karan");
void displayInformation(){ s2.insertRecord(222,"Aryan");
s1.displayInformation();
System.out.println(rollno+" "+name); s2.displayInformation();
} }
} }
3 WAYS TO INITIALIZE OBJECT
Object and Class Example: Initialization through a constructor
public class Account{ public void setName(String name)
private String name; // instance {
variable this.name = name;
}
public Account(String name)
{ // method to retrieve the name
public String getName()
this.name = name; {
return name;
} }
} // end class
// method to set the name
JAVA CLASS ATTRIBUTES
class attributes are variables within a class.
Example:
public class MyClass { public class MyClass {
int x = 5; int x;
public static void main(String[] public static void main(String[]
args) { args) {
MyClass myObj = new MyClass(); MyClass myObj = new MyClass();
System.out.println(myObj.x); myObj.x = 40;
} System.out.println(myObj.x);
} }
}
JAVA CLASS ATTRIBUTES
Example:01 Example:02
public class MyClass { public class MyClass {
int x = 10; final int x = 10;
public static void main(String[] public static void main(String[]
args) { args) {
MyClass myObj = new MyClass(); MyClass myObj = new MyClass();
myObj.x = 25; myObj.x = 25;
System.out.println(myObj.x); System.out.println(myObj.x);
} }
} }
LOGICAL SNIPPETS
Class Test {
public static void main(String[] args) {
Test obj = new Test();
obj.start();
}
void start() {
String stra = "do";
String strb = method(stra);
System.out.print(": " + stra + strb);
}
String method(String stra) {
stra = stra + "good";
System.out.print(stra);
return " good";
}
}
QUESTION : 01
Which of the following statement(s) is/are correct?
X: A class is like a blue print and we can create as many objects using that class.
Y: Every object should belong to a class, since we can not create objects with
out a class.
A. X only
B. Y only
C. Both X and Y are correct
D. Both X and Y are incorrect Answer:
C
QUESTION : 02
Which of the following class declaration is correct?
/* X */ /* Y */
class Student Class Student
{ {
String name; String name;
int marks; int marks;
char section; char section;
} }
A. X only
B. Y only
C. X and Y both are correct
D. X and Y both are incorrect Answer:
A
QUESTION : 03
Which of the following is a valid declaration of an object of class Student?
1. Student obj = new Student;
2. Student obj = new Student();
3. obj = new Student();
4. new Student obj;
A. 1 only
B. 2 only
C. 1 & 2
D. 3 & 4 Answer:
B
QUESTION : 05
What will be the output of the following program?
class Apple {
int weight;
} A. 1
class Main { B. 2
public static void main(String args[]) { C. Error
Apple a1 = new Apple(); D. 1
Apple a2 = new Apple();
a1.weight = 1;
a2.weight = 2;
a2 = a1;
System.out.println(a2.weight);
}
}
Answer:
1
QUESTION : 09
Which statement is NOT true in java language?
A. A public member of a class can be accessed in all the packages.
B. A private member of a class cannot be accessed by the methods of the same
class.
C. A private member of a class cannot be accessed from its derived class.
D. A protected member of a class can be accessed from its derived class.
E. None of the above
Answer:
B
QUESTION : 10
Which one of the following is NOT true?
A. A class containing abstract methods is called an abstract class.
B. Abstract methods should be implemented in the derived class.
C. An abstract class cannot have non-abstract methods.
D. A class must be qualified as ‘abstract’ class, if it contains one abstract
method.
E. None of the above.
Answer:
C
QUESTION : 11
In java, objects are passed as
A. Copy of that object
B. Method called call by value
C. Memory address
D. Constructor
E. Default constructor.
Answer:
C
QUESTION : 12
Which statement is true regarding an object?
A. An object is what classes instantiated are from
B. An object is an instance of a class
C. An object is a variable
D. An object is a reference to an attribute
E. An object is not an instance of a class.
Answer:
B
QUESTION : 18
Which of the following may be part of a class definition?
A. Instance variables
B. Instance methods
C. Constructors
D. All of the above
E. None of the above
Answer:
D
QUESTION : 20
…….. is passed to a method by use of call by reference.
A. Variables
B. Objects
C. Methods
D. Operators
Answer:
B
QUESTION : 21
Using the keyboard interface you can fully abstract a ……
A. Method
B. Keyword
C. Class
D. Variables
Answer:
C
QUESTION : 22
What will be the result of compiling
class Car{
following code. public class Main {
int Wheels; public static void main(String[] args) {
void swap(Car other) { Car C1 = new Car();
other = this; C1.Wheels=4;
} Car C2= new Car();
} C2.Wheels = 8;
C2.swap(C1);
System.out.println(C1.Wheels);
System.out.println(C2.Wheels);
}
}
A. 4 8
B. 4 4 Answer:
C. 8 4
D. 8 8 A
QUESTION : 23
What will be the result of compiling
class Car{
following code. public class Main {
int Wheels; public static void main(String[] args) {
void swap(Car other) { Car C1 = new Car();
//other = this; C1.Wheels=4;
return; Car C2= new Car();
} C2.Wheels = 8;
} C2.swap(C1);
System.out.println(C1.Wheels);
System.out.println(C2.Wheels);
}
}
A. 4 8
B. 4 4 Answer:
C. 8 4
D. 8 8 A
QUESTION : 24
What will be the result of compiling
class Car{
following code. public class Main {
int Wheels; public static void main(String[] args) {
void swap(Car other) { Car C1 = new Car();
other.Wheels = C1.Wheels=4;
this.Wheels; Car C2= new Car();
} C2.Wheels = 8;
} C1.swap(C2);
System.out.println(C1.Wheels);
System.out.println(C2.Wheels);
}
}
A. 4 8
B. 4 4 Answer:
C. 8 4
D. 8 8 B
QUESTION : 25
What will be the result of compiling
class ReferencesAndObjects{
following code. class Student
public static void main(String s[]){ {
Student st1 = new Student(); String
Student st2; name;
st2 = st1; int marks;
st1.name = "Rajesh"; char
st2.marks = 87; section;
st1.section = 'C'; }
System.out.println(""+st1.name+""+st1.marks+""+st1.section)
;
System.out.println(""+st2.name+""+st2.marks+""+st2.sect
ion);
} A. Rajesh 87 C Rajesh 87 C
} B. rajesh 87 C rajesh 87 C Answer:
C. Rajesh C 87 Rajesh C 87
A
D. rajesh C 87 rajesh C 87
QUESTION : 26
What will be the result of compiling
public class BikeTester { class Bike{
following code.
public static void main(String s[]) { Bike(String companyParam,String modelParam,int
Bike bike = new Bike(null, null, 0); ccParam){
bike.company= "Hero honda"; company = companyParam;
bike.model="shine"; model= modelParam;
bike.cc=125; cc=ccParam;
bike.mileage=72.5; }
bike.diskBreaks=false; String company;
System.out.println(bike.company+"s"+bike.m String model;
odel+"gives a milage of int cc;
"+bike.mileage+"kmpl."); double mileage;
} boolean diskBreaks;
} }
A. Compilation error - since the Bike object is created using a constructor without
parameters.
B. Compilation error - since data is assigned to member variables - mileage and
diskBrakes - after the object is created.
C. Hero Honda's Shine gives a mileage of 72.5kmpl.
D. Runtime error - NullPointerException occurs while executing the program.
Answer:
QUESTION : 27
What will be the result of compiling
class BikeTester { class Bike{
following code.
public static void main(String s[]) { Bike(String companyParam,String modelParam,int
Bike bike = new Bike("Hero ccParam){
Honda","shine",125); company = companyParam;
bike.mileage=72.5; model= modelParam;
bike.diskBreaks=false; cc=ccParam;
System.out.println(bike.company +"'s }
"+bike.model +"has"+bike.cc+"cc power."); Bike(){ }
} String company;
String model;
} int cc;
double mileage;
boolean diskBreaks;
}
A. Compilation Error - since we can not have multiple constructors in a class.
B. Compilation Error - since we can not define a constructor with no parameters.
C. Hero Honda's Shine has 125cc power.
D. Compilation Error - since the default constructor has to be defined above the other
constructors.
Answer:
QUESTION : 28
What will be the result of compiling
class CarTester
following code. { class Car{
public static void main(String public String company;
s[] ){ Car(String company,String model,int cc,double
Car car = new mileage){
Car("Maruthi","WagonR",1000,12.4); this.company =company;
System.out.println(car.company+"'s this.model=model;
"+car.model+" has engine power this.cc= cc;
of"+car.cc+"cc"); this.mileage=mileage;
} }
} String Comapy;
String model;
int cc;
double mileage;
}
A. Maruti's Wagon R has engine power of 1000cc.
B. null's null has engine power of 0cc. Answer:
C. Compilation Error - since the parameter name passed to the constructor
A
can not be the same as member variable.
D. Compilation Error - since there is no default constructor.
QUESTION : 29
Identify the incorrect statement about classes.
A. Classes can contain only primitive data types.
B. Classes can contain both primitive data types and non-primitive
data types.
C. We can define multiple constructors in a class.
D. Classes contain variables as well as methods.
Answer:
A
QUESTION : 30
What will be the result of compiling
public class
following code. Price {
public void doIt(int i, HtcMobile p) {
i = 10000;
p.number = 1; }
public static void main(String args[]) {
int x = 2000;
HtcMobile p = new HtcMobile();
new Price().doIt(x, p);
System.out.println("Price: " + x + ", Mobile Count: " + p.number);
}}
class HtcMobile {
public int number;
}
A. Price: 2000, Mobile Count: 1
B. Price: 10000, Mobile Count: 1 Answer:
C. Price: 2000, Mobile Count: 0 A
D. Price: 10000, Mobile Count: 0
QUESTION : 31
What will be the result of compiling
public class
following code. Increment { class A {
public static void main(String[] args int i = 0;
){ A(int i) {
A a = new A(5); this.i = i + 4;
a.increment(); }
a.operate(); void increment() {
a = new A(a.i); i = this.i + 1;
a.operate(); }
a.increment(); void operate() {
System.out.println("i = " + a.i); i *= 3;
} }
} }
A. i=1
B. i = 103 Answer:
C. i = 55 B
D. Compilation Error
QUESTION : 32
What will be the result of compiling
class Simple{
following code. class Demo{
int a = 0; public static void main(String aRgs
void incr(){ [])
a++; {
} Simple s = new Simple();
void display(){ Simple r = new Simple();
System.out.println("a = s.incr();
" + a); r.incr();
} s.display();
} r.display();
}
}
A. a=0a=0
B. a=1a=1 Answer:
C. a=0a=1
D. a=1a=2 B
E. Compilation Error
QUESTION : 33
What will be the result of compiling
class ReferencesAndObjects{
following code. class Student
public static void main(String s[]){ {
Student st1 = new Student(); String name;
Student st2 = new Student(); int marks;
st1.name = "Rajesh"; char section;
st2.marks = 87; }
st1.section = 'C';
System.out.print("Print using st1 : " + st1.name + " " + st1.marks + " " + st1.section);
System.out.print("Print using st2 : " + st2.name + " " + st2.marks + " " + st2.section);
}
}
A. Print using st1 : Rajesh 0 C Print using st2 : null 87
B. Print using st1 : Rajesh 87 C Print using st2 : Rajesh 87
C. Compilation Error
D. Runtime Error Answer:
A
QUESTION : 34
What will be the result of compiling
class ReferencesAndObjects{
following code. class Student
public static void main(String s[]){ {
Student st1 = new Student(); String name;
Student st2 = new Student(); int marks;
st2 = st1; char section;
st2 = new Student(); }
st1.name = "Rajesh";
st2.marks = 87;
st1.section = 'C';
System.out.println("Print using st1 : " + st1.name + " " + st1.marks + "
" + st1.section);
System.out.println("Print using st2 : " + st2.name + " " + st2.marks + "
" + st2.section);
}}
A. Print using st1 : Rajesh 0 C Print using st2 : null 87
B. Print using st1 : Rajesh 87 C Print using st2 : Rajesh 87 C
C. Print using st1 : null 0 Print using st2 : Rajesh 87 C
D. Compilation Error Answer:
QUESTION : 35
What will be the result of compiling
class ReferencesAndObjects{
following code. class Student
public static void main(String s[]){ {
Student st1 = new Student(); String name;
Student st2 = new Student(); int marks;
st1 = st2; char section;
st1.name = "Rajesh"; }
st2.marks = 87;
st1.section = 'C';
System.out.print("Print using st1 : " + st1.name + " " + st1.marks + " " + st1.section);
System.out.print("Print using st2 : " + st2.name + " " + st2.marks + " " + st2.section);
}}
A. Print using st1 : Rajesh 0 C Print using st2 : null 87
B. Print using st1 : Rajesh 87 C Print using st2 : Rajesh 87 C
C. Print using st1 : null 0 Print using st2 : Rajesh 87 C
D. Compilation Error Answer:
QUESTION : 36
What will be the result of compiling
following code.
class OutPut{ class PrintA {
int c; int printA;
public static void main(String[] args){ }
PrintA a = new PrintA(); class PrintB{
PrintB c = new PrintB(); int printB = 5;
System.out.println("printA = int c;
" + a.printA); }
System.out.println("printB =
" + c.printB);
System.out.println("c = " + c.c);
}
}
A. printA = 0 printB = 5 c = 0
B. printA = 0 printB = 5 c = (some object reference value)
C. Compilation Error
D. Runtime Error Answer:
QUESTION : 37
What will be the result of compiling
following code. InitializationTest {
public class
int number;
public void InitializationTest(int number) {
this.number = number;
System.out.println(number);}
public static void main(String... strings) {
InitializationTest obj1, obj2;
obj1 = new InitializationTest();
obj2 = new InitializationTest(5);
}
}
A. 5
B. Compile time Error since no default constructor
C. Compile time Error since no parameterized constructor
D. Compilation Error or Runtime Error for some other Answer:
reason C
QUESTION : 38
Which of the following is correct?
X: These are different types of inner classes - Nested top-level classes, Member
classes, Local classes, Anonymous classes.
Y: If the main method is declared as private then the program compiles properly
but at run-time it gives a message conveying "Main method is not public."
A. X only
B. Y only
C. X and Y
D. Both are incorrect Answer:
QUESTION : 39
What will be the result of compiling
following
public code.
class Final { int process(int a)
final int assign = 30; {
public static void main(String[] args){ return a + 5;
final int result = 20; }
final int assign; }
Final f = new Final();
assign = 20;
System.out.println(assign);
System.out.println(f.assign);
System.out.println(f.process(result));
}
A. 20 20 25
B. 20 30 25
C. Compilation Error
D. Runtime Error Answer:
QUESTION : 40
What will be the result of compiling
following
public code.
class Final {
final int assign;
public static void main(String[] args) {
final int result = 20;
Final f = new Final();
f.assign = process(result);
System.out.println(f.assign);
}
final static int process(int a){
return a + 5;
}}
A. 20
B. 25
C. Compilation Error
D. Runtime Error Answer:
OUR PRODUCT
Aptimithra
aptimithra.co
m
Google Review
ethnus.com
THANK YOU