• Tidak ada hasil yang ditemukan

int x = 5, y = 20; x += 32; y /= 4; System.out.println("x = " + x + ", y = " + y); x = 37, y = 5 2

N/A
N/A
Protected

Academic year: 2025

Membagikan "int x = 5, y = 20; x += 32; y /= 4; System.out.println("x = " + x + ", y = " + y); x = 37, y = 5 2"

Copied!
7
0
0

Teks penuh

(1)

EE202 Questions-Samples

1. What will be displayed as a result of executing the following code?

int x = 5, y = 20;

x += 32;

y /= 4;

System.out.println("x = " + x + ", y = " + y);

x = 37, y = 5

2. What will be the value of z as a result of executing the following code?

int x = 5, y = 28;

float z;

z = (float) (y / x);

5.0

3. What would be the value of bonus after the following statements are executed?

int bonus, sales = 1250;

if (sales > 1000) bonus = 100;

if (sales > 750) bonus = 50;

if (sales > 500) bonus = 25;

else

bonus = 0;

0

4. What would be the value of x after the following statements were executed?

int x = 10;

switch (x) {

case 10:

x += 15;

case 12:

x -= 5;

break;

default:

x *= 3;

(2)

} 20

5. What would be the value of discountRate after the following statements are executed?

double discountRate;

char custType = 'B';

switch (custType) {

case 'A':

discountRate = .08;

break;

case 'B':

discountRate = .06;

case 'C':

discountRate = .04;

default:

discountRate = 0.0;

} 0.0

6. In the following code, what values could be read into number to terminate the while loop?

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = keyboard.nextInt();

while (number < 100 && number > 500) {

System.out.print("Enter another number: ");

number = keyboard.nextInt();

}

The boolean condition can never be true

7. What will be the value of x after the following code is executed?

int x, y = 15, z = 3;

x = (y--) / (++z);

3

8. what would be a valid method call for the following method?

(3)

public static void showProduct (int num1, double num2) {

int product;

product = num1 * (int)num2;

System.out.println("The product is " + product);

}

showProduct(10, 4.5);

9. What will be returned from the following method?

public static int methodA() {

double a = 8.5 + 9.5;

return a;

}

This is an error

10. Given the following code, what will be the value of finalAmount when it is displayed?

public class Order {

private int orderNum;

private double orderAmount;

private double orderDiscount;

public Order(int orderNumber, double orderAmt, double orderDisc)

{

orderNum = orderNumber;

orderAmount = orderAmt;

orderDiscount = orderDisc;

}

public int getOrderAmount() {

return orderAmount;

}

public int getOrderDisc()

(4)

{

return orderDisc;

} }

public class CustomerOrder {

public static void main(String[] args) {

int ordNum = 1234;

double ordAmount = 580.00;

double discountPer = .1;

Order order;

double finalAmount = order.getOrderAmount() – order.getOrderAmount() * order.getOrderDisc();

System.out.println("Final order amount = $" + finalAmount);

} }

There is no value because the object order has not been created.

11. What will be the value of x[1] after the following code is executed?

int[] x = {22, 33, 44};

arrayProcess(x);

public static void arrayProcess(int[] a) {

for(int k = 0; k < 3; k++) {

a[k] = a[k] + 5;

} }

38

12. Write a method that prints your name, age, and major. The method should accept no parameters and return no value.

(5)

public void myInfo() {

System.out.println(“Name:\tJohn Smith”);

System.out.println(“Age:\t30”);

System.out.println(“Major:\tBasket Weaving”);

}

13. Rewrite the following for loop as a while loop.

for(int i = 0; i < MAX; i++) //loop body

int i = 0;

while(i < MAX) { //loop body i++;

}

14. Write a short program that allows the user to input a positive integer and then outputs a randomly generated integer between 1 and the input number.

import java.util.Scanner;

import java.util.Random;

public class RandomInteger {

public static void main(String [] args) { int inputNum, randNum;

Scanner input = new Scanner(System.in);

Random generator = new Random();

System.out.print(“Please enter a positive integer: “);

inputNum = input.nextInt();

randNum = generator.nextInt(inputNum) + 1;

System.out.println(“Your random number is “ + randNum + “.”);

(6)

}//end main }//end class

15. Write a method called average that accepts an array of floating point numbers and returns their average.

public double average(float[] array) { double sum = 0;

for(int i = 0; i < array.length; i++) sum += array[i];

return (double) sum/array.length;

}

16. Write a method called containsPair that accepts an array of integers as a parameter and returns true if it contains two integers that are equal.

public boolean containsPair(int[] values) { for(int i = 0; i < values.length; i++) {

for(int j = i+1; j < values.length; j++) if(values[i] == values[j])

return true;

}

return false;

}

17. Write a method that accepts an array of integers and returns the smallest value in the list.

public int smallest(int[] list) { int currentSmallest = list[0];

for(int i = 0; i < list.length; i++) if(list[i] < currentSmallest)

currentSmallest = list[i];

return currentSmallest;

}

(7)

Referensi

Dokumen terkait

Dua buah lingkaran jarak kedua pusatnya adalah 17 cm, panjang garis singgung persekutuan dalamnya 15 cm, jika panjang salah satu jari-jarinya 5 cm, maka

Segi-8 beraturan merupakan gabungan dari 8 segitiga sama kaki dengan besar sudut yang diapit oleh kedua sisi yang sama panjang adalah sebesar 45 0. Dari soal

Diketahui 2 buah titik A(2,1) dan titik B(8,5) bila titik A sebagai titik awal dan titik B sebagai titik akhir, maka buatlah garis yang menghubungkan titik tersebut dengan

Người ta dựng các hình chữ nhật OABC trong góc phần tư thứ nhất của hệ tọa độ như hình vẽ, với A thuộc trục hoành, C thuộc trục tung, B thuộc đồ thị yex.. Tìm diện tích lớn nhất của

Viết phương trình tổng quát của đường thẳng AB biết đỉnh D có hoành độ dương và D nằm trên đường thẳng :5d x  y 1 0... Thử lại

Chứng minh rằng tồn tại phần tử thuộc tất cả 2018 tập hợp đã cho.. Họ và tên thí

1- PhD Candidate, Department of Nursing, University of Social Welfare and Rehabilitation Sciences AND Faculty Member, Shahed University, Tehran, Iran 2- Associate Professor, Social

Walaubagaimanapun, masalah timbul di peringkat memuktamadkan kos pengawasan perunding kerana perunding berkehendakkan bayaran berdasarkan Manual Perolehan Perkhidmatan Perunding yang