You are on page 1of 8

FACULTY OF ENGINEERING & TECHNOLOGY, SRM UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

15SE205J - Programming in Java


CYCLE TEST 1 (Batch I - EVEN)

B.Tech. Computer Science and Engineering – IV semester Year: 2016-2017


Max Marks: 50 Date: 7.3.17 Duration: 2Hrs

PART - A

Answer any 5: 5 x 4 = 20
1. Write a program to read the below data of various types using Scanner class.
Name - string, Gender - character, age - integer, ContactNo - long, CGPA – double

import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your gender");
char gender = sc.next().charAt(0);
System.out.println("Enter your age");
int age=sc.nextInt();
System.out.println("Enter your contact no");
long contacto=sc.nextLong();
System.out.println("Enter your CGPA");
double fee=sc.nextDouble();
System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Age: "+age);
System.out.println("Mobile Number: "+mobileNo);
System.out.println("CGPA: "+cgpa);
sc.close();
}
}

2. Consider the following code snippet.


int i = 10;
int n = i++ % 5;
(a) Print the output for the given code
(b) Instead of postfix version use ++i and print the output
(a) i=11, n=0
(b) i=11 , n=1

3. "JAVA is platform-independent" - Justify.

A programming language or technology is said to be platform independent if and only if


which can run on all available operating systems with respect to its development and
compilation. (Platform represents Operating System).

Java is a platform independent programming language, Because when you install jdk
software on your system then automatically JVM are installed on your system. For every
operating system separate JVM is available which is capable to read the .class file or byte
code. When we compile your Java code then .class file is generated by javac compiler
these codes are readable by JVM and every operating system have its own JVM so JVM is
platform dependent but due to JVM java language is become platform independent.

4. Write the importance of keywords public, static and void in main() method.
Public : is an Access Modifier, which defines who can access this Method. Public means that
this Method will be accessible by any Class(If other Classes are able to access this Class.).

Static : is a keyword which identifies the class related thing. This means the given Method or
variable is not instance related but Class related. It can be accessed without creating the
instance of a Class.

Void : is used to define the Return Type of the Method. It defines what the method can
return. Void means the Method will not return any value.

5. Comapare equals() and == operator for comparing two strings and give an example.
We can compare string in java on the basis of content and reference. The String equals()
method compares the original content of the string. It compares values of string for
equality.

Example:
class Teststringcomparison1{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
} }
The = = operator compares references not values.
Example:
class Teststringcomparison3{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}

6. Write a program to displays all the numbers from 1 to 50 that are divisible by 5 and
6.
class print
{
public static void main(String ar[])
{
int i;
System.out.println(“No. divisible by 5 and 6 in between 1 and 50”);
for(i=1;i<=50;i++)
{
if(i%5==0 && i%6==0)
System.out.print(i +”,”);
}
}
}

7. Give an example to get a string input from the user using BufferedReader class and
print the data.

import java.io.*;
class BufferedReaderStringDemo
{
public static void main(String args[]) throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");

str = br.readLine();
System.out.println(str);
}
}

PART - B

Answer all: 2 x15 = 30


8a. i. Write the need of static member and static class and explain how to access them?
Give a suitable example. (10 Marks)

If you declare any variable as static, it is known static variable.

o The static variable can be used to refer the common property of all objects (that is not
unique for each object) e.g. company name of employees,college name of students
etc.
o The static variable gets memory only once in class area at the time of class loading.
o Outer classes cannot be static, but nested/inner classes can be. Static classes basically
helps you to use the nested/inner class without creating an instance of the outer class.

//Program of static variable

class Student8{
int rollno;
String name;
static String college ="ITS";

Student8(int r,String n){


rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[]){
Student8 s1 = new Student8(111,"Karan");
Student8 s2 = new Student8(222,"Aryan");
s1.display();
s2.display();
}
}
//Program of static class
public class Filter {
Vector criteria = new Vector();
public addCriterion(Criterion c) {
criteria.addElement(c);
}
public boolean isTrue(Record rec) {
for(Enumeration e=criteria.elements();
e.hasMoreElements();) {
if(! ((Criterion)e.nextElement()).isTrue(rec))
return false;
}
return true;
}
public static class Criterion {
String colName, colValue;
public Criterion(Stirng name, String val) {
colName = name; colValue = val;
}
public boolean isTrue(Record rec) {
String data = rec.getData(colName);
if(data.equals(colValue)) return true;
return false;
}
}
}

ii. Describe java type casting operations with example program. (5 Marks)
• Java is known as a strongly typed language.
– In a Strongly Typed Language before a value is assigned to a variable, Java
checks the types of the variable and the value being assigned to it to determine
if they are compatible.
• In assignment statements where values of a lower-ranked data types are stored in
variables of higher-ranked data types, Java automatically converts the lower-ranked
value to the higher-ranked type.
– This is called a Widening Conversion.
double x;
int y = 10;
x = y;
– A Narrowing Conversion conversion of a value to a lower-ranked type.
• These can cause a loss of data, so Java does not automatically perform
them.
– Imagine converting from double to int…
• You can perform narrowing conversions with type casting operators.
class IntToByteConversion
{
public static void main(String arg[])
{
int a = 350;
byte b;

b = (byte) a;

System.out.println("b = " + b );

}
}

(or)

8b. Define the term array? With example programs explain in detail about single
dimensional array and two dimensional array. (6+9 Marks)
• An array is an ordered list of values. A particular value in an array is referenced using
the array name followed by the index in brackets.
• The values held in an array are called array elements. An array stores multiple values
of the same type – the element type. The element type can be a primitive type or an
object reference
• Therefore, we can create an array of integers, an array of characters, an array of String
objects, an array of Coin objects, etc.
Any example program can be explained using single dimensional array and two
dimensional array.

9a. Design a class StudentData with the following data members private int stuID,
String stuName, int stuAge. Store the student data using constructor and methods.
Then display the student information. (Note: Store multiple data and display)

class Student{
int stuid;
String stuname;
int stuage;
Student(int i, String n, int a)
{
stuid =i;
stuname =n;
stuage =a;
}

void insert(int i, String n, int a) {


stuid =i;
stuname =n;
stuage =a;
}
void display()
{
System.out.println(id+" "+name+" "+age);
} }
public class TestStudent {
public static void main(String[] args) {
Student s1=new Student();
Student s2=new Student();
Student s3=new Student();
s1.insert(101,"ajeet",34);
s2.insert(102,"irfan",40);
s3.insert(103,"nakul",45);
s1.display();
s2.display();
s3.display();
Student s4=new Student(104,"vijay",35);
Student s5=new Student(105,"jay”,40);
s4.display();
s5.display();
}
}

(or)

9b. (i) write a program to find the area of a triangle, rectangle and square using method
overloading. (8 Marks)

class OverloadDemo
{
void area(int x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
}
void area(int x, int y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(double x, double y)
{ double z =x*y/2;
System.out.println("the area of the triangle is "+z+" sq units");
}
}
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5,3.2);
}
}

(ii) Write a program to find the area of cube using constructor overloading. (7
Marks)

class cube
{
int side;
cube(int x)
{
side=x;
}
cube()
{
side=20;
}

float GetData()
{
return(6*side*side);
}
}
class ConstructorOverloading
{
public static void main(String args[])
{
cube c1 = new cube();
cube c2 = new cube(10);
System.out.println("Area of First cube is : "+c1.GetData());
System.out.println("Area of Second cube is : "+c2.GetData());

}
}

You might also like