EE202 Questions-Samples
- 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- 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- 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- 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;
}
20- What would be the value of discountRate after the following statements are executed?
doublediscountRate;
charcustType = 'B';
switch (custType)
{
case 'A':
discountRate = .08;
break;
case 'B':
discountRate = .06;
case 'C':
discountRate = .04;
default:
discountRate = 0.0;
}
0.0- 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- What will be the value of x after the following code is executed?
int x, y = 15, z = 3;
x = (y--) / (++z);
3- what would be a valid method call for the following method?
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);- What will be returned from the following method?
public static intmethodA()
{
double a = 8.5 + 9.5;
return a;
}
This is an error- Given the following code, what will be the value of finalAmount when it is displayed?
public class Order
{
privateintorderNum;
private double orderAmount;
private double orderDiscount;
public Order(intorderNumber, double orderAmt,
doubleorderDisc)
{
orderNum = orderNumber;
orderAmount = orderAmt;
orderDiscount = orderDisc;
}
publicintgetOrderAmount()
{
returnorderAmount;
}
publicintgetOrderDisc()
{
returnorderDisc;
}
}
public class CustomerOrder
{
public static void main(String[] args)
{
intordNum = 1234;
doubleordAmount = 580.00;
doublediscountPer = .1;
Order order;
doublefinalAmount = 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.- 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- Write a method that prints your name, age, and major. The method should accept no parameters and return no value.
public void myInfo() {
System.out.println(“Name:\tJohn Smith”);
System.out.println(“Age:\t30”);
System.out.println(“Major:\tBasket Weaving”);
}
- 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++;
}
- 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.
importjava.util.Scanner;
importjava.util.Random;
public class RandomInteger {
public static void main(String [] args) {
intinputNum, 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 + “.”);
}//end main
}//end class
- 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;
}
- 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.
publicbooleancontainsPair(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;
}
- Write a method that accepts an array of integers and returns the smallest value in the list.
publicint smallest(int[] list) {
intcurrentSmallest = list[0];
for(int i = 0; i < list.length; i++)
if(list[i] < currentSmallest)
currentSmallest = list[i];
returncurrentSmallest;
}