Wipro Practice Qs
Wipro Practice Qs
No Topics
Hands-on Assignment Status
. Covered
Example1)
If the two command line arguments are Wipro and Bangalore then
the output generated should be Wipro Technologies Bangalore.
Example2)
If the command line arguments are ABC and Mumbai then the
output generated should be ABC Technologies Mumbai
Command
[Note: It is mandatory to pass two arguments in command line]
1 Line
Argument
2 Command
Write a Program to accept a String as a command line argument Line
and print a Welcome message as given below. Argument
Example1)
C:\> java Sample John
O/P Expected : Welcome John
Example1)
C:\>java Sample 10 20
O/P Expected : The sum of 10 and 20 is 30
Loops
No Statu
Hands-on Assignment Topics Covered
. s
Write a program to check if a given integer number is
Positive, Negative, or Zero.
}
}
3 If Statement
Write a program to check if the program has received
command line arguments or not.
4 If Statement
Initialize two character variables in a program and
display the characters in alphabetical order.
5 If Statement
public class Five {
public static void main(String args[]) {
char c='@';
if((c>='a' && c<='z') || (c>='A' && c<='Z') )
System.out.println("alphabet");
else if(Character.isDigit(c)) System.out.println("number");
else System.out.println("special char");
}
}
6 If Statement
Write a program to accept gender ("Male" or "Female") and
age from command line arguments and print the percentage of
interest based on the given conditions.
7 If Statement
Initialize a character variable with an alphabhet in a
program.
Example1)
i/p:a
o/p:a->A
8 Switch
Write a program to receive a color code from the user (an Statement
Alphabhet).
The program should then print the color name, based on the
color code given.
import java.util.Scanner;
public class Eight {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
char c=sc.next().charAt(0);
sc.close();
if(c=='r') System.out.println("red");
else if(c=='b') System.out.println("blue");
else if(c=='g') System.out.println("green");
else if(c=='o') System.out.println("orange");
else if(c=='y') System.out.println("yellow");
else if(c=='w') System.out.println("white");
else System.out.println("invalid code");
}
}
9 Switch
Write a program to receive a number and print the Statement
corresponding month name.
Example1)
C:\>java Sample 12
Example2)
C:\>java Sample
Example3)
C:\>java Sample 15
import java.util.Scanner;
public class Nine {
public static void main(String args[]) {
int number;
Scanner sc=new Scanner(System.in);
String[]
months={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","
dec"};
number=sc.nextInt();
sc.close();
System.out.println(months[number-1]);
}
}
12 For Loop
Write a program to check if a given number is prime or
not.
import java.util.Scanner;
public class Twelve {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
sc.close();
int f=0;
for(int i=2;i<=number/2;i++)
{
if(number%i==0) f=1;
}
if(f==0) System.out.println("prime");
else System.out.println("not prime");
}
}
14 For Loop
Write a Java program to find if the given number is prime
or not.
Example1)
C:\>java Sample
O/P: Please enter an integer number
Example2)
C:\>java Sample 1
O/P:1 is neither prime nor composite
Example3)
C:\>java Sample 0
O/P: 0 is neither prime nor composite
Example4)
C:\>java Sample 10
O/P: 10 is not a prime number
Example5)
C:\>java Sample 7
O/P : 7 is a prime number
import java.util.Scanner;
public class Fourteen {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
sc.close();
int f=0;
for(int i=2;i<=number/2;i++)
{
if(number%i==0) f=1;
}
if(f==0) System.out.println("prime");
else System.out.println("not prime");
}
}
Write a program to print the sum of all the digits of a
given number.
Example1)
I/P:1234
O/P:10
import java.util.Scanner;
public class Fifteen {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
15 For Loop
sc.close();
int ans=0;
String s=""+n;
//System.out.println(s);
for(int i=0;i<s.length();i++)
{
int d=Character.getNumericValue(s.charAt(i));
ans+=d;
//System.out.println(ans);
}
System.out.println(ans);
}
}
16 For Loop
Write a program to print * in Floyds format (using for and
while loop)
*
* *
* * *
Example1)
C:\>java Sample
O/P: Please enter an integer number
Example2)
C:\>java Sample 3
O/P :
*
* *
* * *
import java.util.Scanner;
public class Sixteen {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
17 While Loop
Write a program to reverse a given number and print
Example1)
I/P: 1234
O/P:4321
Example2)
I/P:1004
O/P:4001
import java.util.Scanner;
public class Seventeen {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
StringBuffer s=new StringBuffer(String.valueOf(n));
System.out.println(s.reverse());
}
}
18 While Loop
Write a Java program to find if the given number is
palindrome or not
Example1)
C:\>java Sample 110011
O/P: 110011 is a palindrome
Example2)
C:\>java Sample 1234
O/P: 1234 is not a palindrome
import java.util.Scanner;
public class Eighteen {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
int number=0;
int temp=n;
while(n>0)
{
int t=n%10;
number=number*10+t;
n/=10;
}
if(temp==number) System.out.println("palindrome");
else System.out.println("not palindrome");
}
}
Arrays
Topics
No. Hands-on Assignment Status
Covered
}
}
import java.util.Arrays;
import java.util.*;
public class Two {
public static void main(String[] args) { One
2 Integer[] arr={1,5,7,23}; dimensional
int min=Collections.min(Arrays.asList(arr)); Array
int max=Collections.max(Arrays.asList(arr));
System.out.println("min value = "+ min);
System.out.println("max value = "+max);
}
}
3 One
Write a program to initialize an integer array with values dimensional
and check if a given number is present in the array or not.
import java.util.*;
public class Three { Array
public static void main(String[] args) {
int[] arr={1,5,2,6,22};
System.out.println("enter search value");
Scanner sc=new Scanner(System.in);
int search=sc.nextInt();
sc.close();
int index=-1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==search) index=i;
}
if(index==-1) System.out.println("-1");
else System.out.println(index);
}
}
4 One
Initialize an integer array with ascii values and print the dimensional
corresponding character values in a single row. Array
import java.util.Arrays;
import java.util.Arrays;
8 One
Write a program to print the sum of the elements of an array dimensional
following the given below condition. Array
import java.util.Scanner;
Example1)
C:\>java Sample 1 2 3
O/P: Please enter 4 integer numbers
Example2)
C:\>java Sample 1 2 3 4
O/P:
The given array is :
1 2
3 4
The reverse of the array is :
4 3
2 1
10 Two
Write a program to find the biggest number in a 3*3 array. Dimensional
The program is supposed to receive 9 integer numbers as Array
command line arguments.
Example1:
C:\>java Sample 1 2 3
O/P: Please enter 9 integer numbers
Example2:
C:\>java Sample 1 23 45 55 121 222 56 77 89
O/P:
The given array is :
1 23 45
55 121 222
56 77 89
The biggest number in the given array is 222
No Topics
Hands-on Assignment Status
. Covered
1 Classes
Create a class Box that uses a parameterized constructor to and
initialize the dimensions of a box.The dimensions of the Box Objects,
are width, height, depth. The class should have a method that Constructor
can return the volume of the box. Create an object of the Box
class and test the functionalities.
class Box
{
double l,h,w;
Box()
{
l=10;
h=w=l;
}
double vol()
{
return l*w*h;
}
}
public class First {
public static void main(String[] args) {
Box b1=new Box();
double v=b1.vol();
System.out.println("vol is "+v);
}
}
2 Classes
Create a new class called Calculator with the following and
methods: Objects,
1. A static method called powerInt(int num1,int num2) Constructor,
static
This method should return num1 to the power num2.
2. A static method called powerDouble(double num1,int num2).
This method should return num1 to the power num2.
3. Invoke both the methods and test the functionalities.
Hint: Use Math.pow(double,double) to calculate the power.
class Calculator
{
static double powerInt(int n1,int n2)
{
return Math.pow(n1, n2);
}
static double powerDouble(double n1,double n2)
{
return Math.pow(n1,n2);
}
}
public class Second {
public static void main(String[] args) {
System.out.println(Calculator.powerInt(2,2));
System.out.println(Calculator.powerDouble(4.0, 4.2));
}
}
3 Classes
Design a class that can be used by a health care professional and
to keep track of a patient’s vital statistics. The following Objects,
are the details. Constructor,
Name of the class - Patient static
Member Variables -
patientName(String),height(double),width(double)
Member Function - double computeBMI()
The above method should compute the BMI and return the result.
The formula for computation of BMI is weight (in kg) ÷
height*height(in metres).
Create an object of the Patient class and check the results.
this.patietName = patietName;
this.height = height;
this.width = width;
return (this.width+(this.height*this.height));
System.out.println(p1.computeBMI());
No Topics
Hands-on Assignment Status
. Covered
1 Encapsulation
Create a class Author with the following information. / Abstraction
Member variables : name (String), email (String), and gender
(char)
Parameterized Constructor: To initialize the variables
class Author
{
static String name,email;
static char gender;
Author(String s,String e,char g)
{
name=s;
email=e;
gender=g;
}
}
class Book
{
String bname,author;
double price;
int qty;
Book(String n)
{
bname=n;
}
public void setPrice()
{
price=500;
}
public double getPrice()
{
return price;
}
public void setQtyInStock()
{
qty=10;
}
public int getQtyInStock()
{
return qty;
}
/*public String setName()
{
name;
}*/
public String getName()
{
return bname;
}
public void getAuthor()
{
System.out.println("Author");
System.out.println("------");
System.out.println("Author name: "+Author.name+",
email: "+Author.email+", Gender: "+Author.gender);
}
}
public class First {
public static void main(String[] args) {
Book book=new Book("telltales");
book.setPrice();
book.setQtyInStock();
System.out.println("Book");
System.out.println("-----");
System.out.println("Name: "+book.getName()+", price:
"+book.getPrice()+" ,quantity: "+book.getQtyInStock());
Author a=new Author("benjamin","xyz@gmail.com",'g');
book.getAuthor();
}
}
Create a child class of Animal named ‘Bird’ and override the parent class methods.
Add a new method named fly().
Create an instance of Animal class and invoke the eat and sleep methods using this
object.
Create an instance of Bird class and invoke the eat, sleep and fly methods using this
object.
class Animal
{
public Animal()
{
System.out.println("new animal");
}
public void eat()
{
System.out.println("eating");
}
public void sleep()
{
System.out.println("sleeping");
}
}
class Bird extends Animal
{
public Bird()
{
super();
System.out.println("bird created");
}
public void eat()
{
System.out.println("bird sleeps");
}
public void sleep()
{
System.out.println("bird sleeps");
}
public void fly()
{
System.out.println("bird flies");
}
}
Inheritance 2
Create a class called Person with a member variable name. Save it in a file called
Person.java
Create a class called Employee that will inherit the Person class.The other data
members of the Employee class are annual salary (double), the year the employee
started to work, and the national insurance number which is a String.Save this in a
file called Employee.java
Your class should have the necessary constructors and getter/setter methods.
Write another class called TestEmployee, containing a main method to fully test your
class definition.
public class Person {
private String name;
}
}
Inheritance 3
Create a school application with a class called Person. Create name and dateOfBirth
as member variables.
Create a class called Teacher that inherits from the Person class. The teacher will
have additional properties like salary, and the subject that the teacher teaches.
Create a class called Student that inherits from Person class. This class will have a
member variable called studentId.
Create a class called College Student that inherits from Student class. This class
will have collegeName, the year in which the student is studying
(first/second/third/fourth) etc.
Create objects of each of this classes, invoke and test the methods that are
available in these classes.
package com.company;
public class Person {
this.name = name;
this.dob = dob;
return name;
return dob;
package com.company;
import com.company.Person;
this.salary = salary;
this.sub = sub;
return salary;
return sub;
package com.company;
super(name, dob);
this.id = id;
return id;
}
}
package com.company;
public CollegeStudent(String name, String dob, int id, String cname1, int year) {
this.cname = cname1;
this.year = year;
@Override
return cname;
return year;
Inheritance
Overriding / Polymorphism
No Topics
Hands-on Assignment Status
. Covered
1 Inheritance
Create a base class Fruit with name ,taste and size as its / Overriding
attributes.
package com.company;
public CollegeStudent(String name, String dob, int id, String cname1, int
year) {
this.cname = cname1;
this.year = year;
@Override
return cname;
}
public int getYear() {
return year;
@Override
public void eat() {
super.eat();
}
}
@Override
public void eat() {
super.eat();
}
}
2
Write a program to create a class named shape. It should
contain 2 methods, draw() and erase() that prints “Drawing
Shape” and “Erasing Shape” respectively.
For this class, create three sub classes, Circle, Triangle and
Square and each class should override the parent class
functions - draw () and erase ().
@Override
public void erase() {
System.out.println("erasing circle");
}
}
@Override
public void erase() {
System.out.println("erasing square");
}
}
@Override
public void erase() {
System.out.println("erasing triangle");
}
}
import java.util.Scanner;
sb.append(s);sb=sb.reverse();//System.out.println(s);System.out.println(sb);
return sb.toString().equals(s);
}
}
2 String/StringBuffer
Write a java program that will concatenate 2 strings and
return the result. The result should be in lowercase.
Example1)
i/p:Sachin,Tendulkar
o/p:sachin tendulkar
Example2)
i/p:Mark,kate
o/p:markate
import java.util.Scanner;
Example1)
i/p:Wipro
o/p:WiWiWiWiWi
import java.util.Scanner;
3 String/StringBuffer
public class Third {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();sc.close();
String rep=s.substring(0, 2);
for(int i=0;i<s.length();i++) System.out.print(rep);
}
4 String/StringBuffer
Write a java program that will return the first half of
the string, if the length of the string is even. It should
return null for odd length string.
Example1)
i/p:TomCat
o/p:Tom
Example2)
i/p:Apron
o/p:null
import java.util.Scanner;
Example1)
i/p:Suman
o/p:uma
The strings will not be the same length, but they may be
empty (length 0).
import java.util.Scanner;
6 String/StringBuffer
7 String/StringBuffer
Given a string, if the first or last chars are 'x',
return the string without those 'x' chars, otherwise
return the string unchanged.
import java.util.Scanner;
Example1)
i/p:ab*cd
o/p:ad
import java.util.Scanner;
9 String/StringBuffer
Given two strings, a and b, print a new string which is
made of the following combination-first character of a,
the first character of b, second character of a, second
character of b and so on.
Any characters left, will go to the end of the result.
Example1)
i/p:Hello,World
o/p:HWeolrllod
import java.util.Scanner;
System.out.println(res);
}
10 String/StringBuffer
Given a string and an integer n, print a new string made
of n repetitions of the last n characters of the string.
You may assume that n is between 0 and the length of the
string, inclusive.
Example1)
i/p:Wipro,3
o/p:propropro
import java.util.Scanner;
11 String/StringBuffer
Given two strings a and b, return a new string, following
the rules given below.
If string b occurs in string a, then the new string should
concatenate the characters that appear before and after of
String b.
Ignore cases where there is no character before or after
the word, and a character may be included twice if it is
in between two string b's.
Example1)
i/p:abcXY123XYijk,XY
o/p:c13i
Example2)
i/p:XY123XY,XY
o/p:13
Example3)
i/p:XY1XY,XY
o/p:11
import java.util.Scanner;
}
No Topics
Hands-on Assignment Status
. Covered
1 Abstract
Create a class called GeneralBank that acts as base class for Classes
all banks. This class has getSavingsInterestRate and
getFixedDepositInterestRate methods, which returns the savings
account interest rate and fixed deposit account interest rate
that the specific bank gives. Since GeneralBank cannot say what
percentage which bank would give, make these methods abstract.
3 Abstract
Create an abstract class Instrument which is having the Classes
abstract function play.
No Topics
Hands-on Assignment Status
. Covered
2 Packages
Create a class called compartment which represents the ship User
compartments with attributes like height, width and breadth. defined
packages
4 Packages
Add the following ideas to the previous hands on: User
Create FourWheeler subpackage under automobile package defined
packages
Logan class extends com.automobile.Vehicle class
public int speed()
– Returns the current speed of the vehicle.
public int gps()
– provides facility to control the gps device
No Topics
Hands-on Assignment Status
. Covered
1 Interfaces
An online library application need to be created for two types
of users/roles-Adults and children. Both of these users should
be able to register an account.
void requestBook
()
registerAccount():
requestBook():
registerAccount():
requestBook function:
2 Interfaces
Write an interface called Playable, with a method
void play();
Let this interface be placed in a package called music.
Write a class called Veena which implements Playable interface.
Let this class be placed in a package music.string
No Topics
Hands-on Assignment Status
. Covered
3 Exception
Handling:
Try-catch
Use
multiple
catch block
Write a program that takes as input the size of the array and
the elements in the array. The program then asks the user to
enter a particular index and prints the element at that index.
Index starts from zero.
5 throws
The method should declare that it throws ArithmeticException.
This exception should be handled in the main method.
7 Exception
A student portal provides user to register their profile. Handling:
During registration the system needs to validate the user User
should be located in India. If not the system should throw an Defined
exception. Exception &
throw
Invoke the method registerUser from the main method with the
data specified and see how the program behaves.
Example1)
i/p:Mickey,US
o/p:InvalidCountryException should be thrown.
The message should be “User Outside India cannot be
registered”
Example2)
i/p:Mini,India
o/p:User registration done successfully
9 Exception
Write a program that accepts 2 integers a and b as input and Handling:
Finally
finds the quotient of a/b.
Example1)
Enter the 2 numbers
block
5
2
The quotient of 5/2 = 2
Inside finally block
Example2)
Enter the 2 numbers
5
DivideByZeroException caught
Inside finally block
No Topics
Hands-on Assignment Status
. Covered
1 I/O Streams
Write a program to count the number of times a character
appears in a File.
3 I/O
Anitha : 1
Harish : 1
Janani : 1
Katari : 1
Manoj : 1
Sureka : 1
Wipro : 6
at : 6
works : 6
No Topics
Hands-on Assignment Status
. Covered
No Topics
Hands-on Assignment Status
. Covered
Create two threads and assign names ‘Scooby’ and ‘Shaggy’ to Setting the
1 the two threads. Display both thread names. name of the
thread.