湖南科技学院2015年Java试卷
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java Examination 2014 – 15
Total Marks: 100 Q1. Multiple Choice Questions
(10 Marks)
1.Keyboard is ____a___ device.
a.Input
b.Output
c.Processing
d.Storing
2.Java compiler translates a java source file into a __b____ file
a.HTML
b.Bytecode
c.Javascript
d.JSP
3.____c____ is the brain of the computer.
a.RAM
b.OS
c.CPU
d. BUS
4.Full Form of JVM b
a.Java Variety Machine
b.Java Virtual Machine
c.Java Valuable Machine
d.Java Volume Machine
5.“A Java program must have a main method”. Is this statement true?
a
a.Yes
b.No
c.May Be
d.I don‟t know
6.Which keyword from following is used to define a constant? a
a.Final
b.Constant
c.Const
d.Max
7.Range of INT data type is b
a. - 2147483648 to 2147423647
b. - 32768 to 32767
c.- 128 to 127
d.None of the above
8.Which of the following is pre decrement? b
a.++var
b.- -var
c.var++
d.var- -
9.What is output of the 96 % 4 - 4? b
a.4
b.-4
c.-0
d.0
10.What is the value of k of the boolean k = “TRUE”? c
a.TRUE
b.FALSE
c.ERROR
d.NO OUTPUT
Q2. What will be output of the following code?
(5 Marks)
public class Output_01{
public static void main(String[] args){
float f = 12.5F;
int i = (int) f;
char x = …a‟;
System.out.println(f);
System.out.println(i);
System.out.println(++x);
System.out.println(“1” + 1);
System.out.println(1 + “Welcome”+ 1+1);
}
}
The result:
12.5
12
b
11
1Welcome11
Q3. Write a Java Program to find an input year is leap year or not.
(5 Marks)
(Hint : leap year is divisible by 4)
Example:
Input :Enter a year : 2012
Output :2012 is a leap year.
Input :Enter a year : 2017
Output : 2017 is not a leap year.
代码:
import java.util.*;
public class FindLeapYear {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a year :");
int year = input.nextInt();
if((year % 4 == 0 && year % 100 !=0 )|| year % 400 == 0) System.out.println(year+" is a leap year");
else
System.out.println(year+" is not a leap year");
}
}
Q4. Write a Java Program to Print the following Pattern.
(15 Marks)
n = 4 n = 5
1 2 3 4 1 2 3 4 5
1 2 3 1 2 3 4
1 2 1 2 3
1 1 2
1
The code:
//package PrintPattern;
public class PrintPattern {
public static void main(String[] args){
int n = 4;
for(int a = n;a > 0;a--){
for(int y = 1;y < a-1;y++)
System.out.print("*");
for(int b = 1;b <= a;b++) System.out.print(b); System.out.println();
}
}
}
Q5. Create a java method that find the maximum of three integer. Use the following method header
(15 Marks)
public static int maxNum(int num1,int num2,int num3)
Example 1
int n1 = 2;
int n2 = 5;
int n3 = 100;
int max = maxNum(n1,n2,n3)
System.out.printl n(“Maximum is ”+max) // Maximum is 100 The code :
class FindMaximum{
public static void main(String[] args){
int n1 = 10;
int n2 = 30;
int n3 = 20;
int max = maxNum(n1,n2,n3);
System.out.println("The maximum is "+max);
}
public static int maxNum(int num1,int num2,int num3){
int maxnum = 0;
if(num1 > num2)
maxnum = num1;
else
maxnum = num2;
if (maxnum < num3)
maxnum = num3;
return maxnum;
}
}
Q6. Write a Java Program that reads 5 integers in an array and displays the average of the numbers in array(15 marks) Example
Input : Enter 5 integers : 4 6 12 8 10
Output :Average is : 8 // average is (4+6+12+8+10) / 5 The code:
import java.util.*;
class DisplayTheAverage{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] a = new int[5];
int sum = a[0];
System.out.print("Enter "+a.length+" integers: ");
for(int i = 0;i < a.length; i++){
a[i] = input.nextInt();
}
for(int j = 0;j < a.length; j++)
sum = sum + a[j];
System.out.println("Average is "+(sum/a.length));
}
}
Q7. Write a Java Program to Sort a string
(15 Marks)
Example 1:
Input: Please enter a string : acbed
Output: Sorted String : abcde
Example 2:
Input: Please enter a string : hello
Output: Sorted String : ehllo
Q8. Fan Class (20 marks)
Design a class named Fan to represent a fan. The class contains:
1.Three constants named SLOW, MEDIUM and FAST with value 1,
2, and 3 to denote the fan speed.
2.An int data field named speed that specifies the speed of the
fan(default SLOW).
3.A boolean data field named on that specifies whether the fan is
on(default false).
4.A double data field named radius that specifies the radius of the
fan(default 5).
5.A string data field named color that specifies the color of the fan
(default blue).
6.A no-argument constructor that creates a default fan.
7.The accessor and mutator methods for all four data field.
8.A method named toString() that returns a string description for the
fan. if the fan is on, the method returns the fan speed, color and
radius in one combined string. if the fan is not on, the method
returns fan color and radius along with the string "fan is off" in one combine string.
The code :
class Fan{
public static void main(String[] args){
Fan a = new Fan();
System.out.println(a.toString());
Fan b = new Fan();
b.setspeed(3);
b.seton(false);
System.out.println(b.toString());
Fan f2 = new Fan(3,true,10,"RED");
System.out.println(f2.toString());
}
final int SLOW = 1;
final int MEDIUM = 2;
final int FAST = 3;
int speed = SLOW;
boolean on = false;
double radius = 5;
String color = "blue";
Fan(){}
Fan(int newspeed,boolean newon,double newradius,String newcolor){
speed = newspeed; on = newon; radius = newradius; color = newcolor;
}
public int getspeed(){
return speed;
}
public boolean geton(){
return on;
}
public double getradius(){
return radius;
}
public String getcolor(){
return color;
}
void setspeed(int newspeed){
speed = newspeed;
}
void seton(boolean newon){
on = newon;
}
void setradius(double newradius){
radius = newradius;
}
void setcolor(String newcolor){
color = newcolor;
}
public String toString(){
String status = "";
if(this.on){
status = "The fan is on ,Speed of the fan is "+this.getspeed()+" ,Radius of the fan is
"+this.getradius()+" ,Color of the fan is "+this.getcolor();
} else {
status = "The fan is off ,Radius of the fan is "+this.getradius()+" ,Color of the fan is "+this.getcolor();
}
return status;
}
}。