answersLogoWhite

0

📱

Java Programming

The Java programming language was released in 1995 as a core component of the Java platform of Sun Microsystems. It is a general-purpose, class-based, object-oriented language that is widely used in application software and web applications.

5,203 Questions

What is difference between procedure oriented and Object Oriented?

  • In POP, importance is given to the sequence of things to be done i.e. algorithms and in OOP, importance is given to the data.
  • In POP, larger programs are divided into functions and in OOP, larger programs are divided into objects.
  • In POP, most functions share global data i.e data move freely around the system from function to function. In OOP mostly the data is private and only functions inside the object can access the data.
  • POP follows a top down approach in problem solving while OOP follows a bottom up approach.
  • In POP, adding of data and function is difficult and in OOP it is easy.
  • In POP, there is no access specifier and in OOP there are public, private and protected specifier.
  • In POP, operator cannot be overloaded and in OOP operator can be overloaded.
  • In POP, Data moves openly around the system from function to function, In OOP objects communicate with each other through member functions.

What are abstract classin java?

An Abstract class is a special kind of class that cannot be instantiated. It has one or more methods which are not implemented in the class. These methods are declared abstract and they do not contain any code inside them.

Ex:

abstract class Parent {

public abstract String getSon();

public abstract String getDaughter();

....

....

//More methods that contain specific behaviour/code in them

}

The above is an abstract class "Parent" that has a lot of functionality but it has declared two abstract methods which have no code inside them. Any class that has one or more abstract methods has to be abstract. This abstract class cannot be instantiated.

i.e., the below piece of code will not work. The code will not even compile.

Parent object = new Parent();

Purpose of Abstract Classes:

Abstract classes are generally used where you want an amount of behaviour to be used by the class that extends the abstract class while at the same time giving options to the child class to provide a certain amount of behaviour itself.

A Child Class extending the Abstract Class:

public class Child extends Parent {

public String getSon() {

return "Sons Name";

}

public String getDaughter(){

return "Daughters Name";

}

...

... //Code specific to the Child class

}

Different between compiler and interpreater?

The execution of a program can happen either natively -- the intructions are actual CPU instructions, or it can happen through an interpreter. The interprer thus takes instructions (which are typically not native CPU instructions), and performs the actions associated with the instruction (open a file, write a character to the screen, etc). The interpreter is thus in charge of the execution of the program instructions.

Now consider a program written in spoken English. It is obvious that the CPU does not understand spoken English. We can either use an interpreter to execute this program, or we can translate to "another form" (typically machine code specific to a particular CPU) -- using a compiler. This "other form" may require additional things to happen, so the compiler may insert extra instructions to cater for these things. The end result is our spoken English program, in another form -- either native, which can be executed by the CPU, or an a form which requires that an interpreter be used to execute it.

The interested reader is encouraged to read Allan Turing's groundbreaking paper on computing machines entitled "On computable numbers: With an application to the Entscheidungsproblem". You can find this easily by simply searching for this title with your favourite search engine.

Another AnswerWe usually prefer to write computer programs in languages we understand rather than in machine language, but the processor can only understand machine language. So we need a way of converting our instructions (source code) into machine language. This is done by an interpreter or a compiler.

An interpreter reads the source code one instruction or line at a time, converts this line into machine code and executes it. The machine code is then discarded and the next line is read. The advantage of this is it's simple and you can interrupt it while it is running, change the program and either continue or start again. The disadvantage is that every line has to be translated every time it is executed, even if it is executed many times as the program runs. Because of this interpreters tend to be slow. Examples of interpreters are Basic on older home computers, and script interpreters such as JavaScript, and languages such as Lisp and Forth.

A compiler reads the whole source code and translates it into a complete machine code program to perform the required tasks which is output as a new file. This completely separates the source code from the executable file. The biggest advantage of this is that the translation is done once only and as a separate process. The program that is run is already translated into machine code so is much faster in execution. The disadvantage is that you cannot change the program without going back to the original source code, editing that and recompiling (though for a professional software developer this is more of an advantage because it stops source code being copied). Current examples of compilers are Visual Basic, C, C++, C#, Fortran, Cobol, Ada, Pascal and so on.

You will sometimes see reference to a third type of translation program: an assembler. This is like a compiler, but works at a much lower level, where one source code line usually translates directly into one machine code instruction. Assemblers are normally used only by people who want to squeeze the last bit of performance out of a processor by working at machine code level.

Compiler

A Compiler is a program that translates code of a programming language in machine code

*****Translated source code into machine code***** .

A compiler is a special program that processes statements written in a particular programming language and converts them into machine language, a "binary program" or "code," that a computer processor uses.

A compiler works with what are sometimes called 3GL and higher-level languages (3rd-generation languages, such as Java and C

Interpreter

Interpreters translate code one line at time, executing each line as it is "translated," much the way a foreign language interpreter would translate a book, by translating one line at a time. Interpreters do generate binary code, but that code is never compiled into one program entity.

Interpreters offer programmers some advantages that compilers do not. Interpreted languages are easier to learn than compiled languages, which is great for beginning programmers. An interpreter lets the programmer know immediately when and where problems exist in the code; compiled programs make the programmer wait until the program is complete.

Interpreters therefore can be easier to use and produce more immediate results; however the source code of an interpreted language cannot run without the interpreter.

Compilers produce better optimized code that generally run faster and compiled code is self sufficient and can be run on their intended platforms without the compiler present.

When do you use protected visibility specifier to a class member in C?

a class member declared as private can only be accessed by member functions and friends of that class
a class member declared as protected can only be accessed by member functions and friends of that class,and by member functions and friends of derived classes

Is it possible in java that using user defined methods in predefined classes?

public void test(int arg1, int arg2) throws Exception{

.......

}

Above is a typical declaration of a user defined method in java.

The first word public defines the access modifier for the method. you can use public or private.

the second word defines the return type of the method. a void represents no return value. you can have int, String, float etc...

third word is the method name. You can have anything except keywords in this

The values inside the parenthesis are the arguments. A method can take any number of arguments

The throws declaration signifies that this method may throw exceptions. The calling method should have code to handle them.

Why is it good advice to indent all the statements inside a set of braces in java programming?

it is necessary to indent all the statements inside a set of braces so that the compiler can know what & when to be compiled.An opening braces indicates that a new process has created & closing braces indicates that the process is getting terminated at that point.

Find the GCD of two numbers?

#include<stdio.h>

#include<conio.h>

int find_gcd(int,int);

int find_lcm(int,int);

int main(){

int num1,num2,gcd,lcm;

clrscr();

printf("\nEnter two numbers:\n ");

scanf("%d %d",&num1,&num2);

gcd=find_gcd(num1,num2);

printf("\n\nGCD of %d and %d is: %d\n\n",num1,num2,gcd);

if(num1>num2)

lcm = find_lcm(num1,num2);

else

lcm = find_lcm(num2,num1);

printf("\n\nLCM of %d and %d is: %d\n\n",num1,num2,lcm);

return 0;

}

int find_gcd(int n1,int n2){

while(n1!=n2){

if(n1>n2)

return find_gcd(n1-n2,n2);

else

return find_gcd(n1,n2-n1);

}

return x;

}

What is API?

API is an Application Programming Interface.

It is an interface that defines the ways by which an application program may request services from libraries and/or operating systems.

An API determines the vocabulary and calling conventions the programmer should employ to use the services. It may include specifications for routines, data structures, object classes, and protocols used to communicate between the requesting software and the library.

An API itself is largely abstract in that it specifies an interface and controls the behavior of the objects specified in that interface. The software that provides the functionality described by an API is said to be an implementation of the API. An API is typically defined in terms of the programming language used to build the application.

The API initialism may sometimes be used as a reference, not only to the full interface, but also to one function, or even a set of multiple APIs provided by an organization. Thus, the scope of meaning is usually determined by the person or document that communicates the information.

Write a program for Multiplication of two matrices in java?

//Matrix multiplication import java.util.Scanner; public class Matrix

{

public static void main(String args[])

{

Scanner s= new Scanner(System.in);

int i,j,k; System.out.println("enter the value of n"); int n=s.nextInt(); int a[][]=new int[n][n]; int b[][]=new int[n][n]; int c[][]=new int[n][n]; System.out.println("enter the array elements of a:"); for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

a[i][j]=s.nextInt();

}

}//end of a matrix System.out.println("enter the array elements of b:"); for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

b[i][j]=s.nextInt();

}

}//end of b matrix System.out.println("the result matrix is:");

for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

for(k=0;k<n;k++)

{ c[i][j]+=a[i][k]*b[k][j]; }

}

} for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

System.out.print(+c[i][j]);

}System.out.println();

}

}//end of main

}//end of class

Write a c program to compare two strings without using builtin function?

A string is a character array, so you can compare them one character at a time:

String x = "test"

String y = "test"

for(int i = 0; i < x.length && i < y.length; i++)

{

if(x[i] != y[i])

return false;

}

return true;

What is the difference between a computer hacker and a computer fraud?

A hacker is someone who take time on a computer to learn how to manipulate the system and have access to the system.

Hacking is the process of manipulating data in order to get into the system without permision

What is difference between parameter and argument in java?

They are synonyms.

Some people use the word 'argument' for the 'formal parameter' and 'parameter' for the 'actual parameter', others do on the other way around.

PS:

example for the formal parameters (function declaration):

int myfun (const char *s, int p);

example for the actual parameters (function calling):

myfun ("Hello", 44);

What is a overload?

"Overloading" a function means that you have multiple functions with the same name, but different signatures. The signature of a function is its return type and number/types of parameters.

For example:

void foo(int a, char b)

can be distinguished from

void foo()

which can be distinguished from

void foo(double a)

which can be distinguished from

void foo(int a)

The program will call the correct function based off of the number and types of parameters it is given. So:

foo(1) will call the 4th example

foo(1.0) will call the 3rd example

foor() will call the 2nd example

and foo(1, 't') will call the 1st example

Note that MOST programming languages do not allow you to distinguish between function signatures by return type, thus:

void foo()

and

int foo()

is not allowed.

Function overloading should not be confused with function overriding. Overriding involves inheritance and is related to polymorphism.

Is it necessary to give the size of array?

Depends on the language. For C, no you don't. You can type blank brackets (int Arr[]) when declaring the array, or you can just use a pointer (int* Arr). Both will allow you to use the variable as an array without having to declare the specific size. Hope this answers your question.

In Java, an array is an object, and one which is dynamically allocated space. The default constructor does not require a size be specified.

What is the need of overloading in java?

Overloading is very useful in Java. For instance, methods can be overload as long as the type or number of parameters (arguments) differ for each version of the method. This comes in handy when, for instance, you need to perform a complex mathematical operation, but sometimes need to do it with two numbers and sometimes three, etc.

What are the characteristics of object oriented systems?

the three main design principles of object oriented programming are the following:

  • encapsulation - this allows the user to hide the information for outside world and doesn't allow the other user to change or modify the internal values of class.
  • polymorphism - one term in many forms
  • inheritance - offers to derive a new class from an existing one and acquire all the feature of the existing class. The new class which get the feature from the existing class is called the derived class and other class is called the base class.

What is hashcode in java?

hashcode is an integer number which is provide to each object by jvm note that this is not address of object but for convencing internally they use

Java uses the hash function given below:- s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.

Why the java hybrid language?

I am not sure why you label it "hybrid". Java compiles the source code, not for a specific processor, but for what you might consider a fictitious processor. That is, it doesn't compile for the specific machine code understood by a real processor.As for the reason, that's because that's what Java is all about. Java programs are supposed to be compiled only once, and then run on any computer that has an appropriate Java runtime (the "Java Virtual Machine").

What is the function in java to find the size of given variable?

Dependion on the variable there are several methods to do it, this can only be applied to primitive types and arrays, for an array its the "name_of_array.length", for the arraylist this change just a little, it would be like "name_of_array_list.size()", for an int, double, float, long, byte, it would be like "name_of_variable.LENGTH" this is a public variable so you dont need a get, an finally for the String there is a method "name_of_string.length()" this would return the size of this String

How do you print 1 to 10 number using for loop?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace printnumber

{

class Program

{

static void Main(string[] args)

{

int i;

for (i = 0; i < 20; i++)

{

Console.Write(i);

Console.Read();

}

}

}

}

String In JAVA language?

In Java, literal character strings such as "foo" are implemented as instances of the String class. These strings are constant and can not be modified directly. Mutable character strings, or string variables, are implemented as instances of StringBuffer class. It is also possible to work directly with arrays of chars, but String and related classes offer lots of useful methods.

Program to find palindrome in a string?

#include
#include
void main()
{
char str1[80];
int i,j,flag=1;

printf("enter a word:");
gets(str1);
for(i=0,j= (strlen(str1)-1);i<=(strlen(str1)-1),j>=0;i++,j--)
{
if(str1[i]!=str1[j])
flag=0;
}
if(flag)
printf("palindrome");
else
printf("not a palindrome");
}

What does the Java compiler translate Java source code to?

The Java compiler translates Java source code to Java byte code.

What is the function of a layoutmanager in java?

The function of a layout manager is to help the java programmer with aligning and positioning components inside the AWT or Java Swing components like a Frame or a Panel. Without the layout managers the programmer will have to position these components one by one manually which is very difficult and may also result in poor layout response when the user re-sizes the UI window.

Some of the commonly used layout managers are:

a. BorderLayout

b. GridLayout

c. GridBagLayout

d. Etc