Question

need help with java questions

The following snippet of code would produce what outcome? public static void main(String 2 [] args) { int day = 5; switch (daWhat will be the output of the following code: int x = 20; int y = 40; if (x > 10) { if (y > 50) { System.out.println(Hello,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)code:

=====================================

public class MyClass {
public static void main(String args[]) {
int day=5;
  
switch(day){
case 1:System.out.println("Monday");
case 2:System.out.println("Tuesday");
case 3:System.out.println("Wednesday");
case 4:System.out.println("Thursday");
case 5:System.out.println("Friday");
case 6:System.out.println("Saturday");
case 7:System.out.println("Monday");break;
default:System.out.println("Invalid day");
}
}
}

=================================================

public class MyClass { public static void main(String args[]) { int day=5; 1 2- 3 4 5 6 7 8 9 10 11 12 13 14 15 16 switch(day

The o/p is :

Friday

Saturday

Sunday

because , it will enter the switch with value of day as 5. So it checks for case 5, and it prints the Friday ,but since we dont have a break statement , the subsequent cases also exceuted.

so case 6 is executed and Saturday is printed. again we dont have a break statement .So it goes to next case 7 and prints Sunday.

now in case 7 we have a break statement ,so switch is exited and program stops running.

===========================================================

2)program:

============================================================

public class MyClass {
public static void main(String args[]) {
  
int x=20;
int y=40;
  
if(x>10){
if(y>50){
System.out.println("Hello,Frined.");
}else{
System.out.println("GoodBye,Frined.");
}
}else{
if(y>50){
System.out.println("Hello,Enemy.");
}else{
System.out.println("GoodBye,Enemy.");
}
}
}
}

=================================================

4 1. public class MyClass { 2 public static void main(String args[]) { 3 int x=20; 5 int y=40; 6 if(x>10){ 8 if(y>50){ 9 Syst

The O/p is GoodBye,Friend

because ,since x=20; if(x>10) -> (20>10)is true and it enters the if statement .

next if(y>50) -> (40>50) is false , so it will go to else and prints the " GoodBye Friend. "

it will not go to else of line 13 ,since we entered already the corresponding if in line 7.

========================================================================

Please upvote and comment for doubts

Add a comment
Know the answer?
Add Answer to:
need help with java questions The following snippet of code would produce what outcome? public static...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks {...

    Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + "."); else System.out.println("Invalid Entry"); System.out.print("Try again (Y/N): "); ch = scanner.next().charAt(0); }while(ch=='Y'); }...

  • Need Help Filling in Areas: /** Based on a Big Java problem You have to write...

    Need Help Filling in Areas: /** Based on a Big Java problem You have to write a static method that removes every other element from a linked list. Expected output: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] [Monday, Wednesday, Friday] [Wednesday] [] */ public class DownSizeTester { public static void main(String[] args) { LinkedList list = new LinkedList() ; list.add("Sunday") ; list.add("Monday") ; list.add("Tuesday") ; list.add("Wednesday") ; list.add("Thursday") ; list.add("Friday") ; list.add("Saturday") ; System.out.println(list) ; downsize(list) ; System.out.println(list) ; downsize(list)...

  • Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender {...

    Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender { MyDate myDate; Day day; enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }    public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter date below :"); System.out.println("Enter day :"); int day = sc.nextInt(); System.out.println("Enter Month :"); int month = sc.nextInt(); System.out.println("Enter year :"); int year = sc.nextInt(); MyDate md = new MyDate(day, month, year); if(!md.isDateValid()){ System.out.println("\n Invalid date . Try...

  • THIS CODE SHOULD BE MODIFIED TO READ SuperMarket.dat instead of Inputting and Outputting. SuperMarket.dat Monday 4...

    THIS CODE SHOULD BE MODIFIED TO READ SuperMarket.dat instead of Inputting and Outputting. SuperMarket.dat Monday 4 Monday 6 Monday 2 Tuesday 3 Tuesday 2 Wednesday 3 Wednesday 5 Wednesday 7 Thursday 5 Friday 4 Friday 3 Friday 4 Saturday 8 Saturday 8 Saturday 8 Sunday 0 QUESTION: Write the control break code, including the code for the dayChange() method, in the main() method. // SuperMarket.java - This program creates a report that lists weekly hours worked // by employees of...

  • Code goes like: #include <stdio.h> /* * askGrade(): * Asks the student for a letter grade...

    Code goes like: #include <stdio.h> /* * askGrade(): * Asks the student for a letter grade * (A = 90-100, B = 80-89, etc.) * then gives the appropriate letter grade in upper case. */ void askGrade() { int grade;    printf("Enter your grade:"); scanf("%d", &grade);    printf("Your grade is: ");    // BEGIN YOUR CODE    // TODO: Use if-else statements to print the right letter grade.    // END YOUR CODE    printf("\n"); } /* * dayOfTheWeek(): *...

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

  • Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program...

    Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program that prompts the user to enter a year, month, day of month and it displays the name of the week. Here is my source code. I need it broke down into a flowchart. public static void main(String[] args) {        //all variables are initialized to 0, because local variables require at least an initial value before use        Scanner input = new...

  • Answer the following questions with the given data set: 1 Distribution of accidents Accidents Weekunday Monday...

    Answer the following questions with the given data set: 1 Distribution of accidents Accidents Weekunday Monday Tuesday Wednesday Thursday Day of the Mon Week |- | 10|29 Frequency 36 Friday Saturday 41 50 63 40 PrintDone A researcher wanted to determine whether certain accidents were uniformly distributed over the days of the week. The data show the day of the week for n =299 randomly selected accidents. Is there reason to believe that the accident occurs with equal frequency with...

  • Hello, I have a bug in my code, and when I run it should ask the...

    Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT