Question

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);

newClass something;
something.setA(8);
int x = something.getA();

newClass something;
something.a = 8;

newClass something(17);

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

Option3

newClass something;
something.a = 8;

Explanation:

The Error In the above code occurs due to the following reason.

a is a private member of class and hence can't be accessed directly using an object

Add a comment
Know the answer?
Add Answer to:
Given this code defining a class (shown with line numbers): 1: class newClass { 2: private:...
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
  • Given the following class: class Q2 { private int a; private int b; private int c;...

    Given the following class: 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 gets(){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; What is the output of the...

  • 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 ()...

  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

  • 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...

  • Please help, Array does not print properly. I need to print out the 8 class numbers...

    Please help, Array does not print properly. I need to print out the 8 class numbers entered by the user ! Java only using J option pane, you cannot use ulti or anything else only J option pane Program compiles but does not print the array correctly! import javax.swing.JOptionPane; public class programtrial {    public static void main(String[] args) {    int newclass = 0;    int countclass = 0;    final int class_Max = 8;    int[] classarray =...

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

  • JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private...

    JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private Link prev; public CircularList() { // implement: set both current and prev to null } public boolean isEmpty() { // implement return true; } public void insert(int id) { // implement: insert the new node behind the current node } public Link delete() { // implement: delete the node referred by current return null; } public Link delete(int id) { // implement: delete the...

  • Complete the code MyImage and test in your own main Public class MyImage { private char[][]...

    Complete the code MyImage and test in your own main Public class MyImage { private char[][] pixels; private int imageSize; //constructor public MyImage(int imageSize) { pixels = new char[imageSize][imageSize]; for (int i = 0; i < imageSize; ++i) { for (int j = 0; j < imageSize; ++j) { pixels[i][j] = '.'; } } } //print image public void print(){ //add your code } //draw a triangle with '*' public void drawTriangle() { //add your code } //draw a diamond...

  • 3 6 7 10 11 QUESTION 4: Object Orientation (a) Given the provided code: [08] 1...

    3 6 7 10 11 QUESTION 4: Object Orientation (a) Given the provided code: [08] 1 public class Base{ public void doSomething() {} /* Remainder of class omitted */ public class Derived extends Base{ public void do something() {} * Remainder of class omitted */ public class Container<t extends Base> { private T data; public void dort() { data.doSomething(); /* Remainder of Class omitted */ 16 public class Main { public static void main(String[] args) { Container<Derived> instance = new...

  • There are four syntax errors/incorrect statements in the following class definition. List the line numbers of...

    There are four syntax errors/incorrect statements in the following class definition. List the line numbers of the lines that contain errors, and then write the corrected line(s). (4 points) class jetType //Line 1 { //Line 2 public: //Line 3 void setValues(string, int, double); //Line 4 print() const; //Line 5 string getManufacturer() const; //Line 6 double getPrice() const; //Line 7 bool compareSeating(const jetType&); //Line 8 int jetType(); //Line 9 jetType(string); //Line 10 jetType(string, int, double); //Line 11 private; //Line 12 string...

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