Question

Given the following class: class Q2 { private int a; private int b; private int c; public void setA(int a){this.a = a; } publ
y 10; What is the output of the following code segment? Q2 obj1 = new Q2(); Q2 obj2 = new Q2(); obji.setA(0); obj1.setB(e); o
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer is D. obj2.c = -1

obj2.c will print -1 because the function setC in class only sets the variable C and getC returns c variable. while loop in Main class will not execute so obj2.setC will remain as it is that is -1.

Code Snippet with output given below :

//////////////////////////////////////////////////////////////////////

class Main
{
   public static void main (String[] args) throws java.lang.Exception
   {
       Q2 obj1 = new Q2();
       Q2 obj2 = new Q2();
       obj1.setA(0); obj1.setB(0);obj1.setC(10);
       obj2.setA(4); obj2.setB(5);obj2.setC(-1);
       while(!obj1.m2(1,3))
       {
           obj2.setC(obj2.getC() + 10);
       }
       System.out.println("obj2.c = " + obj2.getC());
   }
}
class Q2
{
   private int a;
   private int b;
   private int c;
   public void setA(int a){this.a = a;}
   public void setB(int b){this.b = b;}
   public void setC(int c){this.c = c;}
   public int getA(){return a;}
   public int getB(){return b;}
   public int getC(){return c;}
   public int m1(int a, int b){
       return a+b;
   }
   public boolean m2 (int x, int y){
       return m1(x, y) + x + y < 10;
   }
}

//////////////////////////////////////////////////////////////////////

Output obj2.c = -1

Add a comment
Know the answer?
Add Answer to:
Given the following class: class Q2 { private int a; private int b; private int c;...
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
  • Modify the Triangle class (from previous code, will post under this) to throw an exception in...

    Modify the Triangle class (from previous code, will post under this) to throw an exception in the constructor and set routines if the triangle is not valid (i.e., does not satisfy the triangle inequality). Modify the main routine to prompt the user for the sides of the triangle and to catch the exception and re-prompt the user for valid triangle sides. In addition, for any valid triangle supply a method to compute and display the area of the triangle using...

  • Question 19 Given the following class: public class Swapper ( private int x; private String y...

    Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...

  • can someone explain to me what does the dot mean ? and what would be the...

    can someone explain to me what does the dot mean ? and what would be the output and explain it? import java.io.*; public class Green { private int a; private int b; public Green(int aa,int bb) { a=aa; b=bb; } public void equals(Green c) { this.a=c.a;this.b=c.b;} public void fn(Green c) { this.a=3*c.b-c.a; this.b=2*c.a-this.b;} public void gg() { this.b=this.b-1; this.a=this.b-2; } public static void main(String args[]) { Green x=new Green(2,2); Green y=new Green(2,1); Green z=new Green(1,4); int xx=1,yy=2,zz=3; x.fn(y); z.gg(); System.out.println("...

  • the answer must be in java. thank you Question 2 [8 points] Consider the class Class...

    the answer must be in java. thank you Question 2 [8 points] Consider the class Class A below: class Class A implements Serializable{ int a; static int b; transient int c; String s; // Constructor public Class A (int a, int b, int c, Strings){ this.a = a; this.b = b; this.c = c; this.s = s; Complete the class Test to test the serialization and deserialization of the objects of class Class A. State the possible variable values following...

  • Java Inner class class Outer {    private int x=10;    private static int y=20 ;...

    Java Inner class class Outer {    private int x=10;    private static int y=20 ;    publics void M1( ) {          int z=30;        class Inner           {             public void M2()              {                                                                              Sytem.out.println(“sum: ”+ (x+y+z));               }         } Inner i=new Inner();    i.M2() ; /// first call    i.M2(); // second call i.M2(); // third call }   // end of M1        publics static void main(String[] args)       {           Outer O = new...

  • 1. Questions on inheritance. (a) Consider the following program segment: 1 class Array2 2 private: ClassX...

    1. Questions on inheritance. (a) Consider the following program segment: 1 class Array2 2 private: ClassX a: // ClassY b: // assume class ClassY is defined. assume class ClassX is defined . 3 4 5 public: 6 Array2 (int size) ( a new Clas sX [ size]; // a is an array of ClassX objects b new ClassY [size]: 1/ b is an array of ClassY objects 7 8 9 ) 10 ClassX getA() (return a; ) ClassY getB ()...

  • Given this code defining a class (shown with line numbers): 1: class newClass { 2: private:...

    Given this code defining a class (shown with line numbers): 1: class newClass { 2: private: 3: int a; 4: public: 5: void setA(int value) { a = value; } 6: int getA() { return a; } 7: newClass(int value) : a(value) {}; 8: newClass() { a = 0; } 9: }; Which of the following snippets of code, when written in main() where the class above has been defined, will cause a syntax error when compiled? newClass something; something.setA(8);...

  • Draw a UML class diagram (with associations) to show the design of the Java application. public...

    Draw a UML class diagram (with associations) to show the design of the Java application. public class OOPExercises {     public static void main(String[] args) {         //A objA = new A();         B objB = new B();         System.out.println("in main(): ");         //System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         //objA.setA (222);         objB.setB (333.33);         //System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } public class A {     int a = 100;     public A() {...

  • Martian Cubics and Unit Testing You must implement all of the data members and methods described...

    Martian Cubics and Unit Testing You must implement all of the data members and methods described below. RAM is more limited on NASA missions than on your home computer, so you may NOT add any instance variables or static variables to this class other than those described below. You may add methods of your own, as long as they are private. The data type class you are writing is a very general class that could be of use in a...

  • Java question Consider the following code segment. public class KaBoom { public int b; public void...

    Java question Consider the following code segment. public class KaBoom { public int b; public void m1 () {//Your code goes here } public void m2 (KaBoom k) { System, out. print In (k. b); } } Complete the method body of m1 () indicated by the comment line. The m1 () method simply calls the m2 () method by passing in the current KaBoom object variable to m2 ().

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