How to assign posts without any category to the default category

You can use wp_insert_post like this:

$my_post = array(
  'post_title'    => 'My post',
  'post_content'  => 'This is my post.',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array(8,39)
);

// Insert the post into the database
wp_insert_post( $my_post );

Ref: https://codex.wordpress.org/Function_Reference/wp_insert_post

use wp_update_post to update posts.