NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

1.Memory allocation
Which of these operators is used to allocate memory for an object?
Options
malloc
alloc
new R
give
Correct Answer
2.Create object
Which of the following is a valid statement to create an object of class Bird in Java ?
Options
Bird b = new Bird;
Bird b = new Bird(); R
Bird b;
new Bird b;
Correct Answer
3.Find correct statement
Send Feedback
Figure out the correct statement
class Car{
String color;
double price;
}

public class CarUse {
public static void main(String[] args) {
Car c = new Car();
//Figure out the correct statement to set color of the car object referred by c to “white”
}
}
Options
c->color = “white”
Car.color = “white”
c.color = "white"; R
Correct Answer
4. What is the output
Send Feedback
What will be the output of the following program ?
// Class Shape and ShapeUse are in same package

class Shape{
int height;
int width;
}

public class ShapeUse {
public static void main(String[] args) {
Shape s = new Shape();
s.height = 1;
System.out.println(s.height);
}
}
Options
Error
0
1 R
None of these
Correct Answer
Solution Description
####Since height is a default data member of class Shape and Shape and ShapeUse both the classes are in same package. So we can access height via shape object in ShapeUse class.
5.What is the output
Send Feedback
What will be the output of the following program ?
// Class Shape and ShapeUse are in same package

class Shape{
private int height;
private int width;
}

public class ShapeUse {
public static void main(String[] args) {
Shape s = new Shape();
System.out.println(s.height);
}
}
Options
Error R
0
1
None of these
Correct Answer
Solution Description
####As height is a private data member of class Shape, thus it can’t be accessed outside the class.
6.Check Statements
Send Feedback
Which statement is not true in java language ?
Options
A public member of a class can be accessed in all the packages in the same project.
A private member of a class cannot be accessed by the methods of the same class.
A private member of a class cannot be accessed outside the same class.
None of the above.
Correct Answer
7.What is the output
Send Feedback
What will be the Output ?
Both classes are in same package.
class Box{
int width;
int height;
int length;
void volume(){
System.out.println(length * width * height);
}
}

public class BoxUse {
public static void main(String[] args) {
Box b = new Box();
b.height = 5;
b.width = 4;
b.volume();
}
}
Options
Error
0
20
None of the above
Correct Answer
Solution Description
####Default value for an integer data member of a class is 0. Thus default value for length is 0, whereas height and width are initialised to 5 and 4 respectively. Volume will thus be 0.
8.Fill the output
Send Feedback
What will be the output of the following code ?
class Student{
String name;
int rollNo;
Student(int num){
rollNo = num;
}
public void print(){
System.out.print(name +" " + rollNo+” “);
}
}

public class StudentUse {
public static void main(String[] args) {
Student s = new Student(12);
s.print();
}
}
Answer
null 12
Type here
Correct Answer
Solution Description
####Constructor is called when an object is created, here when we create a Student object coressponding student object is created and rollNo is assigned to 12, whereas name is assigned null (as default value for String data member is null). So output is null 12
9.Fill the output
Send Feedback
What will be the output of the following code ?
Both classes are in same package.
class Student{
String name;
int rollNo;
Student(int num){
rollNo = num;
}
public void print(){
System.out.print(name +" " + rollNo+” “);
}
}

public class StudentUse {
public static void main(String[] args) {
Student s = new Student();
s.rollNo = 15;
s.print();
}
}
Options
null 0
null 15
Compilation Error
Correct Answer
Solution Description
####There is Compilation error - “constructor Student() is undefined” because the default constructor is available only till the point we don’t create our own constructor. So Student class has only one constructor which require roll number as argument, hence we should pass integer as argument while creating any Student object.
10.What is the output
What will be the output of the following code ?
class Student{
String name;
int rollNo;

// Constructor 1
Student(int num){
rollNo = num;
name = "abc";
}

// Constructor 2
Student(int num, String str){
rollNo = num;
name = str;
}

public void print(){
System.out.print(name +" " + rollNo+" ");
}
}

public class StudentUse {
public static void main(String[] args) {
Student s1 = new Student(101);
s1.print();
Student s2 = new Student(150, "xyz");
s2.print();
}
}
Options
Error
null 101 xyz 150
abc 101 xyz 150
Correct Answer
Solution Description
####While creating first Student object we have just passed single integer argument, hence constructor 1 will be called and for second Student object constructor 2 will be called. So output will be abc 101 xyz 150.
11.Reference keyword
Send Feedback
Which keyword works as a reference to the current Object whose Method or constructor is being invoked ?
Options
import
catch
abstract
this
Correct Answer
12.What is the output
Send Feedback
What will be the output of this code ?
class Book{
final int price = 10;
}

public class MCQs {
public static void main(String[] args) {
Book b = new Book();
b.price = 16;
System.out.println(b.price);
}
}
Options
16
10
Error
None of the above
Correct Answer
Solution Description
####price is a final field, thus statement “ b.price = 16 “ is incorrect since we cannot re assign a final variable
13.What is the output
Send Feedback
What will be the output of this code ?
class Book{
int price;
static int count;

public Book(int price) {
this.price = price;
count++;
}
}

public class MCQs {
public static void main(String[] args) {
System.out.print(Book.count + " ");
Book b1 = new Book(500);
Book b2 = new Book(600);
System.out.println(Book.count);
}
}
Options
Error
0 2
0 0
2 2
Correct Answer
14.Constructor access
Send Feedback
Which of the modifier can't be used for constructors ?
Options
public
private
static
protected
Correct Answer
Solution Description
####static keyword is bound to the class and not to an individual object, thus we can’t make constructors static.
15.Complex Number Class
Send Feedback

public class ComplexNumbers {
// Complete this class
private int real;
private int imaginary;

public ComplexNumbers(int real,int imaginary){
this.real=real;

this.imaginary=imaginary;

}
public void setReal(int real){
this.real=real;
}
public void setImaginary(int imaginary){
this.imaginary=imaginary;
}
public int getReal(){
return real;
}
public int getImaginary(){
return imaginary;
}
public void print(){
System.out.println(real +" + i"+imaginary);
}
public void plus(ComplexNumbers c2){
this.real=this.real+ c2.real;
this.imaginary=this.imaginary+ c2.imaginary;
}
public void multiply(ComplexNumbers c2){
int i=(this.real*c2.real) - (this.imaginary * c2.imaginary);
this.imaginary=(this.real*c2.imaginary)+(this.imaginary*c2.real);
this.real=i;
}

}
16.Polynomial class
Send Feedbackpublic class Polynomial {
/* This function sets coefficient for a particular degree value, if degree is not there in the polynomial
* then corresponding term(with specified degree and value is added int the polynomial. If the degree
* is already present in the polynomial then previous coefficient is replaced by
* new coefficient value passed as function argument
*/
int[] arr;

//constructor
public Polynomial(){
arr = new int[100];
}
//setter
public void setCoefficient(int degree, int coeff){
if(degree>=arr.length){
int[] temp= arr;
arr= new int[degree+10];
for(int i=0;i<temp.length;i++){
arr[i]= temp[i];
}
}
arr[degree]= coeff;

}

// Prints all the terms(only terms with non zero coefficients are to be printed) in increasing order of degree.
public void print(){
for(int i=0; i<arr.length;i++){
if(arr[i]!=0){
System.out.print(arr[i]+"x"+i+" ");
}
}
}


// Adds two polynomials and returns a new polynomial which has result
public Polynomial add(Polynomial p){
int temp1[]= new int[Math.max(this.arr.length, p.arr.length)];
for(int i=0; i<temp1.length;i++){
temp1[i] = this.arr[i] + p.arr[i];
}
Polynomial p1 = new Polynomial();
for(int i=0; i<temp1.length;i++){
p1.setCoefficient(i, temp1[i]);
}
return p1;

}

// Subtracts two polynomials and returns a new polynomial which has result
public Polynomial subtract(Polynomial p){
int temp1[]= new int[Math.max(this.arr.length, p.arr.length)];
for(int i=0; i<temp1.length;i++){
temp1[i] = this.arr[i] - p.arr[i];
}
Polynomial p2 = new Polynomial();
for(int i=0; i<temp1.length;i++){
p2.setCoefficient(i, temp1[i]);
}
return p2;

}

// Multiply two polynomials and returns a new polynomial which has result
public Polynomial multiply(Polynomial p){
int temp1[]= new int[this.arr.length+p.arr.length];
for(int i=0; i<this.arr.length;i++){
for(int j=0; j<p.arr.length;j++){
temp1[i+j] = temp1[i+j] + (this.arr[i]*p.arr[j]);
}

}
Polynomial p3 = new Polynomial();
for(int i=0; i<temp1.length;i++){
p3.setCoefficient(i, temp1[i]);
}
return p3;
}
}

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.