Question

In: Computer Science

Which of this method of class String is used to obtain a length of String object?...

Which of this method of class String is used to obtain a length of String object?

What is the output of the below Java program with WHILE, BREAK and CONTINUE?

int cnt=0;

while(true)

{

if(cnt > 4)

   break;

  

if(cnt==0)

{

    cnt++;

continue;

}

  System.out.print(cnt + ",");

  cnt++;

}

1,2,3,4

Compiler error

0,1,2,3,4,

1,2,3,4,

Solutions

Expert Solution

Step1:

First of We should be clear with break,continue,and while statements:

while: while is used to iterate again and again over the statements till the condition is true

break: break is used to break the loop and comes out from the loop without executing the remaining statements .

continue: Using this statements curser  will jump to start of the loop without executing the remaining statements.

Step2:

Now Given code is:

  int cnt=0;
        while(true)
       {
          if(cnt > 4)
          break;
          if(cnt==0)
          {
            cnt++;
            continue;
          }
            System.out.print(cnt + ",");
           cnt++;
        }

Step3:

Here cnt is 0 initially

while will try to execute infinitely because true is passed as condition which will always be true

Now here one if condition is there which will break the loop:

if(cnt > 4)
break;

It means when cnt value will gets greater then 4 it will comes out of the loop .

Now we have one more condition is there

 if(cnt==0)
  {
     cnt++;
     continue;
   }

it will run only when cnt is equal to zero it means it will execute first time and increment cnt by 1 and jump to the start of the loop

After that we have

 System.out.print(cnt + ",");
           cnt++;

it will print cnt and a comma ( , )

so Code execution will go like this ..

firstly cnt is 0 so it will come inside second if condition and gets increment by 1 and jump to start of the loop without printing .

next time cnt will be 1 so it will not go in any if condition and 1, will gets printed similarly 2,3,4, gets printed in subsquent iteration .

Now when cnt will be equal to 5 it will gets into the first if condiion and break statements gets executed so it will comes out of the loop .

Output:

so final Output  will be 1,2,3,4,  

Please note that there will be a , at last of the output.

So third option is correct

Thanks


Related Solutions

For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
Write a method that accepts a String object as an argument and displays its contents backward....
Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method. Sample Run java BackwardString Enter·a·string:Hello·world↵ dlrow·olleH↵
Please implement Sample string toString()method for each class and return itself a string, not the output....
Please implement Sample string toString()method for each class and return itself a string, not the output. import java.util.ArrayList; public class Customer extends User{ private ArrayList orders; public Customer(String display_name, String password, String email) { super(display_name, password, email); } @Override public String getPermissionLevel() { return "CUSTOMER"; } public void addOrder(Order order){ this.orders.add(order); } public ArrayList listOrders(){ return this.orders; }; } ---------------- public class ElectronicProduct extends Product{ private long SNo; private String warranty_period; public ElectronicProduct(long SNo, String warranty_period, String productId, String productName,...
Python: How would I modify the class below that takes a string and returns an object...
Python: How would I modify the class below that takes a string and returns an object holding a valid NANP phone number. I am asked to filll in the three methods listed, but underfined, below: __str__(), area_code(), and normalize(). My task is to clean up differently formatted telephone numbers by removing punctuation, such as '(', '-', and the like, and removing and the country code (1) if present. I am asked to start by stripping non-digits, and then see if...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the...
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the List method: void printDogList() //print all dogs in list /* You’ll need an index. Iterate over your list of dog names in a while loop. Use your index to “get” the dog at the current index When you ‘find’ your dog name in the list, print it out */ Method: void findDogUsingWhile(String dogName) /* You’ll need an index. Iterate over your list of dog...
1. A constructor is a special Class member method. It is automatically called when an object...
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented...
Which charging method for objects involves touching a neutral object with a charged object and results...
Which charging method for objects involves touching a neutral object with a charged object and results in both objects sharing the same charge? Select one: a. zap charging b. Inductive charging c. Contact charging d. Frictional charging Which charging method for objects involves bringing a neutral conductor near but not touching a charged object and then grounding one side of the conductor and removing the ground then the charged object resulting in the conductor having a net charge? Select one:...
What are the object oriented concepts and which all object oriented concepts are used in the...
What are the object oriented concepts and which all object oriented concepts are used in the given program? Consider the following code and explain how each of the object oriented concepts are applied in the given program. (CO1) class Vehicle { string brand; public: void honk(); void honk(int); }; void Vehicle::honk() { cout << "Tuut, tuut! \n" ; } void Vehicle::honk(int x) { for(int i=0;i<x;i++) cout << "Tuut, tuut! \n" ; } int main() { Vehicle V1; V1.honk(); V1.honk(3); }
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT