Question

In: Computer Science

using Java progam Q1: What will be the value of x after the following code is...

using Java progam
Q1: What will be the value of x after the following code is executed?

int x = 10;

while (x < 100)

{

x += 100;

}

Q2: What will be the value of x after the following statements are executed?

int x = 10;

switch (x)

{

case 10:

x += 15;

case 12:

x -= 5;

break;

default:

x *= 3;

}

Q3: What is the output of the following progm?
public static void main(String [] args) {
System.out.print("A");
processRequest();
System.out.print("B");
System.out.print("C");
showMenu();
System.out.print("D");
}
public static void showMenu() {
System.out.print("Q");
}
public static void eraseFile()
{
System.out.print("T");
}
public static void processRequest()
{
System.out.print("2");
}

Q4: Write a method declaration called discount which accepts 2 integers as the formal parameters. The two parameters represent the original price and sale price. The method returns the discount value i.e. the difference between the original price and sale price as an integer.

Q5: Write a program that takes two integer variables and prints the largest number using if-else statements.

Q6: public static ______testNumber(int x)
{
if ( x<100 )

{
return "The number is less than 100";
}

else

{
return "The number is greater or equal 100";

}
}

Q7: Write a program using a conditional operator (&&, ||, !)

1. which takes input from user for variables x and y.

2. it should give an output if and only if two numbers are equal, otherwise prompt message numbers are not equal.

Q8: Write a program to print a table of two using a loop.

Q9: Is the call to the method installApplication() below, valid?: Yes/No?

public static void main(String [] args)

{
installApplication("Angry Birds", "version 1.2");
}
public static void installApplication(String appName, int appVersion)

{
// rest of method not important
}

Q10: What will be the values of ans, x, and y after the following statements are executed?

int ans = 35, x = 50, y = 50;

if (x >= y)

{

ans = x + 10;

x -= y;

}

else

{

ans = y + 10;

y += x;

}

Solutions

Expert Solution

Q1)The final value of x will be 110. This is because initially x is initialised to 10. It satisfies the condition of the while loop, inside which it is incremented by 100. So x now becomes 110. Now the condition of the loop is not satisfied and we come out of the loop.

Q2)20

Initially the value of x is 10. The first case inside the switch block matches and the value of x is incremented by 15. So x now becomes 25. Since there is no break statement, the statement in the next case gets executed and x is decremented by 5, making its value to be 20. Now we encounter a break statement and get out of the switch block.

Q3)A2BCQD

This program simply prints in the order of the function calls which occur in the program.

Q4)


public class Main
{
public static void main(String [] args) {
    System.out.println(discount(100,80));
}

public static int discount(int originalPrice,int salePrice){
    return originalPrice - salePrice;
}

}

Q5)

import java.util.Scanner;
public class Main
{
public static void main(String [] args) {
   Scanner sc = new Scanner(System.in);
   int a = sc.nextInt();
   int b = sc.nextInt();
   
   if(a>b)
    System.out.println(a);
   else
    System.out.println(b);
}
}

Q6)String

We can easily see from the code that anytime return statement is used inside the function, it returns a String value. So the return type of the function will be String.

Q7)

import java.util.Scanner;
public class Main
{
public static void main(String [] args) {
   Scanner sc = new Scanner(System.in);
   int x = sc.nextInt();
   int y = sc.nextInt();
   
   if(x!=y)
    System.out.println("Numbers are not equal");
   else
    System.out.println("Numbers are equal");
}
}

Q8)


public class Main
{
public static void main(String [] args) {
  for(int i = 1;i<=10;++i){
      System.out.println("2 * "+i+" = "+(2*i));
  }
}
}

Q9)NO, the call to the method is invalid since the method's definition tells us that it is supposed to receive the first parameter as a String value and the second parameter as an integer value. But, while calling the method, we are passing both the values as String. Hence the call is invalid.

Q10) x = 0 , y = 50 , ans = 60

Initial values of x,y and ans are 50,50 and 35 respectively. Since x is equal to y, so the first condition x>=y is true.

ans now becomes 60 and the value of x is decremented by y's current value. So it becomes 0.


Related Solutions

1. What is value of X after following operation int X=0x 45; X = X<<3 ;...
1. What is value of X after following operation int X=0x 45; X = X<<3 ; X= What is value of X after following operation int x=0x 40; X = X>>3 ; X= What is value of X after following operation int x=0X FF; X = X & 0x0F; X= What is value of X after following operation int x=0x FF; X = ~X ; X= What is value of X after following operation int X=0x FF; X = X...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers = {22, 33, 44}; for(int k = 0; k < 3; k++) { numbers[k] = numbers[k] + 5; } } 2. What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } 3.What is the value of numbers [3] after the following line...
What is the net present value of Project X if it has the following after tax...
What is the net present value of Project X if it has the following after tax cash flows? The interest rate is 6%              End of year 1 = ($300)        End of year 2 = $500 End of year 3 = $900 Please show equation that is easy to understand, able to put into TI-84.
Using JAVA and NETBEANS Assignment Content For this assignment, you will develop "starter" code. After you...
Using JAVA and NETBEANS Assignment Content For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text file, sort and store the contents of the text file into an ArrayList, then write the sorted contents via an output stream to a separate output text file. Copy and paste the following Java™ code into a JAVA source file in...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
Given the Java code: double x =15; int n=15; Find the return value: a. x/2 b....
Given the Java code: double x =15; int n=15; Find the return value: a. x/2 b. n/2 c. n/2.0 d. (double)(n/2)
If x=3, y=4 what is the value of this Java expression? (double) (x/y) What is the...
If x=3, y=4 what is the value of this Java expression? (double) (x/y) What is the value of this Java expression? (int)(1.0 / 2.0) + (double)(1 / 2) True or False: (double)(x)/y is the same as double (x/y) What is the value of this Java expression? (double)3  
java code Question 2: What are the type and value of each of the expressions below?...
java code Question 2: What are the type and value of each of the expressions below? int [] numbers = { 3, 6, 15, 22, 100, 0 }; double [] decimals = { 3.5, 4.5, 2.0, 2.0, 2.0 }; String [] words = {"alpha", "beta", "gamma"}; numbers[3] + numbers[2] decimals[2] - decimals[0] + numbers[4] words[1].charAt( numbers [0] ) numbers[4] * decimals[1] <= numbers[5] * numbers[0]
What are the contents of the array after the for-loop in the following code?
(IN C)What are the contents of the array after the for-loop in the following code?int array[ SIZE ][ SIZE ] = { { 4, 5, 6, 7, 8 },{ 1, 2, 3, 4, 5 },{ 3, 6, 7, 8, 9 },{ 2, 3, 4, 5, 6 },{ 5, 6, 7, 8, 9 } };int i;int *ptr = array[ 0 ];for( i = 0; i < SIZE * SIZE; i++ ) {if( i % SIZE < 2 ) {*( ptr +...
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT