Why do abstract class can not be inherited?
An abstract class is designed to provide function and organization to subclasses without ever existing as an object itself. In other words, it is a glorified template, an abstraction for subclasses, and would be illogical to instantiate.
As an example, imagine that a zoo wanted to make separate classes representing each of its animals, but wanted them to all have some common features like a variable for life expectancy or a method to project feeding costs. To force every class to implement this functionality, the programmer may create an abstract class called Animal that each subclass would extend. However, would it ever make sense to create an Animal? No, because an Animal does not exist anywhere in the zoo - an Alligator however might.
Why you don't use void in constructor function although it doesn't return any value?
Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this:
MyClass var = new MyClass();
Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler.
public class MyClass{
// The java compiler will insert a real constructor here
public Integer MyClass(){ //This isn't a constructor, only a simple method
return new Integer(1);
}
}
What is the constructors in class?
A class is simply the definition of a data type. A constructor is a special method of a class that is automatically invoked whenever an object of that type is instantiated, and is used to initialise the object's data members.
What is a argument constructor?
The value what we pass in the constructor is know as argument.you can look into the example given below to make it more clear.
class Test
{
Test(int a,int b)
{
int c=a+b;
System,out.println("the value of "+c);
}
}
public class testMain{
Public static void main(String args[])
{
Test t=new Test(10,20);
//here 10 and 20 are the arguments and a,b are parameters....
}
}
i hope this will help u
Kumar Ashish
What is the difference between static and non static class members in java?
Static data members are different from automatic ones in the way that their lifetime is equals to the lifetime of your program. Even if you have declared static members inside of function (class) other than main();
What is the advantages of high level languages to low level languages?
High Level Language:
High level language are easier to use and less technical skills are required to do a program. It is very useful where textual data is given priority. Troubleshooting doesn't takes a longer time. We get to know the errors very easily.
Low Level Language:
Low level language are not easily read at a glance and very high technical skill are required to do a program. Troubleshooting takes a longer time. They can produce stunning graphics.
Java is used just about everywhere. Your phone, television, computer etc. Java is the most popular programming language. Example of Java:
import java.io.*;
import java.util.*;
public class Calculator
{
public static void main(String args[])
{
System.out.println("Make your arithmetic selection from the choices below:\n");
System.out.println(" 1. Addition");
System.out.println(" 2. Subtraction");
System.out.println(" 3. Multiplication");
System.out.println(" 4. Division\n");
System.out.print(" Your choice? ");
Scanner kbReader = new Scanner(System.in);
int choice = kbReader.nextInt();
if((choice<=4) && (choice>0))
{
System.out.print("\nEnter first operand. ");
double op1 = kbReader.nextDouble();
System.out.print("\nEnter second operand.");
double op2 = kbReader.nextDouble();
System.out.println("");
switch (choice)
{
case 1: //addition
System.out.println(op1 + " plus " + op2 + " = " + (op1 + op2) );
break;
case 2: //subtraction
System.out.println(op1 + " minus " + op2 + " = " + (op1 - op2) );
break;
case 3: //multiplication
System.out.println(op1 + " times " + op2 + " = " + (op1 * op2) );
break;
case 4: //division
System.out.println(op1 + " divided by " + op2 + " = " + (op1 / op2) );
}
}
else
{
System.out.println("Please enter a 1, 2, 3, or 4.");
}
}
}
What is the difference between Statement and PreparedStatement?
* Prepared Statement is Pre-compiled class , but Statement is not. * So in PreparedStatement the execution will be faster. Actually when u submit a simple statement to the databse, at first the DBMS parses it and sends it back with the result, so again when u send the same statement again the DBMS server parses it and sends back the result so here a lot of time is wasted and because the statement is again parsed though it has been sent twice or thrice it consumes a lot of time and response will be slow.Prepared Statement is used when u want to execute a statement object many times. when u submit a PreparedStatement the DBMS server parses it and creates a execution plan. This e-plan can be used when u again send the same statement to the database.That is the DBMS server zest executes the compiled statement rather that executing it from first, hence we get an precompiled statement.And also the advanatge of using this PreparedStatement is it is used to send dynamic sql statements, which u can give values later than giving the values at the time of creation.
Difference between abstract and final class?
How can you convert an infix notation to postfix notation?
#include <iostream>
#include <stack>
using namespace std;
int prec (char ch){
// Gives precedence to different operators
switch (ch) {
case '^':
return 5;
case '/':
return 4;
case '*':
return 4;
case '+':
return 2;
case '-':
return 1;
default :
return 0;
}
}
bool isOperand(char ch){
// Finds out is a character is an operand or not
if ((ch>='0' && ch<='9') (ch>='a' && ch<='z'))
return true;
else
return false;
}
string postFix (string infix){
string pfix = "";
stack<char> opstack;
for (int i=0; i<infix.length(); i++){
// Scan character by character
if (isOperand(infix[i]))
{
pfix += infix[i];
}
else if (infix[i] ')')
{
// Retrace to last ( closure
while (opstack.top() != '(')
{
pfix += opstack.top();
opstack.pop();
}
// Remove the '(' found by while loop
opstack.pop();
}
A parameter or an argument is a value that is passed to a method so that the method can use that value in its processing.
Ex:
public String getName(String x){
return "Mr. " + x;
}
Here the method just prefixe's a Mr. with the value that is being passed to the method getName. The value X can be termed a parameter or an argument
How do you display a string on the console with java?
For example:
System.out.println("Hello.");
For example:
System.out.println("Hello.");
For example:
System.out.println("Hello.");
For example:
System.out.println("Hello.");
How do you repair an unhandled exception?
You can stop an unhandled exception by handling it. When your program crashes it will tell you exactly where it crashed and what exception it ran into. Dealing with the exception is the hard part. Generally, you want to take one of two approaches. The first is to make sure that the exception cannot happen. You may, for example, have to validate data to make sure that your users aren't allowed to give input which would result in a division-by-zero exception. The second is to allow the exception to be thrown, but use a try-catch block to catch it and print out a useful message instead of crashing. The method you choose will depend on what your program is supposed to do, who is using it, what exceptions are being thrown, where they're being thrown, etc. There's no silver bullet solution for handling an exception.
What is the difference between static and nonstatic member with example in java?
It depends on whether the member is a static variable or a static method of the class.
A non-static member variable is an instance variable. That is, each instance of the class has its own independent set of instance variables.
A static member variable is not associated with any one instance of the class, and exists even when there are no instances of the class. As with all static variables, it exists for the entire duration of the program.
A non-static member method is an instance method, thus the method automatically inherits a this pointer.
A static member method does not inherit a this pointer, but it does have private access to to the class. Thus specific instances can be passed to a static method if necessary.
Static members can be thought of as being common to all instances of a class, rather than a specific instance, even though no instances are actually required in order to make use of them.
Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer?
public class Add { public staticvoid main(String[] args) { int value=12; intcount=0; int sum=0; while(count<=4) { sum+=value%10; value=value/10; count++; } System.out.print("The sum of the individual integers is= "+ sum); } }
Write the program that display the factorial of a number?
/* gcc -ansi -Wall -Wextra -pedantic -s -static 0.c -o 0 */
#include <stdio.h>
int main ( )
{
int n , factorial = 1 ;
printf ( "enter the value of n\n") ;
scanf ( "%i" , & n ) ;
while ( n != 0 )
{
factorial *= n ;
n -- ;
}
printf ( "The factorial of n is\n%i\n" , factorial ) ;
return 0;
}
Write a program to detect duplicate elements in array?
The simplest way of doing this is not very efficient, but provides a quick development time. You want to take all of the elements of the array, add them to a Set, then retrieve them as an array again. Note that this will probably not preserve the order of elements in the array.
{
Object[] originalArray; // let's pretend this contains the data that
// you want to delete duplicates from
Set newSet = new HashSet();
for (Object o : originalArray) {
newSet.add(o);
}
// originalArray is now equal to the array without duplicates
originalArray = newSet.toArray();
}
Now the efficient, and more correct, solution.
This one will create a new array of the correct size without duplicates.
{
Object[] originalArray; // again, pretend this contains our original data
// new temporary array to hold non-duplicate data
Object[] newArray = new Object[originalArray.length];
// current index in the new array (also the number of non-dup elements)
int currentIndex = 0;
// loop through the original array...
for (int i = 0; i < originalArray.length; ++i) {
// contains => true iff newArray contains originalArray[i]
boolean contains = false;
// search through newArray to see if it contains an element equal
// to the element in originalArray[i]
for(int j = 0; j <= currentIndex; ++j) {
// if the same element is found, don't add it to the new array
if(originalArray[i].equals(newArray[j])) {
contains = true;
break;
}
}
// if we didn't find a duplicate, add the new element to the new array
if(!contains) {
// note: you may want to use a copy constructor, or a .clone()
// here if the situation warrants more than a shallow copy
newArray[currentIndex] = originalArray[i];
++currentIndex;
}
}
// OPTIONAL
// resize newArray (using _newArray) so that we don't have any null references
Object[] _newArray = new Object[currentIndex];
System.arraycopy(newArray, 0, _newArray, 0, currentIndex);
}
public static void main(String[] args) { int val = 100; int val1 = 50; System.out.println("Number of digits in " + val + " is: " + new String(val + "").length()); System.out.println("Number of digits in " + val1 + " is: " + new String(val1 + "").length()); }
Can a private method be overridden?
No. A method that is declared as private in a class is not inherited by any other class and hence if another class that extends this class declares a method with the same name and signature, it does not mean that this method is overridden. It is an entirely separate entity.
What is throw keyword in java?
The throw keyword is used from within a method to "throw" an exception to the calling method. In order to throw an exception, the method signature must indicate that it will throw an exception. For example: public void foo() throws Exception { } When you want to actually throw the exception, you can do it a few different ways: throw new Exception("Exception message"); or from within a catch block ( catch(Exception ex) ): throw ex;
An array is a group of related elements, with a common variable name. The index is a number that indicates the position of an element within an array: the 1st. element, the 2nd. element, etc. (many languages start counting at zero).
Why java is known as internet programming language?
too many reasons...it is easy to use, it contains built-in support for using computer networks, it can execute code from remote sources securley, it prevents possible errors and anti-pattern design...but there are many limitations for what java can do.
How threads are synchronized in a java?
By using an Runnable and Thread object.
EX:
Runnable r = new Runnable() {
Thread t = new Thread() {
@Override
public void run() {
//place code for new thread here
}
};
@Override
public void run() {
t.start();
}
};
r.run();
Write a program using one print statement to print the asterisks triangle?
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,row,m;
printf("Enter the no of rows:");
scanf("%d",&row);
m=row;
for(i=0;i<m;i++)
{
printf("\n");
for(k=0;k<row;k++)
printf(" ");
for(j=0;j<=i;j++)
printf(" *");
row--;
}
getch();
}
How do you read from an external file in java?
The class I find useful is the FileReader class in java.io. It allows you to read an external file in your program.
For instance,
Consider a file input.txt:
Hello
How are you?
1 2 3 4 5
You can access it by:
FileReader read = new FileReader("input.txt"); // pass the file name as a String
//now the read is the file
//can scan from the file using Scanner
Scanner scan = new Scanner(read);
System.out.println( scan.nextLine()); // prints Hello
System.out.println( scan.nextLine()); // prints How are you?
while(scan.hasNextInt())
System.out.print(scan.nextInt() + " "); // prints 1 2 3 4 5