Question

What is the output of the code below? public class Hobbits { String name; public static...

What is the output of the code below?

public class Hobbits {

String name;

public static void main(String [] args) {

Hobbits [] h = new Hobbits[3];

int z = -1;

while (z < 2) {

z = z +1;

h[z] = new Hobbits();

h[z].name = "bilbo";

if (z == 1) {

h[z].name = "frodo";

}

if (z == 2) {

h[z].name = "sam";

}

system.out.println(h[z].name + " is a ");

system.out.println("good Hobbit name");

}

}

}

Output:

a. bilbo is a good Hobbit name

b. bilbo is a good Hobbit name

frodo is a good Hobbit name

c. bilbo is a good Hobbit name

sam is a good Hobbit name

d. bilbo is a good Hobbit name

frodo is a good Hobbit name

sam is a good Hobbit name

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

bilbo is a good Hobbit name
frodo is a good Hobbit name
sam is a good Hobbit name

Option d

Add a comment
Know the answer?
Add Answer to:
What is the output of the code below? public class Hobbits { String name; 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
  • What is output 1 in the code below? import java.util."; public class StackDemo { public static...

    What is output 1 in the code below? import java.util."; public class StackDemo { public static void main(String [] args) { Stack<Integer> intStack = new Stack<>(); Stack<Integer> tempStack = new Stack<>(); intStack.push(18); intStack.push(21); intStack.push(25); tempStack = intStack; while (!tempStack.empty() { System.out.print(tempStack.peek()+"");//output 1 tempStack.pop(); 3 System.out.println(); System.out.println(intStack.peek()); //output 2 25 18 21 • 25 21 18

  • What is the output of the following code? Explain your answer. public class Test {public static...

    What is the output of the following code? Explain your answer. public class Test {public static void main (String [] args) {System.out. print((int) 5.6); System.out.println((int) 5.2);}}

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • Identify a logical error in the following code and fix it. public class test1 {    ...

    Identify a logical error in the following code and fix it. public class test1 {     public static void main(String[] args){         int total;         float avg;         int a, b, c;         a=10;         b=5;         c=2;         total=a+b+c;         avg=total/3;         System.out.println("Average: "+avg);     } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...

  • Explain in detail what the code below does: public class MyClass {       public static void...

    Explain in detail what the code below does: public class MyClass {       public static void main(String args[]) {              System.out.println(isUniqueChars("something"));       }       public static boolean isUniqueChars(String str) {             int checker = 0;                                                                                               for (int i = 0; i < str.length(); ++i) {                         int val = str.charAt(i) - 'a';                         if ((checker & (1 << val)) > 0) return false;                         checker |= (1 << val);             }             return true;...

  • What output is produced by the following program? public class MysteryNums public static void main(String[] args)...

    What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);

  • What will be the output of the following Java code? class Output { public static void...

    What will be the output of the following Java code? class Output { public static void main(String args[]) { boolean a = true; boolean b = false; boolean c = a ^ b; System.out.println(!c); } } a) 0 b) 1 c) false d) true

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

  • What will the code shown below print to the console? public class Cascade public static void...

    What will the code shown below print to the console? public class Cascade public static void print(int arg){ for(int i=0; i<arg; i++){ System.out.print(""); } System.out.println(""); if(arg > 1){ print(arg-1); } } public static void main(String[] args) { print(4); } } E B IV AA- IES XX, SE V GT 12pt Paragraph -

  • What is wrong the following code? Explain. public class Test { public static void main(String[] args)...

    What is wrong the following code? Explain. public class Test { public static void main(String[] args) { A a = new A(5.0); } class A { int value = 2; } }

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
ADVERTISEMENT