Unit 1:
Unit 1 contains questions form Internet Protocol, Core Java Questions based on OOPS concept like
Encapsulation, Inheritance, Polymorphism, Abstraction with coding example.
Some problem-based questions may also be asked example.
````````````````````````````Hands on practice
Questions(easy to hard level)
Q1. Create a function that takes an array of numbers and return "Boom!" if the digit 7 appears
in the array. Otherwise, return "there is no 7 in the array".
Q2. Create a function that will test if a string is a valid PIN or not via a regular expression. A
valid PIN has:
• Exactly 4 or 6 characters.
• Only numeric characters (0-9).
• No whitespace.
Q3. A number is said to be Harshad if it's exactly divisible by the sum of its digits. Create a
function that determines whether a number is a Harshad or not.
Q4. You have a pack of 5 randomly numbered cards, which can range from 0-9. You can win
if you can produce a higher two-digit number from your cards than your opponent.
Return true if your cards win that round.
Q5. Create a function that determines whether elements in an array can be re-arranged to form
a consecutive list of numbers where each number appears exactly once.
Q6. Given the shortest side of a 30° by 60° by 90° triangle, find out the other two sides.
Return the longest side and medium-length side in that order.
Q7. Create a function that takes an integer and outputs an n x n square solely consisting of
the integer n .
Q8. Create a function that will count the number of digits of a number. Conversion of
the number to a string is not allowed.
Q9. Create a function that takes three numbers — the width and height of a rectangle, and
the radius of a circle — and returns true if the rectangle can fit inside the
circle, false if it can't.
Q10. Create a function that takes two number strings and returns their sum as a string.
Q11. Create a function that takes three numbers — the width and height of a rectangle, and
the radius of a circle — and returns true if the rectangle can fit inside the
circle, false if it can't.
Q12. Create a function that takes an array of numbers and returns the mean (average) of all
those numbers.
Q13. A word nest is created by taking a starting word, and generating a new string by placing
the word inside itself. This process is then repeated.
Q14. Given a number, return a list containing the two halves of the number. If the number is
odd, make the rightmost number higher.
Q15 .Write a program by creating an 'Employee' class having the following methods and
print the final salary.
1 - 'getInfo()' which takes the salary, number of hours of work per day of employee as
parameter
2 - 'AddSal()' which adds $10 to salary of the employee if it is less than $500.
3 - 'AddWork()' which adds $5 to salary of employee if the number of hours of work per day
is more than 6 hours.
Q16. Create a class called 'Matrix' containing constructor that initializes the number of rows
and number of columns of a new Matrix object. The Matrix class has the following
information:
1 - number of rows of matrix
2 - number of columns of matrix
3 - elements of matrix in the form of 2D array
Q17. The Matrix class has methods for each of the following:
1 - get the number of rows
2 - get the number of columns
3 - set the elements of the matrix at given position (i,j)
4 - adding two matrices. If the matrices are not addable, "Matrices cannot be added" will be
displayed.
5 - multiplying the two matrices
Q18. Write a program to print the area of a rectangle by creating a class named 'Area' having
two methods. First method named as 'setDim' takes length and breadth of rectangle as
parameters and the second method named as 'getArea' returns the area of the rectangle. Length
and breadth of rectangle are entered through keyboard.
Q19. Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'.
Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class
Student.
Q20. Assign and print the roll number, phone number and address of two students having
names "Sam" and "John" respectively by creating two objects of class 'Student'.
Q21. Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5
units by creating a class named 'Triangle' without any parameter in its constructor.
Q22. Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5
units by creating a class named 'Triangle' with constructor having the three sides as its
parameters.
Q23. Write a program to print the area of two rectangles having sides (4,5) and (5,8)
respectively by creating a class named 'Rectangle' with a method named 'Area' which returns
the area and length and breadth passed as parameters to its constructor.
Q24. Write a program to print the area of a rectangle by creating a class named 'Area' taking
the values of its length and breadth as parameters of its constructor and having a method named
'returnArea' which returns the area of the rectangle. Length and breadth of rectangle are entered
through keyboard.
Q25. Print the average of three numbers entered by user by creating a class named 'Average'
having a method to calculate and print the average.
Q26. Print the sum, difference and product of two complex numbers by creating a class named
'Complex' with separate methods for each operation whose real and imaginary parts are entered
by user.
Q27. Write a program that would print the information (name, year of joining, salary,
address) of three employees by creating a class named 'Employee'. The output should be as
follows:
Name Year of joining Address
Robert 1994 64C- WallsStreat
Sam 2000 68D- WallsStreat
John 1999 26B- WallsStreat
Q28. Add two distances in inch-feet by creating a class named 'AddDistance'.
Q29. Input: Given a list of numbers separated with a comma.The numbers 5 and 8 are present
in the list.
Assume that 8 always comes after 5.
Case 1: num1 -> Add all numbers which do not lie between 5 and 8 in the Input List.
Case 2: num2 -> Numbers formed by concatenating all numbers from 5 to 8 in the list
.Output: Sum of num1 and num2
Example: 3,2,6,5,1,4,8,9
Num1: 3+2+6+9 =20
Num2: 5148O/p=5148+20 = 5168
Q30. A string which is a mixture of letters, numbers, and special characters from which
produce the largest
even number from the available digit after removing the duplicates digits.
If an even number did not produce then return -1.
Ex: infosys@337
O/p : -1
———————————-
Hello#81@21349
O/p : 984312
Q31. Take input a number ’N’ and an array as given below.
Input:-N = 2
Array =1,2,3,3,4,4
O/p : 2
Find the least number of unique elements after deleting N numbers of elements from the array.
In the above example, after deleting N=2 elements from the array.
In above 1,2 will be deleted.
So 3,3,4,4 will be remaining so,
2 unique elements are in the array i.e 3 and 4.
Q32. A non-empty string containing only alphabets. print the longest prefix from the input
string which is the same as the suffix.
Prefix and Suffix should not be overlapped.
Print -1 if no prefix exists which is also the suffix without overlap.
Do case-sensitive comparison.
Positions start from 1.
Input : xxAbcxxAbcxx
o/p: xx (‘xx’ in the prefix and ‘xx’ in the suffix and this is the longest one in the input string so
the output will be ‘xx’).
Input: Racecar
o/p: -1 (There is no prefix which is also a suffix so the output will be -1).
Q33. Number of odd sub-arrays.
Find the number of distinct subarrays in an array of integers such that the sum of the subarray
is an odd integer, two subarrays are considered different if they either start or end at different
indexes.
Input:
1
3
123
Output:
4
Explanation : Total subarrays are [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]
In this there are four subarrays which sum is odd i.e: [1],[1,2] ,[2,3],[3].
Create an abstract class 'Parent' with a method 'message'. It has two subclasses each having a method
with the same name 'message' that prints "This is first subclass" and "This is second subclass"
respectively. Call the methods 'message' by creating an object for each subclass.
Q34.Create an abstract class 'Bank' with an abstract method 'getBalance'. $100, $150 and $200 are
deposited in banks A, B and C respectively. 'BankA', 'BankB' and 'BankC' are subclasses of class 'Bank',
each having a method named 'getBalance'. Call this method by creating an object of each of the three
classes.
Q35.We have to calculate the percentage of marks obtained in three subjects (each out of 100) by
student A and in four subjects (each out of 100) by student B. Create an abstract class 'Marks' with an
abstract method 'getPercentage'. It is inherited by two other classes 'A' and 'B' each having a method
with the same name which returns the percentage of the students. The constructor of student A takes
the marks in three subjects as its parameters and the marks in four subjects as its parameters for
student B. Create an object for eac of the two classes and print the percentage of marks for both the
students.
Q36. An abstract class has a construtor which prints "This is constructor of abstract class", an
abstract method named 'a_method' and a non-abstract method which prints "This is a normal method
of abstract class". A class 'SubClass' inherits the abstract class and has a method named 'a_method'
which prints "This is abstract method". Now create an object of 'SubClass' and call the abstract method
and the non-abstract method. (Analyse the result).
Q37.Create an abstract class 'Animals' with two abstract methods 'cats' and 'dogs'. Now create a
class 'Cats' with a method 'cats' which prints "Cats meow" and a class 'Dogs' with a method 'dogs'
which prints "Dogs bark", both inheriting the class 'Animals'. Now create an object for each of the
subclasses and call their respective methods.
Q38.We have to calculate the area of a rectangle, a square and a circle. Create an abstract class
'Shape' with three abstract methods namely 'RectangleArea' taking two parameters, 'SquareArea' and
'CircleArea' taking one parameter each. The parameters of 'RectangleArea' are its length and breadth,
that of 'SquareArea' is its side and that of 'CircleArea' is its radius. Now create another class 'Area'
containing all the three methods 'RectangleArea', 'SquareArea' and 'CircleArea' for printing the area of
rectangle, square and circle respectively. Create an object of class 'Area' and call all the three methods.
Q39.Repeat the above question for 4 rectangles, 4 squares and 5 circles.
Hint- Use array of objects
Q40.Create a class named 'PrintNumber' to print various numbers of different datatypes by
creating different methods with the same name 'printn' having a parameter for each datatype.
Q41.Create a class to print an integer and a character with two methods having the same name but
different sequence of the integer and the character parameters.
For example, if the parameters of the first method are of the form (int n, char c), then that of the second
method will be of the form (char c, int n).
Q42.Create a class to print the area of a square and a rectangle. The class has two methods with the
same name but different number of parameters. The method for printing area of rectangle has two
parameters which are length and breadth respetively while the other method for printing area of
square has one parameter which is side of square.
Q43.Create a class 'Student' with three data members which are name, age and address. The
constructor of the class assigns default values name as "unknown", age as '0' and address as "not
available". It has two members with the same name 'setInfo'. First method has two parameters for
name and age and assigns the same whereas the second method takes has three parameters which are
assigned to name, age and address respectively. Print the name, age and address of 10 students.
Hint - Use array of objects
Q44.Create a class 'Degree' having a method 'getDegree' that prints "I got a degree". It has two
subclasses namely 'Undergraduate' and 'Postgraduate' each having a method with the same name that
prints "I am an Undergraduate" and "I am a Postgraduate" respectively. Call the method by creating an
object of each of the three classes.
Q45.A boy has his money deposited $1000, $1500 and $2000 in banks-Bank A, Bank B and Bank C
respectively. We have to print the money deposited by him in a particular bank.
Create a class 'Bank' with a method 'getBalance' which returns 0. Make its three subclasses named
'BankA', 'BankB' and 'BankC' with a method with the same name 'getBalance' which returns the
amount deposited in that particular bank. Call the method 'getBalance' by the object of each of the
three banks.
Q46.A class has an integer data member 'i' and a method named 'printNum' to print thevalue of 'i'.
Its subclass also has an integer data member 'j' and a method named 'printNum' to print the value of
'j'. Make an object of the subclass and use it to assign a value to 'i' and to 'j'. Now call the method
'printNum' by this object.
Q47.Suppose a class 'A' has a static method to print "Parent". Its subclass 'B' also has a static method
with the same name to print "Child". Now call this method by the objects of the two classes. Also, call
this method by an object of the parent class refering to the child class i.e. A obj = new B().
Q48.All the banks operating in India are controlled by RBI. RBI has set a well defined guideline (e.g.
minimum interest rate, minimum balance allowed, maximum withdrawal limit etc) which all banks
must follow. For example, suppose RBI has set minimum interest rate applicable to a saving bank
account to be 4% annually; however, banks are free to use 4% interest rate or to set any rates above it.
Q49.Write a JAVA program to implement bank functionality in the above scenario and
demonstrate the dynamic polymorphism concept. Note: Create few classes namely Customer,
Account, RBI (Base Class) and few derived classes (SBI, ICICI, PNB etc). Assume and implement
required member variables and functions in each class.
Hint:
Class Customer
{
//Personal Details ...
// Few functions ...
}
Class Account
{
// Account Detail ...
// Few functions ...
}
Class RBI
{
Customer c; //hasA relationship
Account a; //hasA relationship
..
Public double GetInterestRate() { }
Public double GetWithdrawalLimit() { }
}
Class SBI: public RBI
{
//Use RBI functionality or define own functionality.
}
Class ICICI: public RBI
{
//Use RBI functionality or define own functionality.
}
Q50. Write a program to print the names of students by creating a Student class. If no name
is passed while creating an object of Student class, then the name should be "Unknown",
otherwise the name should be equal to the String value passed while creating object of
Student class.
Q51. Create a class with a method that prints "This is parent class" and its subclass with another
method that prints "This is child class". Now, create an object for each of the class and call
1 - method of parent class by object of parent class
2 - method of child class by object of child class
3 - method of parent class by object of child class.
Q52.In the above example, declare the method of the parent class as private and then repeat the
first two operations (You will get error in the third).
Q53.Create a class named 'Member' having the following members:
Data members
1 - Name
2 - Age
3 - Phone number
4 - Address
5 - Salary
It also has a method named 'printSalary' which prints the salary of the members.
Two classes 'Employee' and 'Manager' inherits the 'Member' class. The 'Employee' and 'Manager'
classes have data members 'specialization' and 'department' respectively. Now, assign name, age,
phone number, address and salary to an employee and a manager by making an object of both of
these classes and print the same.
Q54.Create a class named 'Rectangle' with two data members 'length' and 'breadth' and two
methods to print the area and perimeter of the rectangle respectively. Its constructor having
parameters for length and breadth is used to initialize length and breadth of the rectangle. Let class
'Square' inherit the 'Rectangle' class with its constructor having a parameter for its side (suppose s)
calling the constructor of its parent class as 'super(s,s)'. Print the area and perimeter of a rectangle
and a square..
Q55. Now repeat the above example to print the area of 10 squares.
Hint-Use array of objects.
Q56.Create a class named 'Shape' with a method to print "This is This is shape". Then create two
other classes named 'Rectangle', 'Circle' inheriting the Shape class, both having a method to print "This
is rectangular shape" and "This is circular shape" respectively. Create a subclass 'Square' of 'Rectangle'
having a method to print "Square is a rectangle". Now call the method of 'Shape' and 'Rectangle' class
by the object of 'Square' class.
Unit 2
Unit 2 contains HTML, CSS, XML, content.