COMP90041-java代写
时间:2023-06-22
COMP90041 Final
COMP90041
public class TestClass
{
public static void main(String[] args) {
int i = -1;
if ((i > 0) || (1/(i+1) > 10)) {
System.out.println("True, branch. i = " + i);
} else {
System.out.println( "False branch. i= " + i);
}
}
}
COMP90041
public class TestClass {
public static void main(String[] args) {
int[] arr = {1, 8, 4, 11, 7, 14, 10};
int x = arr[6];
for (int i = 0: i < arr.length && arr[i] <= x; i++) {
if (arr[i] % 2 == 0)
System.out.print(i + “ “) ;
}
}
}
COMP90041
public class TestClass
{
public static void main(String[] args) {
boolean[] arr = new boolean[4];
arr[0] = true;
arr[1] = arr.length > 4 ? true : false;
System.out.printf("%b %b %b", arr[0], arr[1], arr[3]);
}
}
COMP90041
public class TestClass {
public static void main(String[] args) {
int intArray[] = {1, 2, 4, 5};
intArray[4] = intArray[4] * 2;
int total = 0;
for(int value : intArray) {
total += value;
}
System.out.println(total);
}
}
public class TestClass {
int x;
public static void main(String[] args)
{
TestClass anObject;
System.out.println(anObject.x):
}
}
COMP90041
In the code below, the Triangle class is a child class of the Shape class. The Shape class declares a
non-static method draw() which will draw a square to the screen. The Triangle class also declares a
non-static method draw() which will draw a triangle onto the screen. Pick the statement that best
applies from the list.
public class TestClass{
public static void main(String[] args) {
ArrayList shapes = new ArrayList():
shapes. add (new Shape () ):
shapes.add(new Triangle());
for (Shape shape: shapes)
shape.draw():
}
}
}
The code will draw two squares onto the screen.
The code will draw a square and then a triangle onto the screen.
The code will not compile because the ArrayList only takes Shape objects, but we are trying to a Triangle
object to it as well.
The code will draw two triangles onto the screen.
COMP90041
public class TestException(
public static void main(String[] args) {
while(true) {
try {
throw new Exception():
} catch (Exception e) {
System.out.println("An exception is caught!"):
break;
} finally {
System.out.println( "Inside the finally block.") :
}
}
System.out.println( "Before exiting.") ;
}
}
1. Before exiting.
2. An exception is caught!
Before exiting.
3. An exception is caught!
Inside the finally block.
4. An exception is caught!
Inside the finally block.
Before exiting.
COMP90041
public class TestException{
public static void main(String[] args) {
while(true) {
try {
//nothing wrong here
} catch (Exception e) {
System.out.println("An exception is caught!") :
break:
} finally {
System.out.println("Inside the finally block.") :
}
}
System.out.println("Before exiting.");
}
}
COMP90041
public class Shape {
//
}
public class Rectangle extends Shape {
//
}
public class Square extends Rectangle {
//.
}
1 A Rectangle is a Square and also a Shape.
2 A Square is a Rectangle but not a Shape.
3 Such inheritance is not allowed in Java.
4 A Rectangle is a Shape but not a Square.
COMP90041
public class Engine {
private boolen isInitialized = true;
public static boolean isInitialized()
return this.isInitialized;
}
}
1 The code will compile and run without a problem, but
an object of the class Engine must be created before the
isInitialized method is called.
2 The code will not compile because isInitialized is a
static method, and it cannot access a non-static instance variable.
3 The code will compile and run without a problem. When the method
isInitialized is called, true will be returned.
4 The code will compile and run without a problem.
When the method isInitialized is called, false will be returned.
COMP90041
public class TestClass
{
public static void main(String[] args)
double[] arr = new double[4];
arr[0] = 1.0;
arr[1] = 4.0:
System.out.println(arr[0] + arr[1] + arr[2]);
}
}
COMP90041
public class Car {
private String make;
private int year;
public Car( ) {
make ="Fiat":
year = 2021;
}
public Car(String make, int year) {
this.make = make;
this. year = year;
}
public String toString() {
return (make + " " + year);
}
public static void main(String[] args) {
Car aCar = new Car ("Kia",2020);
System.out.println(aCar);
}
}
COMP90041
Given the following definitions of class Base (stored in a file named Base.java) and class Derived
(stored in a file named Derived.java). Suppose that this statement "Base anObject = new Derived(;"
appears in a client program, what will be the output of the method call "anObject.methodOne();"?
public class Base {
public void methodOne() {
System.out.print("A"):
methodTwo();
}
public void methodTwo() {
System.out.print("B");
}
public class Derived extends Base {
public void methodOne(){
super.methodOne();
System.out.print(“C");
}
public void methodTwo(){
super.methodTwo();
System.out.print("D");
}
}
COMP90041
public class TestFileReadingExceptions {
public static void main(String[] args) {
Scanner inputStream = null:
try {
inputStream = new Scanner (new FileInputStream(args[1])):
} catch (FileNotFoundException e) {
System.out.println("File not found") ;
}
while (inputStream.hasNextLine()){
String line = inputStream.nextLine():
int number = Integer.parseInt (line);
System.out.println(number);
}
inputStream.close():
} }
1 ArrayIndexOutOfBoundsException
2 NumberFormatException
3 FileNotFoundException
4 NullPointerException
5 ClassCastException
COMP90041
public class TestException {
private String str = "a";
public void methodA() {
try {
str += “b”;
methodB();
} catch (Exception e) {
str += “c”;
}
}
public void methodB() throws Exception {
try {
str += “d";
methodC();
} catch (Exception e) {
throw new Exception();
} finally {
str += “e”;
}
}
str += "f";
public void method() throws Exception {
throw new Exception();
}
public void display0) {
System.out.println(str):
public static void main(String[] args) (
TestException Test = new TestException();
aTest.methodA();
aTest.display();
public void methodC() throws Exception {
throw new Exception();
}
public void display() {
System.out.println(str);
}
public static void main(String[] args) (
TestException Test = new TestException();
aTest.methodA();
aTest.display(); }}
COMP90041
public class TestNestedLoop{
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for ( ?????)
System.out.print(“*");
System.out.println();
}
}
}
*
***
*****
*******
*********
COMP90041
COMP90041
Given that x and y are integer numbers, and that x = 9 and y = 4, what is the value of the following
expression?
(x * y - 10 > 30) || (x /y + x > 5) || (x> y)
Which of the following expression evaluates to 'false'?
8/4== 2
8 > 4 || 8<5
8% 10 == 0
5%2 == 1
COMP90041
The statement
if (x < 0) y = x + 1; else y = x - 1;
can be rewritten using a conditional operator (ternary operator) as:
y = (x < 0)? x + 1 : x - 1;
(x < 0) ? y =x+ 1: y= x - 1;
y = if (x < 0) x: 0
cannot use a conditional operator to rewrite
COMP90041
public class TestString // args —> Comp90041 Exam
{
public static void main(String[] args) {
String aString = args[0];
for (int i = 0; i < aString.length(); i++) {
char aChar = aString.charAt(i);
if (i % 2 == 0) {
System.out.print (Character.toLowerCase (aChar)) ;
} else {
System.out.print (Character.toUpperCase(aChar));
}
}
}
}
COMP90041
What does "overriding a method" mean?
A never-ending loop (deadlock) within a method
Different methods have the same name, but different signatures
The definition of an inherited method can be changed in the definition of a derived class so that it has a
meaning in the derived class that is different from what it is in the base class.
The number of parameters exceeds what is needed in the function body
The relationship between classes and objects is best described as:
objects are instances of classes
classes are instances of objects
classes are programs while objects are variables
objects and classes are the same thing
COMP90041
Given that x and y are integer numbers, and that x = 9 and y = 4, what is the value of the following
expression?
(x-y*y+10>5) &&(× >9)&&(x*x⼀y*y<100)
How many times is the following loop executed?
int x = 20;
do {
X = X - 4;
} while (x >= 0) ;
COMP90041 What is the value of the 'count' variable when the loop ends?
public static void main (String[] args) {
int x= 20:
int count=0;
do{
X = X-2;
count = count+ 1
} while (x>0):
}
public static void main(String[] args) {
int x = 20;
int count = 0;
do {
X = X - 2;
for (int i = 0;i< x % 2; i++) {
count = count + 1;
}
} while (x > 0) ;
}
COMP90041
COMP90041
COMP90041
COMP90041
COMP90041
COMP90041
COMP90041
COMP90041
COMP90041
COMP90041