In: Computer Science
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.