Question 1
Question text
___ and _____ are the access specifiers that can be applied to top level Class.
Feedback
Question 2
Question text
class Sample{
private double num = 100;
private int square(int a){
return a*a;
}
}
public class Test{
public static void main(String args[]){
Sample obj = new Sample();
System.out.println(obj.num);
System.out.println(obj.square(10));
}
}
Feedback
Question 3
Question text
Choose the appropriate access specifier for the attribute value so that it can be accessed from anywhere.
class Test
{
int value;
}
Feedback
Choose the appropriate access specifier for the attribute value so that it can be accessed from anywhere.
class Test
{
[public] int value;
}
Question 4
Question text
Choose the appropriate return type for the getters and setters provided below.
class Test
{
private int value;
public setValue(int value){//some code}
public getValue(){//some code}
}
Feedback
Choose the appropriate return type for the getters and setters provided below.
class Test
{
private int value;
public [void] setValue(int value){//some code}
public [int] getValue(){//some code}
}
Question 5
Question text
0 0.0 101
public class Book { private int bookId; private double bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; } } class Test { public static void main(String a[]) { Book bobj=new Book(); } }
Feedback
Once we create an object, the default value will be assigned for each attribute. First print the values, then set the values and again print the values.
0 0.0 101
public class Book { private int bookId; private double bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; } } class Test { public static void main(String a[]) { Book bobj=new Book(); [System.out.println(bobj.getBookId());] [System.out.println(bobj.getBookPrice());] [bobj.setBookId(101);] [System.out.println(bobj.getBookId());] } }
Question 6
Question text
Consider the below code snippet and determine the output.
class Student
{ private int studentId;
private float average;
}
class Test
{
public static void main(String a[])
{
Student s=new Student();
s.studentId=123;
System.out.println(s.studentId);
}
}
Feedback
Private variables can be accessed only within the class. They cannot be accessed outside the class.
Question 7
Question text
The below code snippet shows an error
cannot find symbol:
System.out.println("BookId:"+bobj.getId());
public class Book {
private int bookId;
private double bookPrice;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
}
public class Test {
public static void main(String[] args) {
Book bobj=new Book();
bobj.setBookId(123);
bobj.setBookPrice(500);
System.out.println("BookId:"+bobj.getId());
System.out.println("BookPrice:"+bobj.getBookPrice());
}
}
Analyze the above code and select the correct reason for the error.
Feedback
When we specify the methodname or variablename or classname wrongly,then we will get an error “cannot find symbol”. The compiler tries to fetch the methodname "getId" from the book class, where it is not defined.
Question 8
Question text
Arrange the code in the correct sequence, so that the program compiles successfully.
- public class Employee {
private int employeId;
private float salary; - public void setSalary(float salary1) {
- if(salary>0){
salary=salary1;
} - }
}
Feedback
Your answer is correct.
The purpose of the setter method is to set a valid value for the attribute, by doing the necessary validations
Comments
Post a Comment